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.



12
13
14
# File 'lib/facebook_ads/field_types/list.rb', line 12

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

Instance Method Details

#deserialize(value, session = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/facebook_ads/field_types/list.rb', line 16

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



40
41
42
# File 'lib/facebook_ads/field_types/list.rb', line 40

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