Class: Model

Inherits:
Object
  • Object
show all
Includes:
ValueTransformer
Defined in:
lib/projectile/model.rb

Direct Known Subclasses

Comment, Post

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ValueTransformer

included

Constructor Details

#initialize(json = {}) ⇒ Model



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/projectile/model.rb', line 170

def initialize(json={})
  self.class.get_relationships.each do |relationship|
    name = relationship[:name]
    default = relationship[:default]
    key_path = relationship[:key_path]
    json_value = json.valueForKeyPath(key_path)
    value_to_send = json_value.nil? ? default : json_value
    self.send("#{name}=", value_to_send) unless value_to_send.nil?
  end

  self.class.get_attributes.each do |attribute|
    name = attribute[:name]
    default = attribute[:default]
    key_path = attribute[:key_path]
    json_value = json.valueForKeyPath(key_path)
    value_to_send = json_value.nil? ? default : json_value
    self.send("#{name}=", value_to_send) unless value_to_send.nil?
  end
end

Class Method Details

.attributesObject



52
53
54
# File 'lib/projectile/model.rb', line 52

def attributes
  @attributes ||= []
end

.get_attributesObject

these methods are needed because subclasses do not inherit class instance variables



32
33
34
35
36
37
38
39
40
# File 'lib/projectile/model.rb', line 32

def get_attributes
  attributes = []
  klass = self
  while klass.respond_to? :attributes
    attributes += klass.attributes
    klass = klass.superclass
  end
  attributes
end

.get_relationshipsObject



42
43
44
45
46
47
48
49
50
# File 'lib/projectile/model.rb', line 42

def get_relationships
  relationships = []
  klass = self
  while klass.respond_to? :relationships
    relationships += klass.relationships
    klass = klass.superclass
  end
  relationships
end

.relationshipsObject



76
77
78
# File 'lib/projectile/model.rb', line 76

def relationships
  @relationships ||= []
end

.set_attribute(attribute) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/projectile/model.rb', line 56

def set_attribute(attribute)
  name = attribute[:name]
  type = attribute[:type]
  default = attribute[:default]
  attribute[:key_path] ||= name

  attr_accessor name

  # setter
  define_method("#{name}=") do |value|
    transformer = self.class.value_transformers[type][:to]
    new_value = transformer.call(value) if transformer
    self.willChangeValueForKey name
    self.instance_variable_set("@#{name}", new_value)
    self.didChangeValueForKey name
  end

  self.attributes << attribute
end

.set_relationship(relationship) ⇒ Object



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
# File 'lib/projectile/model.rb', line 80

def set_relationship(relationship)
  name = relationship[:name]
  class_name = relationship[:class_name]
  default = relationship[:default]
  relationship[:key_path] ||= name

  attr_accessor name

  # setter
  define_method("#{name}=") do |value|
    cls = Kernel.const_get(class_name.capitalize)
    new_value = case value
    when Hash
      cls.include?(IdentityMap) ? cls.merge_or_insert(value) : cls.new(value)
    when Array
      value.map {|e| (e.is_a?(cls) ? e : (cls.include?(IdentityMap) ? cls.merge_or_insert(e) : cls.new(e)))}
    when cls
      value
    else
      nil
    end if value

    self.willChangeValueForKey name
    self.instance_variable_set("@#{name}", new_value)
    self.didChangeValueForKey name
  end

  self.relationships << relationship
end

Instance Method Details

#to_hash(serialized_hash_by_model = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/projectile/model.rb', line 111

def to_hash(serialized_hash_by_model={})
  hash = {}
  serialized_hash_by_model[self] = hash

  self.class.get_attributes.each do |attribute|
    name = attribute[:name]
    key_path = attribute[:key_path]
    type = attribute[:type]
    default = attribute[:default]

    # grab value
    model_value = self.send("#{name}")
    next unless model_value

    # create intermediate hashes
    components = key_path.split(".")
    inner_hash = hash
    components.each_with_index do |component, index|
      if index == components.count-1
        transformer = self.class.value_transformers[type][:from]
        inner_hash[component] = transformer.call(model_value)
      else
        hash[component] ||= Hash.new
        inner_hash = hash[component]
      end
    end
  end

  self.class.get_relationships.each do |relationship|
    name = relationship[:name]
    key_path = relationship[:key_path]
    class_name = relationship[:class_name]

    model_value = self.send("#{name}")
    next unless model_value

    components = key_path.split(".")
    inner_hash = hash
    components.each_with_index do |component, index|
      if index == components.count-1
        inner_hash[component] = case model_value
        when Array
          model_value.compact.map do |e|
            serialized_hash_by_model[e] ? serialized_hash_by_model[e].clone : e.to_hash(serialized_hash_by_model)
          end
        when Model
          serialized_hash_by_model[model_value] ? serialized_hash_by_model[model_value].clone : model_value.to_hash(serialized_hash_by_model)
        else
        end
      else
        hash[component] ||= Hash.new
        inner_hash = hash[component]
      end
    end
  end

  hash
end