Class: Teambox::Resource

Inherits:
Object
  • Object
show all
Includes:
ReferenceList
Defined in:
lib/teambox-client/teambox.rb

Overview

Represents an object located on Teambox.

Most resources belonging to a Teambox::Project can be modified, in which case you can use the save, destroy, and reload methods.

All attributes are exposed by the method_missing handler.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ReferenceList

#generate_references, #get_or_make_reference, #get_or_make_references, #get_reference, #set_reference

Constructor Details

#initialize(data, result_list = nil) ⇒ Resource

Returns a new instance of Resource.



213
214
215
216
217
218
219
220
221
# File 'lib/teambox-client/teambox.rb', line 213

def initialize(data, result_list=nil)
  @data = data
  @list = result_list || ResultSet.new(nil, nil, [], [])
  if @data['references']
    generate_references(@data['references'])
  else
    @references = result_list ? result_list.references : {}
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

:nodoc:



278
279
280
# File 'lib/teambox-client/teambox.rb', line 278

def method_missing(method, *args, &block) #:nodoc:
  @data.include?(method.to_s) ? @data[method.to_s] : super
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



210
211
212
# File 'lib/teambox-client/teambox.rb', line 210

def data
  @data
end

#listObject

Returns the value of attribute list.



210
211
212
# File 'lib/teambox-client/teambox.rb', line 210

def list
  @list
end

#referencesObject (readonly)

Returns the value of attribute references.



211
212
213
# File 'lib/teambox-client/teambox.rb', line 211

def references
  @references
end

Instance Method Details

#created_atObject

The time this object was created



228
229
230
# File 'lib/teambox-client/teambox.rb', line 228

def created_at
  @data.has_key?('created_at') ? Time.parse(data['created_at']) : nil
end

#destroyObject

Destroys the resource on teambox



273
274
275
276
# File 'lib/teambox-client/teambox.rb', line 273

def destroy
  @list.client.delete(url) if @list.client && url
  true
end

#idObject



223
224
225
# File 'lib/teambox-client/teambox.rb', line 223

def id
  @data['id']
end

#inspectObject



282
283
284
# File 'lib/teambox-client/teambox.rb', line 282

def inspect
  "#<#{self.class} @data=#{@data.inspect} list=#{@list.object_id}>"
end

#nextObject

:nodoc:



237
238
239
# File 'lib/teambox-client/teambox.rb', line 237

def next #:nodoc:
  nil
end

#prevObject

:nodoc:



241
242
243
# File 'lib/teambox-client/teambox.rb', line 241

def prev #:nodoc:
  nil
end

#reloadObject

Reloads the resource from source data

@returns self


252
253
254
255
256
257
258
259
260
# File 'lib/teambox-client/teambox.rb', line 252

def reload
  if @list.client && url
    updated = @list.client.get(url) rescue nil
    unless updated.nil?
      @data = updated.data
    end
  end
  self
end

#saveObject

Saves the resource to teambox

@returns true if saved


264
265
266
267
268
269
270
# File 'lib/teambox-client/teambox.rb', line 264

def save
  if @list.client && url
    updated = @list.client.put(url, @data) rescue nil
    return true unless updated.nil?
  end
  false
end

#updated_atObject

The last time this object was updated



233
234
235
# File 'lib/teambox-client/teambox.rb', line 233

def updated_at
  @data.has_key?('updated_at') ? Time.parse(data['updated_at']) : nil
end

#urlObject

Location of this object on teambox, used for updating



246
247
248
# File 'lib/teambox-client/teambox.rb', line 246

def url
  nil
end