Class: Mogli::Model

Inherits:
Object
  • Object
show all
Extended by:
Search
Defined in:
lib/mogli/model.rb,
lib/mogli/model/search.rb

Defined Under Namespace

Modules: Search

Instance Attribute Summary collapse

Attributes included from Search

#search_type

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Search

search

Constructor Details

#initialize(hash = {}, client = nil) ⇒ Model

Returns a new instance of Model



19
20
21
22
23
24
25
# File 'lib/mogli/model.rb', line 19

def initialize(hash={},client=nil)
  @_values = {}
  self.client=client
  hash.each do |k,v|
    self.send("#{self.class.fql_mapping[k]||k}=",v)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/mogli/model.rb', line 64

def method_missing(method, *args)
  method_as_s = method.to_s
  if method_as_s.to_s[-1].chr == "="
    warn_about_invalid_property(method_as_s.chop)
  else
    super
  end
end

Instance Attribute Details

#typeObject

Returns the value of attribute type



9
10
11
# File 'lib/mogli/model.rb', line 9

def type
  @type
end

Class Method Details

.add_creation_method(name, klass) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/mogli/model.rb', line 128

def self.add_creation_method(name,klass)
  define_method "#{name}_create" do |*args|
    arg = args.first
    params = arg.nil? ? {} : arg.post_params
    klass_to_send = arg.nil? ? nil : klass
    client.post("#{id}/#{name}", klass_to_send, params)
  end
end

.creation_keysObject



99
100
101
# File 'lib/mogli/model.rb', line 99

def self.creation_keys
  @creation_properties || []
end

.creation_properties(*args) ⇒ Object



95
96
97
# File 'lib/mogli/model.rb', line 95

def self.creation_properties(*args)
  @creation_properties = args
end

.define_properties(*args) ⇒ Object



89
90
91
92
93
# File 'lib/mogli/model.rb', line 89

def self.define_properties(*args)
  args.each do |arg|
    property arg
  end
end

.find(id, client = nil, *fields) ⇒ Object



179
180
181
182
183
# File 'lib/mogli/model.rb', line 179

def self.find(id,client=nil, *fields)
  body_args = fields.empty? ? {} : {:fields => fields.join(',')}
  (id, body_args[:ids] = "", id.join(',')) if id.is_a?(Array)
  (client||Mogli::Client.new).get_and_map(id,self, body_args)
end

.fql_mapping(hash = nil) ⇒ Object



58
59
60
61
62
63
# File 'lib/mogli/model.rb', line 58

def self.fql_mapping(hash=nil)
  if hash
    @fql_mapping = hash
  end
  @fql_mapping || {}
end

.has_association(name, klass) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/mogli/model.rb', line 137

def self.has_association(name,klass)
  define_method name do |*fields|
    body_args = fields.empty? ? {} : {:fields => fields.join(',')}
    if (ret=instance_variable_get("@#{name}")).nil?
      ret = client.get_and_map("#{id}/#{name}",klass, body_args)
      instance_variable_set("@#{name}",ret)
    end
    return ret
  end
  define_method "#{name}=" do |value|
    instance_variable_set("@#{name}",client.map_to_class(client.extract_hash_or_array(value,klass),klass))
  end

  add_creation_method(name,klass)
  (@associations ||= []) << name
end

.hash_populating_accessor(method_name, *klass) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/mogli/model.rb', line 103

def self.hash_populating_accessor(method_name,*klass)
  define_method "#{method_name}=" do |hash|
    instance_variable_set("@#{method_name}",client.map_data(hash,klass))
  end
  define_method "#{method_name}" do
    instance_variable_get "@#{method_name}"
  end

  add_creation_method(method_name,klass)
  (@populating_accessors ||= []) << method_name
end

.hash_populating_accessor_with_default_field(method_name, default_field, *klass) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/mogli/model.rb', line 115

def self.hash_populating_accessor_with_default_field(method_name,default_field,*klass)
  define_method "#{method_name}=" do |hash|
    hash={default_field=>hash} if hash.is_a?(String)
    instance_variable_set("@#{method_name}",client.map_data(hash,klass))
  end
  define_method "#{method_name}" do
    instance_variable_get "@#{method_name}"
  end

  add_creation_method(method_name,klass)
  (@populating_accessors ||= []) << method_name
end

.included(other) ⇒ Object



54
55
56
# File 'lib/mogli/model.rb', line 54

def self.included(other)
  other.extend(ClassMethods)
end

.property(arg) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/mogli/model.rb', line 76

def self.property(arg)
  @properties ||= []
  @properties << arg
  define_method arg do
    @_values[arg.to_s]
  end
  define_method "#{arg}=" do |val|
    @_values[arg.to_s] = val
  end
end

.recognize?(data) ⇒ Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/mogli/model.rb', line 175

def self.recognize?(data)
  true
end

Instance Method Details

#==(other) ⇒ Object



161
162
163
# File 'lib/mogli/model.rb', line 161

def ==(other)
  other.is_a?(Model) and self.id == other.id
end

#clientObject



15
16
17
# File 'lib/mogli/model.rb', line 15

def client
  @client || Mogli::Client.new
end

#client=(val) ⇒ Object



11
12
13
# File 'lib/mogli/model.rb', line 11

def client=(val)
  @client=val
end

#destroyObject



49
50
51
52
# File 'lib/mogli/model.rb', line 49

def destroy
  client.delete(id)
  freeze
end

#fetch(*fields) ⇒ Object

Raises:

  • (ArgumentError)


154
155
156
157
158
159
# File 'lib/mogli/model.rb', line 154

def fetch(*fields)
  raise ArgumentError.new("You cannot fetch models without a populated id attribute") if id.nil?
  other = self.class.find(id,client,fields)
  merge!(other) if other
  self
end

#merge!(other) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/mogli/model.rb', line 165

def merge!(other)
  @_values.merge!(other.instance_variable_get("@_values"))
  # We need to copy not only root values, but, for example, user.location
  ( (self.class.instance_variable_get("@populating_accessors") || []) +
    (self.class.instance_variable_get("@associations") || [])).each do |var_name|

    instance_variable_set("@#{var_name}", other.instance_variable_get("@#{var_name}"))
  end
end

#post_paramsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mogli/model.rb', line 28

def post_params
  post_params = {}
  self.class.creation_keys.each do |key|
    post_params[key] =  @_values[key.to_s]

    # make sure actions and any other creation_properties that aren't just
    # hash entries get added...
    if post_params[key].nil? && self.respond_to?(key.to_sym) && !(val=self.send(key.to_sym)).nil?
       post_params[key] = if val.is_a?(Array)
                            "[#{val.map { |v| v.respond_to?(:to_json) ? v.to_json : nil }.compact.join(',')}]"  
                         elsif val.respond_to?(:to_json)
                           val.to_json
                         else
                           nil
                         end
    end
  end

  post_params
end

#warn_about_invalid_property(property) ⇒ Object



72
73
74
# File 'lib/mogli/model.rb', line 72

def warn_about_invalid_property(property)
  puts "Warning: property #{property} doesn't exist for class #{self.class.name}"
end