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("#{k}=",v)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



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

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



108
109
110
111
112
113
114
115
# File 'lib/mogli/model.rb', line 108

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



92
93
94
# File 'lib/mogli/model.rb', line 92

def self.creation_keys
  @creation_properties || []
end

.creation_properties(*args) ⇒ Object



88
89
90
# File 'lib/mogli/model.rb', line 88

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

.define_properties(*args) ⇒ Object



82
83
84
85
86
# File 'lib/mogli/model.rb', line 82

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

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



152
153
154
155
156
# File 'lib/mogli/model.rb', line 152

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

.has_association(name, klass) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/mogli/model.rb', line 117

def self.has_association(name,klass)
  define_method name do |*fields|
    body_args = fields.empty? ? {} : {:fields => fields}
    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)
end

.hash_populating_accessor(method_name, *klass) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mogli/model.rb', line 96

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)

end

.included(other) ⇒ Object



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

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

.property(arg) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/mogli/model.rb', line 69

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)


148
149
150
# File 'lib/mogli/model.rb', line 148

def self.recognize?(data)
  true
end

Instance Method Details

#==(other) ⇒ Object



140
141
142
# File 'lib/mogli/model.rb', line 140

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



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

def destroy
  client.delete(id)
  freeze
end

#fetchObject

Raises:

  • (ArgumentError)


133
134
135
136
137
138
# File 'lib/mogli/model.rb', line 133

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

#merge!(other) ⇒ Object



144
145
146
# File 'lib/mogli/model.rb', line 144

def merge!(other)
  @_values.merge!(other.instance_variable_get("@_values"))
end

#post_paramsObject



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

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



65
66
67
# File 'lib/mogli/model.rb', line 65

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