Module: Acfs::Resource::QueryMethods::ClassMethods
- Defined in:
- lib/acfs/resource/query_methods.rb
Instance Method Summary collapse
-
#all(params = {}, opts = {}) {|collection| ... } ⇒ Collection
(also: #where)
Try to load all resources.
-
#each_item(opts = {}) {|item| ... } ⇒ Object
Iterates over all items of all pages returned by index action.
-
#each_page(opts = {}) {|collection| ... } ⇒ Collection
Iterates over all pages returned by index action.
- #find(id_or_ids, **opts) ⇒ Object
-
#find_by(params) {|resource| ... } ⇒ self
Try to load first resource.
-
#find_by!(params) {|resource| ... } ⇒ self
Try to load first resource.
Instance Method Details
#all(params = {}, opts = {}) {|collection| ... } ⇒ Collection Also known as: where
Try to load all resources.
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/acfs/resource/query_methods.rb', line 80 def all(params = {}, opts = {}, &block) collection = ::Acfs::Collection.new self collection.__callbacks__ << block if block operation(:list, **opts, params: params) do |data, response| data.each {|obj| collection << create_resource(obj) } collection.process_response response collection.loaded! collection.__invoke__ end collection end |
#each_item(opts = {}) {|item| ... } ⇒ Object
Iterates over all items of all pages returned by index action.
Server must return a paginated resource.
199 200 201 202 203 204 205 |
# File 'lib/acfs/resource/query_methods.rb', line 199 def each_item(opts = {}) each_page(opts) do |collection| collection.each do |item| yield item, collection end end end |
#each_page(opts = {}) {|collection| ... } ⇒ Collection
Iterates over all pages returned by index action.
Server must return a paginated resource.
170 171 172 173 174 175 176 |
# File 'lib/acfs/resource/query_methods.rb', line 170 def each_page(opts = {}) cb = proc do |collection| yield collection collection.next_page(&cb) end where opts, &cb end |
#find(id, opts = {}) {|resource| ... } ⇒ self #find(ids, opts = {}) {|collection| ... } ⇒ Collection
59 60 61 62 63 64 65 |
# File 'lib/acfs/resource/query_methods.rb', line 59 def find(id_or_ids, **opts, &) if id_or_ids.respond_to? :each find_multiple(id_or_ids, opts, &) else find_single(id_or_ids, opts, &) end end |
#find_by(params) {|resource| ... } ⇒ self
Try to load first resource. Return nil if no object can be loaded.
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/acfs/resource/query_methods.rb', line 109 def find_by(params, &block) Acfs::Util::ResourceDelegator.new(new).tap do |m| m.__callbacks__ << block unless block.nil? operation(:list, params: params) do |data| if data.empty? m.__setobj__ nil else m.__setobj__ create_resource(data.first, origin: m.__getobj__) end m.__invoke__ end end end |
#find_by!(params) {|resource| ... } ⇒ self
Try to load first resource. Raise Acfs::ResourceNotFound exception if no object can be loaded.
138 139 140 141 142 143 144 145 146 |
# File 'lib/acfs/resource/query_methods.rb', line 138 def find_by!(params, &block) find_by params do |m| if m.nil? raise Acfs::ResourceNotFound.new message: 'Received erroneous ' \ "response: no `#{name}` with params #{params} found" end block&.call m end end |