Class: Yammer::Base
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from ApiHandler
establish_api_handler
Constructor Details
#initialize(props = {}) {|_self| ... } ⇒ Base
Returns a new instance of Base.
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/yammer/base.rb', line 85
def initialize(props={})
@klass = self.class
@modified_attributes = {}
@new_record = true
@loaded = false
@attrs = props
self.id = @attrs.delete(:id)
self.update(@attrs)
yield self if block_given?
end
|
Instance Attribute Details
#attrs ⇒ Object
Returns the value of attribute attrs.
83
84
85
|
# File 'lib/yammer/base.rb', line 83
def attrs
@attrs
end
|
#id ⇒ Object
Returns the value of attribute id.
83
84
85
|
# File 'lib/yammer/base.rb', line 83
def id
@id
end
|
Class Method Details
.attr_accessor_deffered(*symbols) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/yammer/base.rb', line 58
def attr_accessor_deffered(*symbols)
symbols.each do |key|
model_attributes[key] = false
define_method(key.to_s) do
load_deferred_attribute!(key)
instance_variable_get("@#{key}")
end
define_method("#{key}=") do |value|
load_deferred_attribute!(key)
if persisted? && loaded?
@modified_attributes[key] = value
else
@attrs[key] = value
end
instance_variable_set("@#{key}", value)
end
end
end
|
.base_name ⇒ Object
Returns the non-qualified class name
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/yammer/base.rb', line 8
def base_name
@base_name ||= begin
word = "#{name.split(/::/).last}"
word.gsub!(/::/, '/')
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
word.tr!("-", "_")
word.downcase!
word
end
end
|
.fetch(id) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/yammer/base.rb', line 32
def fetch(id)
return unless identity_map
attributes = identity_map.get("#{base_name}_#{id}")
unless attributes
result = api_handler.send("get_#{base_name}", id)
attributes = result.empty? ? nil : result.body
unless attributes.empty?
identity_map.put("#{base_name}_#{id}", attributes)
end
end
attributes
end
|
Fetches JSON reprsentation for object model with provided id
and returns a model instance with attributes
25
26
27
28
|
# File 'lib/yammer/base.rb', line 25
def get(id)
attrs = fetch(id)
attrs ? new(attrs) : nil
end
|
.identity_map ⇒ Object
46
47
48
|
# File 'lib/yammer/base.rb', line 46
def identity_map
@identity_map ||= Yammer::IdentityMap.new
end
|
.model_attributes ⇒ Object
Returns a hash of all attributes that are meant to trigger an HTTP request
52
53
54
|
# File 'lib/yammer/base.rb', line 52
def model_attributes
@model_attributes ||= {}
end
|
Instance Method Details
#api_handler ⇒ Object
97
98
99
|
# File 'lib/yammer/base.rb', line 97
def api_handler
@klass.api_handler
end
|
#base_name ⇒ Object
101
102
103
|
# File 'lib/yammer/base.rb', line 101
def base_name
@klass.base_name
end
|
#changes ⇒ Object
113
114
115
|
# File 'lib/yammer/base.rb', line 113
def changes
@modified_attributes
end
|
#delete! ⇒ Object
149
150
151
152
153
|
# File 'lib/yammer/base.rb', line 149
def delete!
return if new_record?
result = api_handler.send("delete_#{base_name}", @id)
result.success?
end
|
#load! ⇒ Object
125
126
127
128
129
130
|
# File 'lib/yammer/base.rb', line 125
def load!
@attrs = @klass.fetch(@id)
@loaded = true
update(@attrs)
self
end
|
#load_deferred_attribute!(key) ⇒ Object
173
174
175
176
177
178
179
180
|
# File 'lib/yammer/base.rb', line 173
def load_deferred_attribute!(key)
if @attrs.empty? && persisted? && !loaded?
load!
if !@attrs.has_key?(key)
raise "The key: #{key} appears not to be supported for model: #{self.base_name} \n #{@attrs.keys.inspect}"
end
end
end
|
#loaded? ⇒ Boolean
121
122
123
|
# File 'lib/yammer/base.rb', line 121
def loaded?
@loaded
end
|
#modified? ⇒ Boolean
117
118
119
|
# File 'lib/yammer/base.rb', line 117
def modified?
!changes.empty?
end
|
#new_record? ⇒ Boolean
105
106
107
|
# File 'lib/yammer/base.rb', line 105
def new_record?
@new_record
end
|
#persisted? ⇒ Boolean
109
110
111
|
# File 'lib/yammer/base.rb', line 109
def persisted?
!new_record?
end
|
#reload! ⇒ Object
132
133
134
135
|
# File 'lib/yammer/base.rb', line 132
def reload!
reset!
load!
end
|
#save ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/yammer/base.rb', line 137
def save
return self if ((persisted? && @modified_attributes.empty?) || @attrs.empty?)
result = if new_record?
api_handler.send("create_#{base_name}", @attrs)
else
api_handler.send("update_#{base_name}", @id, @modified_attributes)
end
@modified_attributes = {}
self
end
|
#update(attrs = {}) ⇒ Object
set all fetchable attributes
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/yammer/base.rb', line 183
def update(attrs={})
attrs.each do |key, value|
send("#{key}=", value)
end
if persisted? && !loaded?
@loaded = @klass.model_attributes.keys.inject(true) do |result, key|
result && @attrs.has_key?(key)
end
end
end
|