Class: FacebookAds::FieldTypes::List

Inherits:
Base
  • Object
show all
Defined in:
lib/facebook_ads/field_types/list.rb

Instance Method Summary collapse

Methods included from FacebookAds::FieldTypes

#for, #lookup, #register

Constructor Details

#initialize(sub_type) ⇒ List

Returns a new instance of List.



24
25
26
# File 'lib/facebook_ads/field_types/list.rb', line 24

def initialize(sub_type)
  @sub_type = self.class.for(sub_type)
end

Instance Method Details

#deserialize(value, session = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/facebook_ads/field_types/list.rb', line 28

def deserialize(value, session = nil)
  case value
    when String
      JSON.parse(value).map { |v|
        @sub_type.deserialize(v, session)
      }
    when Array
      value.map { |v|
        @sub_type.deserialize(v, session)
      }
    when Hash
      # Special handle for images
      if value.length == 1 && value.keys.first == 'images'
        value = value['images']
      end

      value.map { |_,v|
        @sub_type.deserialize(v, session)
      }
    else
      [value]
  end
end

#serialize(value) ⇒ Object



52
53
54
# File 'lib/facebook_ads/field_types/list.rb', line 52

def serialize(value)
  JSON.generate(value)
end