Class: Her::Model::Relation
- Inherits:
-
Object
- Object
- Her::Model::Relation
- Defined in:
- lib/castle-her/model/relation.rb
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.
-
#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
44 45 46 |
# File 'lib/castle-her/model/relation.rb', line 44 def method_missing(method, *args, &blk) fetch.send(method, *args, &blk) end |
Instance Method Details
#build(attributes = {}) ⇒ Object
Build a new resource
19 20 21 |
# File 'lib/castle-her/model/relation.rb', line 19 def build(attributes = {}) @parent.build(@params.merge(attributes)) end |
#create(attributes = {}) ⇒ Object
Create a resource and return it
121 122 123 124 125 126 127 |
# File 'lib/castle-her/model/relation.rb', line 121 def create(attributes = {}) attributes ||= {} resource = @parent.new(@params.merge(attributes)) resource.save resource end |
#find(*ids) ⇒ Object
Fetch specific resource(s) by their ID
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/castle-her/model/relation.rb', line 85 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.instance_variable_set(:@changed_attributes, {}) resource.run_callbacks :find else return nil end end resource end ids.length > 1 || ids.first.kind_of?(Array) ? results : results.first end |
#first_or_create(attributes = {}) ⇒ Object
Fetch a resource and create it if it’s not found
139 140 141 |
# File 'lib/castle-her/model/relation.rb', line 139 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
154 155 156 |
# File 'lib/castle-her/model/relation.rb', line 154 def first_or_initialize(attributes = {}) fetch.first || build(attributes) end |
#where(params = {}) ⇒ Object Also known as: all
Add a query string parameter
32 33 34 35 36 37 38 |
# File 'lib/castle-her/model/relation.rb', line 32 def where(params = {}) return self if params.blank? && !@_fetch.nil? self.clone.tap do |r| r.params = r.params.merge(params) r.clear_fetch_cache! end end |