Class: Nasreddin::Resource
- Inherits:
-
Object
- Object
- Nasreddin::Resource
- Defined in:
- lib/nasreddin/resource.rb
Overview
Nasreddin Resource
Provides a base class for implementing an API backed data object. A minimal implementation could be:
class Car < Nasreddin::Resource('cars')
end
Class Attribute Summary collapse
-
.resource ⇒ Object
Returns the value of attribute resource.
Class Method Summary collapse
-
.all ⇒ Object
Allows fetching of all entities without requiring filtering parameters.
-
.create(properties = {}) ⇒ Object
Allows creating a new record in one shot returns true if the record was created example usage: Car.create make: ‘Ford’, model: ‘Focus’ # => true or false.
-
.destroy(id) ⇒ Object
Allows destroying a resource without finding it example usage: Car.destroy(15) # => true or false.
-
.find(*args) ⇒ Object
Allows searching for a specific entity or a collection of entities that match a certain criteria.
- .inherited(sub) ⇒ Object
- .remote ⇒ Object
- .remote_call(params = {}, as_objects = true) ⇒ Object
- .subclasses ⇒ Object
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Object
Custom as_json implementation passes through options to @data.
-
#deleted? ⇒ Boolean
Checks if the current instance has already been deleted.
-
#destroy ⇒ Object
Destroys the current resource instance example usage: car = Car.find(15) car.destroy # => true or false.
-
#initialize(data = {}) ⇒ Resource
constructor
Initialize a new instance also defines setters for any values given example usage: car = Car.new make: ‘Ford’, model: ‘Mustang’ car.respond_to? :make= # => true.
- #method_missing(mid, *args, &block) ⇒ Object
- #remote_call(params) ⇒ Object
- #respond_to?(mid, include_private = false) ⇒ Boolean
-
#save ⇒ Object
Saves the current resource instance if the instance has an ID it sends a PUT request otherwise it sends a POST request will raise an error if the object has been deleted example usage: car = Car.find(15) car.miles += 1500 car.save # => true or false.
-
#to_json(options = {}) ⇒ Object
Custom to_json implementation passes through options to @data.
Constructor Details
#initialize(data = {}) ⇒ Resource
Initialize a new instance also defines setters for any values given example usage:
car = Car.new make: 'Ford', model: 'Mustang'
car.respond_to? :make=
# => true
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/nasreddin/resource.rb', line 135 def initialize(data={}) @deleted = false @data = data @data.each do |key, value| unless respond_to?("#{key.to_s}=") self.class.send(:define_method, "#{key.to_s}=") do |other| @data[key.to_s] = other end end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mid, *args, &block) ⇒ Object
147 148 149 150 151 152 153 |
# File 'lib/nasreddin/resource.rb', line 147 def method_missing(mid, *args, &block) if @data.keys.include?(mid.to_s) @data[mid.to_s] else super end end |
Class Attribute Details
.resource ⇒ Object
Returns the value of attribute resource.
15 16 17 |
# File 'lib/nasreddin/resource.rb', line 15 def resource @resource end |
Class Method Details
.all ⇒ Object
Allows fetching of all entities without requiring filtering parameters.
35 36 37 |
# File 'lib/nasreddin/resource.rb', line 35 def all remote_call({ method: 'GET' }, true) end |
.create(properties = {}) ⇒ Object
Allows creating a new record in one shot returns true if the record was created example usage:
Car.create make: 'Ford', model: 'Focus'
# => true or false
62 63 64 |
# File 'lib/nasreddin/resource.rb', line 62 def create(properties = {}) new(properties).save end |
.destroy(id) ⇒ Object
Allows destroying a resource without finding it example usage: Car.destroy(15) # => true or false
70 71 72 |
# File 'lib/nasreddin/resource.rb', line 70 def destroy(id) remote_call({ method: 'DELETE', id: id }, false).empty? end |
.find(*args) ⇒ Object
Allows searching for a specific entity or a collection of entities that match a certain criteria. example usage:
Car.find(15)
# => #<Car:0x5fafa486>
Car.find(make: 'Ford')
# => [ #<Car:0x5fafa486> ]
Car.find(15, make: 'Ford')
# => [ #<Car:0x5fafa486> ]
50 51 52 53 54 55 |
# File 'lib/nasreddin/resource.rb', line 50 def find(*args) params = args.last.kind_of?(Hash) ? args.pop : {} id = args.shift remote_call({ method: 'GET', id: id, params: params }) end |
.inherited(sub) ⇒ Object
19 20 21 22 |
# File 'lib/nasreddin/resource.rb', line 19 def inherited(sub) subclasses << sub sub.resource = @resource end |
.remote ⇒ Object
24 25 26 |
# File 'lib/nasreddin/resource.rb', line 24 def remote @remote ||= Nasreddin::RemoteTorqueboxAdapter.new(@resource, self) end |
.remote_call(params = {}, as_objects = true) ⇒ Object
28 29 30 31 |
# File 'lib/nasreddin/resource.rb', line 28 def remote_call(params = {}, as_objects=true) success, values = remote.call(params, as_objects) values end |
.subclasses ⇒ Object
17 |
# File 'lib/nasreddin/resource.rb', line 17 def subclasses; (@subclasses ||= []); end |
Instance Method Details
#as_json(options = {}) ⇒ Object
Custom as_json implementation passes through options to @data
83 84 85 |
# File 'lib/nasreddin/resource.rb', line 83 def as_json(={}) @data.as_json() end |
#deleted? ⇒ Boolean
Checks if the current instance has already been deleted
89 90 91 |
# File 'lib/nasreddin/resource.rb', line 89 def deleted? @deleted end |
#destroy ⇒ Object
Destroys the current resource instance example usage: car = Car.find(15) car.destroy # => true or false
123 124 125 126 127 |
# File 'lib/nasreddin/resource.rb', line 123 def destroy if !deleted? @deleted = remote_call({ method: 'DELETE', id: @data['id'] }) end end |
#remote_call(params) ⇒ Object
112 113 114 115 116 |
# File 'lib/nasreddin/resource.rb', line 112 def remote_call(params) success, values = remote.call(params, false) @data = values if values && !values.empty? success end |
#respond_to?(mid, include_private = false) ⇒ Boolean
155 156 157 |
# File 'lib/nasreddin/resource.rb', line 155 def respond_to?(mid, include_private=false) @data.keys.include?(mid.to_s) || super end |
#save ⇒ Object
Saves the current resource instance if the instance has an ID it sends a PUT request otherwise it sends a POST request will raise an error if the object has been deleted example usage:
car = Car.find(15)
car.miles += 1500
car.save
# => true or false
102 103 104 105 106 107 108 109 110 |
# File 'lib/nasreddin/resource.rb', line 102 def save raise SaveError.new("Cannot save a deleted resource") if deleted? if @data['id'].to_s.empty? remote_call({ method: 'POST', params: @data }) else remote_call({ method: 'PUT', id: @data['id'], params: @data }) end end |
#to_json(options = {}) ⇒ Object
Custom to_json implementation passes through options to @data
77 78 79 |
# File 'lib/nasreddin/resource.rb', line 77 def to_json(={}) @data.to_json() end |