Class: Wodify::Resource
- Inherits:
-
Object
- Object
- Wodify::Resource
- Includes:
- ActiveModel::Model
- Defined in:
- lib/wodify/resource.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.response ⇒ Object
Returns the value of attribute response.
Class Method Summary collapse
-
.all(query = {}) ⇒ Object
returns a collection of all remote objects for a resource.
-
.resource(custom_resource = nil) ⇒ Object
name of the rest resource.
Instance Method Summary collapse
-
#assign_attributes(h) ⇒ Object
assings the passed hash’s attributes to self.
-
#attributes ⇒ Object
returns accessible attributes as defined by @attributes.
-
#initialize(attributes, h) ⇒ Resource
constructor
attributes should be a list of attributes that can be set on the remote object.
- #inspect ⇒ Object
-
#to_h ⇒ Object
returns all keys listed in @attributes and their values as a hash.
Constructor Details
#initialize(attributes, h) ⇒ Resource
attributes should be a list of attributes that can be set on the remote object. h will be assigned to self when passed. for safety it is assumed that the remote object already exists if an id is set in h
RemoteObject.new(:resource, { some: 'attribute' }
Note: attributes are only assigned per those defeind in @attributes
17 18 19 20 21 22 23 24 |
# File 'lib/wodify/resource.rb', line 17 def initialize(attributes, h) super() @attributes = attributes @resource = self.class.resource @created = h[:id].present? assign_attributes h if h end |
Class Attribute Details
.response ⇒ Object
Returns the value of attribute response.
66 67 68 |
# File 'lib/wodify/resource.rb', line 66 def response @response end |
Class Method Details
.all(query = {}) ⇒ Object
returns a collection of all remote objects for a resource
RemoteObject.all
83 84 85 86 |
# File 'lib/wodify/resource.rb', line 83 def all(query={}) opts = { query: query } response = Wodify::Requester.request :get, resource, opts end |
.resource(custom_resource = nil) ⇒ Object
name of the rest resource. created by convention using the class’s name
RemoteObject.resource # => :remoteobject
if the resource can't be created by convention, it can be passed
resource('thisisnot/restful')
76 77 78 |
# File 'lib/wodify/resource.rb', line 76 def resource(custom_resource=nil) @resource ||= custom_resource || name.demodulize.downcase.to_sym end |
Instance Method Details
#assign_attributes(h) ⇒ Object
assings the passed hash’s attributes to self. only keys listed in @attributes will be assigned.
object.assign_attributes({remote_id: 1})
37 38 39 40 41 42 43 44 |
# File 'lib/wodify/resource.rb', line 37 def assign_attributes(h) @attributes.each do |key| self.send("#{key}=", h[key]) if h.has_key? key end self.to_h end |
#attributes ⇒ Object
returns accessible attributes as defined by @attributes
object.attributes
29 30 31 |
# File 'lib/wodify/resource.rb', line 29 def attributes @attributes end |
#inspect ⇒ Object
60 61 62 |
# File 'lib/wodify/resource.rb', line 60 def inspect to_h.to_s end |
#to_h ⇒ Object
returns all keys listed in @attributes and their values as a hash
object.to_h
50 51 52 53 54 55 56 57 58 |
# File 'lib/wodify/resource.rb', line 50 def to_h h = {} @attributes.each do |key| h[key] = self.send key end h end |