Class: RestResource::Resource
- Inherits:
-
Object
- Object
- RestResource::Resource
- Defined in:
- lib/rest_resource/resource.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
- .all(params = {}) ⇒ Object
- .create(params) ⇒ Object
- .find(resource_id, extra = {}) ⇒ Object
- .rest_crud(params = {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ Resource
constructor
A new instance of Resource.
- #save ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Resource
Returns a new instance of Resource.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rest_resource/resource.rb', line 30 def initialize(params={}) return if params.blank? params_hash = (params.is_a?(String)) ? ActiveSupport::JSON.decode(params) : params @attributes = params_hash (class << self; self; end).class_eval do params_hash.each_pair do |method_name, value| define_method(method_name) do Value.new(value).value end end end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
3 4 5 |
# File 'lib/rest_resource/resource.rb', line 3 def attributes @attributes end |
Class Method Details
.all(params = {}) ⇒ Object
14 15 16 17 |
# File 'lib/rest_resource/resource.rb', line 14 def all(params={}) val = ActiveSupport::JSON.decode(rest_crud(params).all(params)) val.map{|a|self.new(a)} end |
.create(params) ⇒ Object
10 11 12 |
# File 'lib/rest_resource/resource.rb', line 10 def create(params) self.new(rest_crud(params).create(params)) end |
.find(resource_id, extra = {}) ⇒ Object
6 7 8 |
# File 'lib/rest_resource/resource.rb', line 6 def find(resource_id, extra={}) self.new(rest_crud(extra).find(resource_id)) end |
Instance Method Details
#save ⇒ Object
42 43 44 |
# File 'lib/rest_resource/resource.rb', line 42 def save self.class.rest_crud.update self.attributes end |