Class: RestResource::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/rest_resource/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#attributesObject (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

.rest_crud(params = {}) ⇒ Object



19
20
21
# File 'lib/rest_resource/resource.rb', line 19

def rest_crud(params={})
  RestCrud.new(url(params))
end

Instance Method Details

#saveObject



42
43
44
# File 'lib/rest_resource/resource.rb', line 42

def save
  self.class.rest_crud.update self.attributes
end