Module: FbGraph2::AttributeAssigner

Extended by:
ActiveSupport::Concern
Included in:
Node, Struct
Defined in:
lib/fb_graph2/attribute_assigner.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#assign(attributes) ⇒ Object



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fb_graph2/attribute_assigner.rb', line 31

def assign(attributes)
  self.raw_attributes = attributes
  Array(self.class.registered_attributes).each do |type, keys|
    keys.each do |key|
      if attributes.include? key
        raw = attributes[key]
        value = case type
        when :raw
          raw
        when :int_flag
          raw == 1
        when :date
          Date.strptime raw, '%m/%d/%Y' rescue raw
        when :time
          Time.parse raw rescue raw
        when :timestamp
          Time.at raw
        when :actions
          Collection.new(raw).collect! do |_raw_|
            Struct::Action.new _raw_
          end
        when :album
          Album.new raw[:id], raw
        when :app
          App.new raw[:id], raw
        when :comment
          Comment.new raw[:id], raw
        when :group
          Group.new raw[:id], raw
        when :image_sources
          Collection.new(raw).collect! do |_raw_|
            Struct::ImageSource.new _raw_
          end
        when :messages
          Collection.new(raw).collect! do |_raw_|
            Message.new _raw_[:id], _raw_
          end
        when :location
          Struct::Location.new raw
        when :page
          Page.new raw[:id], raw
        when :pages
          Collection.new(raw).collect! do |_raw_|
            Page.new _raw_[:id], _raw_
          end
        when :place
          if raw.is_a? Hash
            Place.new raw[:id], raw
          else
            Place.new raw
          end
        when :photo
          Photo.new raw[:id], raw
        when :picture
          Struct::Picture.new raw[:data]
        when :profile
          as_profile raw
        when :profiles
          Collection.new(raw).collect! do |_raw_|
            as_profile _raw_
          end
        when :user
          User.new raw[:id], raw
        when :users
          Collection.new(raw).collect! do |_raw_|
            User.new _raw_[:id], _raw_
          end
        when :tags
          Collection.new(raw).collect! do |_raw_|
            Struct::Tag.new _raw_
          end
        when :custom
          # NOTE: handle these attributes in each class
          next
        else
          raise "unknown attribute type #{type}"
        end
        self.send :"#{key}=", value
      end
    end
  end
end