Class: Restorm::Model::Relation
- Inherits:
-
Object
- Object
- Restorm::Model::Relation
- Defined in:
- lib/restorm/model/relation.rb
Instance Attribute Summary collapse
-
#parent ⇒ Object
writeonly
Sets the attribute parent.
Instance Method Summary collapse
-
#build(attributes = {}) ⇒ Object
Build a new resource.
-
#create(attributes = {}) ⇒ Object
Create a resource and return it.
-
#find(*ids) ⇒ Object
Fetch specific resource(s) by their ID.
-
#find_by(params) ⇒ Object
Fetch first resource with the given attributes.
-
#find_or_create_by(attributes) ⇒ Object
Fetch first resource with the given attributes, or create a resource with the attributes if one is not found.
-
#find_or_initialize_by(attributes) ⇒ Object
Fetch first resource with the given attributes, or initialize a resource with the attributes if one is not found.
-
#first_or_create(attributes = {}) ⇒ Object
Fetch a resource and create it if it’s not found.
-
#first_or_initialize(attributes = {}) ⇒ Object
Fetch a resource and build it if it’s not found.
-
#where(params = {}) ⇒ Object
(also: #all)
Add a query string parameter.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &blk) ⇒ Object
Bubble all methods to the fetched collection
46 47 48 |
# File 'lib/restorm/model/relation.rb', line 46 def method_missing(method, *args, &blk) fetch.send(method, *args, &blk) end |
Instance Attribute Details
#parent=(value) ⇒ Object (writeonly)
Sets the attribute parent
7 8 9 |
# File 'lib/restorm/model/relation.rb', line 7 def parent=(value) @parent = value end |
Instance Method Details
#build(attributes = {}) ⇒ Object
Build a new resource
21 22 23 |
# File 'lib/restorm/model/relation.rb', line 21 def build(attributes = {}) @parent.build(@params.merge(attributes)) end |
#create(attributes = {}) ⇒ Object
Create a resource and return it
166 167 168 169 170 171 172 |
# File 'lib/restorm/model/relation.rb', line 166 def create(attributes = {}) attributes ||= {} resource = @parent.new(@params.merge(attributes)) resource.save resource end |
#find(*ids) ⇒ Object
Fetch specific resource(s) by their ID
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/restorm/model/relation.rb', line 87 def find(*ids) params = @params.merge(ids.last.is_a?(Hash) ? ids.pop : {}) ids = Array(params[@parent.primary_key]) if params.key?(@parent.primary_key) results = ids.flatten.compact.uniq.map do |id| resource = nil request_params = params.merge( :_method => @parent.method_for(:find), :_path => @parent.build_request_path(params.merge(@parent.primary_key => id)) ) @parent.request(request_params) do |parsed_data, response| if response.success? resource = @parent.new_from_parsed_data(parsed_data) resource.run_callbacks :find else return nil end end resource end ids.length > 1 || ids.first.is_a?(Array) ? results : results.first end |
#find_by(params) ⇒ Object
Fetch first resource with the given attributes.
If no resource is found, returns nil
.
120 121 122 |
# File 'lib/restorm/model/relation.rb', line 120 def find_by(params) where(params).first end |
#find_or_create_by(attributes) ⇒ Object
Fetch first resource with the given attributes, or create a resource with the attributes if one is not found.
137 138 139 |
# File 'lib/restorm/model/relation.rb', line 137 def find_or_create_by(attributes) find_by(attributes) || create(attributes) end |
#find_or_initialize_by(attributes) ⇒ Object
Fetch first resource with the given attributes, or initialize a resource with the attributes if one is not found.
153 154 155 |
# File 'lib/restorm/model/relation.rb', line 153 def find_or_initialize_by(attributes) find_by(attributes) || build(attributes) end |
#first_or_create(attributes = {}) ⇒ Object
Fetch a resource and create it if it’s not found
184 185 186 |
# File 'lib/restorm/model/relation.rb', line 184 def first_or_create(attributes = {}) fetch.first || create(attributes) end |
#first_or_initialize(attributes = {}) ⇒ Object
Fetch a resource and build it if it’s not found
199 200 201 |
# File 'lib/restorm/model/relation.rb', line 199 def first_or_initialize(attributes = {}) fetch.first || build(attributes) end |
#where(params = {}) ⇒ Object Also known as: all
Add a query string parameter
34 35 36 37 38 39 40 |
# File 'lib/restorm/model/relation.rb', line 34 def where(params = {}) return self if params.blank? && !@_fetch.nil? clone.tap do |r| r.params = r.params.merge(params) r.clear_fetch_cache! end end |