tstmedia/active_zuora

View on GitHub
lib/active_zuora/fields/array_field_decorator.rb

Summary

Maintainability
A
0 mins
Test Coverage
module ActiveZuora
  class ArrayFieldDecorator < SimpleDelegator

    # Wraps a Field object and typecasts/builds
    # item as an array of the given field.

    def type_cast(values)
      # Force into an array and run type_cast on each element.
      [values].flatten.compact.map { |value| __getobj__.type_cast(value) }
    end

    def build_xml(xml, soap, values, options={})
      # It may be wierd that we're mapping and taking the last value,
      # But there's an issue with xml builder where if a block
      # returns an array, it will output the array in the XML.
      # So instead, we'll have our block return the value
      # of the last build_xml call.
      [values].flatten.compact.map do |value|
        __getobj__.build_xml(xml, soap, value, options)
      end.last
    end

    def clear_changed_attributes(values)
      [values].flatten.compact.map { |value| __getobj__.clear_changed_attributes(value) }
    end

  end
end