Class: Bugly::BuglyObject

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bugly.rb

Direct Known Subclasses

APIResource

Constant Summary collapse

@@permanent_attributes =
Set.new([:api_key, :api_base])

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil, api_key = nil) ⇒ BuglyObject

Returns a new instance of BuglyObject.



170
171
172
173
174
175
176
177
178
# File 'lib/bugly.rb', line 170

def initialize(id=nil, api_key=nil)
  @api_key = api_key
  @values = {}
  # This really belongs in APIResource, but not putting it there allows us
  # to have a unified inspect method
  @unsaved_values = Set.new
  @transient_values = Set.new
  self.id = id if id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (protected)



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/bugly.rb', line 262

def method_missing(name, *args)
  # TODO: only allow setting in updateable classes.
  if name.to_s.end_with?('=')
    attr = name.to_s[0...-1].to_sym
    @values[attr] = args[0]
    @unsaved_values.add(attr)
    add_accessors([attr])
    return
  else
    return @values[name] if @values.has_key?(name)
  end

  begin
    super
  rescue NoMethodError => e
    if @transient_values.include?(name)
      raise NoMethodError.new(e.message + ". HINT: The '#{name}' attribute was set in the past, however. It was then wiped when refreshing the object with the result returned by Bugly's API, probably as a result of a save(). The attributes currently available on this object are: #{@values.keys.join(', ')}")
    else
      raise
    end
  end
end

Instance Attribute Details

#api_baseObject

Returns the value of attribute api_base.



162
163
164
# File 'lib/bugly.rb', line 162

def api_base
  @api_base
end

#api_keyObject

Returns the value of attribute api_key.



162
163
164
# File 'lib/bugly.rb', line 162

def api_key
  @api_key
end

Class Method Details

.construct_from(values, api_key = nil) ⇒ Object



180
181
182
183
184
# File 'lib/bugly.rb', line 180

def self.construct_from(values, api_key=nil)
  obj = self.new(values[:id], api_key)
  obj.refresh_from(values, api_key)
  obj
end

Instance Method Details

#[](k) ⇒ Object



217
218
219
220
# File 'lib/bugly.rb', line 217

def [](k)
  k = k.to_sym if k.kind_of?(String)
  @values[k]
end

#[]=(k, v) ⇒ Object



221
222
223
# File 'lib/bugly.rb', line 221

def []=(k, v)
  send(:"#{k}=", v)
end

#as_json(opts = nil) ⇒ Object



227
# File 'lib/bugly.rb', line 227

def as_json(opts=nil); @values.as_json(opts); end

#each(&blk) ⇒ Object



229
# File 'lib/bugly.rb', line 229

def each(&blk); @values.each(&blk); end

#inspectObject



188
189
190
191
# File 'lib/bugly.rb', line 188

def inspect()
  id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
  "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@values)
end

#keysObject



224
# File 'lib/bugly.rb', line 224

def keys; @values.keys; end

#refresh_from(values, api_key, partial = false) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/bugly.rb', line 193

def refresh_from(values, api_key, partial=false)
  @api_key = api_key

  removed = partial ? Set.new : Set.new(@values.keys - values.keys)
  added = Set.new(values.keys - @values.keys)
  # Wipe old state before setting new. Mark those values
  # which don't persist as transient

  instance_eval do
    remove_accessors(removed)
    add_accessors(added)
  end
  removed.each do |k|
    @values.delete(k)
    @transient_values.add(k)
    @unsaved_values.delete(k)
  end
  values.each do |k, v|
    @values[k] = Util.convert_to_bugly_object(v, api_key)
    @transient_values.delete(k)
    @unsaved_values.delete(k)
  end
end

#to_hashObject



228
# File 'lib/bugly.rb', line 228

def to_hash; @values; end

#to_json(*a) ⇒ Object



226
# File 'lib/bugly.rb', line 226

def to_json(*a); @values.to_json(*a); end

#to_s(*args) ⇒ Object



186
# File 'lib/bugly.rb', line 186

def to_s(*args); JSON.pretty_generate(@values); end

#valuesObject



225
# File 'lib/bugly.rb', line 225

def values; @values.values; end