Class: Enginn::ResourceIndex Abstract
- Inherits:
-
Object
- Object
- Enginn::ResourceIndex
- Includes:
- Enumerable
- Defined in:
- lib/enginn/resource_index.rb
Overview
Override the ResourceIndex.resource method to implement.
A ResourceIndex is a collection of fetchable Resource.
It can be filtered and paginated through the chainable methods #per, #page, and #where. It also includes the Enumerable module so methods such as #each, #map or #to_a are available.
Actual API requests are only issued when the #each method (or any method from Enumerable) is called. While #each-ing, new API request will be issued when the end of a page is reached. Note that when using #page, only the given page is reached for. One can also force fetching manually using #fetch.
Direct Known Subclasses
CharactersIndex, DictionaryEntriesIndex, LineTagsIndex, LinesIndex, ProjectsIndex, ScenesIndex, SynthesisExportsIndex, TakeBatchesIndex, TakesIndex
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#pagination ⇒ Object
Returns the value of attribute pagination.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
Class Method Summary collapse
- .path ⇒ String private
-
.resource ⇒ Enginn::Resource
private
Define the type of Resource to use with this kind of ResourceIndex.
Instance Method Summary collapse
- #each {|item| ... } ⇒ self
-
#fetch ⇒ Boolean
Same as #fetch! but return false instead of raising an exception.
-
#fetch! ⇒ true
Fetch the current page from the API.
-
#initialize(project) ⇒ ResourceIndex
constructor
A new instance of ResourceIndex.
- #inspect ⇒ String
-
#page(page) ⇒ Enginn::ResourceIndex
A new index with updated pagination.
-
#per(per) ⇒ Enginn::ResourceIndex
A new index with updated pagination.
- #route ⇒ String private
-
#where(filters) ⇒ Enginn::ResourceIndex
A new filtered index.
Constructor Details
#initialize(project) ⇒ ResourceIndex
Returns a new instance of ResourceIndex.
42 43 44 45 46 47 |
# File 'lib/enginn/resource_index.rb', line 42 def initialize(project) @project = project @filters = {} @pagination = { current: 1 } @errors = [] end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
38 39 40 |
# File 'lib/enginn/resource_index.rb', line 38 def errors @errors end |
#filters ⇒ Object
Returns the value of attribute filters.
39 40 41 |
# File 'lib/enginn/resource_index.rb', line 39 def filters @filters end |
#pagination ⇒ Object
Returns the value of attribute pagination.
39 40 41 |
# File 'lib/enginn/resource_index.rb', line 39 def pagination @pagination end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
38 39 40 |
# File 'lib/enginn/resource_index.rb', line 38 def project @project end |
Class Method Details
.path ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
32 33 34 |
# File 'lib/enginn/resource_index.rb', line 32 def self.path resource.path end |
.resource ⇒ Enginn::Resource
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Define the type of Enginn::Resource to use with this kind of Enginn::ResourceIndex.
26 27 28 |
# File 'lib/enginn/resource_index.rb', line 26 def self.resource raise "resource is not overriden for #{self}" end |
Instance Method Details
#each {|item| ... } ⇒ self
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/enginn/resource_index.rb', line 51 def each(&block) fetch! @collection.each(&block) return self if @pagination[:locked] while @pagination[:current] < @pagination[:last] pagination[:current] += 1 fetch! @collection.each(&block) end @pagination = { current: 1 } self end |
#fetch ⇒ Boolean
107 108 109 110 111 112 113 |
# File 'lib/enginn/resource_index.rb', line 107 def fetch fetch! true rescue Faraday::Error => e @errors << e.response false end |
#fetch! ⇒ true
Fetch the current page from the API. Resulting items of the collection are wrapped in the corresponding Enginn::Resource subclass.
94 95 96 97 98 99 100 101 |
# File 'lib/enginn/resource_index.rb', line 94 def fetch! response = request @pagination = response[:pagination] @collection = response[:result].map do |attributes| self.class.resource.new(@project, attributes) end true end |
#inspect ⇒ String
116 117 118 119 120 121 |
# File 'lib/enginn/resource_index.rb', line 116 def inspect attributes = instance_variables.map do |var| "#{var}=#{instance_variable_get(var)}" end "#<#{self.class} #{attributes.join(', ')}>" end |
#page(page) ⇒ Enginn::ResourceIndex
Returns A new index with updated pagination.
68 69 70 71 72 |
# File 'lib/enginn/resource_index.rb', line 68 def page(page) new_index = clone new_index.pagination = @pagination.merge(current: page, locked: true) new_index end |
#per(per) ⇒ Enginn::ResourceIndex
Returns A new index with updated pagination.
76 77 78 79 80 |
# File 'lib/enginn/resource_index.rb', line 76 def per(per) new_index = clone new_index.pagination = @pagination.merge(per: per) new_index end |
#route ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
125 126 127 |
# File 'lib/enginn/resource_index.rb', line 125 def route "#{@project.route}/#{self.class.path}" end |
#where(filters) ⇒ Enginn::ResourceIndex
Returns A new filtered index.
84 85 86 87 88 |
# File 'lib/enginn/resource_index.rb', line 84 def where(filters) new_index = clone new_index.filters = @filters.merge(filters || {}) new_index end |