Class: Mogli::Model
- Inherits:
-
Object
show all
- Extended by:
- Search
- Defined in:
- lib/mogli/model.rb,
lib/mogli/model/search.rb
Direct Known Subclasses
Action, Activity, Address, Album, AppRequest, Application, Book, Checkin, Comment, Education, Event, Group, Insight, InsightValue, Interest, Link, Location, Movie, Music, Note, Photo, Post, Profile, Status, Television, Video, Work
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
- (Model) initialize(hash = {}, client = nil)
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
- (Object) method_missing(method, *args)
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
- (Object) type
Returns the value of attribute type
9
10
11
|
# File 'lib/mogli/model.rb', line 9
def type
@type
end
|
Class Method Details
+ (Object) add_creation_method(name, klass)
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
|
+ (Object) creation_keys
92
93
94
|
# File 'lib/mogli/model.rb', line 92
def self.creation_keys
@creation_properties || []
end
|
+ (Object) creation_properties(*args)
88
89
90
|
# File 'lib/mogli/model.rb', line 88
def self.creation_properties(*args)
@creation_properties = args
end
|
+ (Object) define_properties(*args)
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
|
+ (Object) find(id, client = nil, *fields)
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
|
+ (Object) has_association(name, klass)
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.(value,klass),klass))
end
add_creation_method(name,klass)
end
|
+ (Object) hash_populating_accessor(method_name, *klass)
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
|
+ (Object) included(other)
53
54
55
|
# File 'lib/mogli/model.rb', line 53
def self.included(other)
other.extend(ClassMethods)
end
|
+ (Object) property(arg)
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
|
+ (Boolean) recognize?(data)
148
149
150
|
# File 'lib/mogli/model.rb', line 148
def self.recognize?(data)
true
end
|
Instance Method Details
- (Object) ==(other)
140
141
142
|
# File 'lib/mogli/model.rb', line 140
def ==(other)
other.is_a?(Model) and self.id == other.id
end
|
- (Object) client
15
16
17
|
# File 'lib/mogli/model.rb', line 15
def client
@client || Mogli::Client.new
end
|
- (Object) client=(val)
11
12
13
|
# File 'lib/mogli/model.rb', line 11
def client=(val)
@client=val
end
|
- (Object) destroy
48
49
50
51
|
# File 'lib/mogli/model.rb', line 48
def destroy
client.delete(id)
freeze
end
|
- (Object) fetch
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
|
- (Object) merge!(other)
144
145
146
|
# File 'lib/mogli/model.rb', line 144
def merge!(other)
@_values.merge!(other.instance_variable_get("@_values"))
end
|
- (Object) post_params
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]
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
|
- (Object) warn_about_invalid_property(property)
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
|