Module: FbGraph2::AttributeAssigner

Extended by:
ActiveSupport::Concern
Defined in:
lib/fb_graph2/attribute_assigner.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#assign(attributes) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fb_graph2/attribute_assigner.rb', line 18

def assign(attributes)
  self.raw_attributes = attributes
  Array(self.class.registered_attributes).each do |type, keys|
    keys.each do |key|
      raw = attributes[key]
      if raw.present?
        value = case type
        when :raw
          raw
        when :date
          Date.strptime raw, '%m/%d/%Y' rescue raw
        when :time
          Time.parse raw
        when :timestamp
          Time.at raw
        when :application
          Application.new raw[:id], raw
        when :page
          Page.new raw[:id], raw
        when :pages
          Collection.new(raw).collect do |_raw_|
            Page.new _raw_[:id], _raw_
          end
        when :profile
          as_profile raw
        when :profiles
          Collection.new(raw).collect do |_raw_|
            as_profile _raw_
          end
        when :application
          Application.new raw[:id], raw
        when :user
          User.new raw[:id], raw
        else
          # NOTE: handle these attributes in each class
          next
        end
        self.send :"#{key}=", value
      end
    end
  end
end