Module: FacebookAds::FieldTypes

Extended by:
FieldTypes
Included in:
FieldTypes, Base
Defined in:
lib/facebook_ads/field_types.rb,
lib/facebook_ads/field_types/base.rb,
lib/facebook_ads/field_types/enum.rb,
lib/facebook_ads/field_types/list.rb,
lib/facebook_ads/field_types/double.rb,
lib/facebook_ads/field_types/object.rb,
lib/facebook_ads/field_types/boolean.rb,
lib/facebook_ads/field_types/integer.rb,
lib/facebook_ads/field_types/datetime.rb,
lib/facebook_ads/field_types/ad_object.rb,
lib/facebook_ads/field_types/string_type.rb,
lib/facebook_ads/field_types/upload_file.rb

Defined Under Namespace

Classes: AdObject, Base, Boolean, Double, Enum, Integer, List, MyDateTime, Object, StringType, UploadFile

Instance Method Summary collapse

Instance Method Details

#for(type_spec) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/facebook_ads/field_types.rb', line 22

def for(type_spec)
  if type_spec.is_a?(Hash)
    type_key = type_spec.keys[0]

    if field_type = lookup(type_key)
      field_type.new(type_spec[type_key])
    else
      raise "Invalid type: #{type_key}"
    end
  else
    if (field_type = lookup(type_spec))
      field_type.new
    else
      begin
        ad_object_class = FacebookAds.const_get(type_spec)
        field_type = lookup('ad_object')
        field_type.new(ad_object_class)
      rescue NameError
        # fallback to string class if nothing matches
        lookup('string').new
      end
    end
  end
end

#lookup(type_name) ⇒ Object



18
19
20
# File 'lib/facebook_ads/field_types.rb', line 18

def lookup(type_name)
  @@registry && @@registry[type_name]
end

#register(*type_names) ⇒ Object



11
12
13
14
15
16
# File 'lib/facebook_ads/field_types.rb', line 11

def register(*type_names)
  @@registry ||= {}
  type_names.each do |type_name|
    @@registry[type_name] = self
  end
end