Class: PDC::Resource::Relation
- Inherits:
-
Object
- Object
- PDC::Resource::Relation
- Includes:
- Enumerable, Logging, Finder, Pagination, Query
- Defined in:
- lib/pdc/resource/relation.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
- #params ⇒ Object
Instance Method Summary collapse
-
#all! ⇒ Object
all! returns all records of the
Resource
NOTE: use this method for resources with small footprint. - #count ⇒ Object
-
#each(&block) ⇒ Object
TODO: handle pagination here.
-
#find(id, vars = {}) ⇒ Object
TODO: need to scale this so that mulitle variables in a uri can be passed - e.g.
-
#initialize(klass, options = {}) ⇒ Relation
constructor
A new instance of Relation.
- #uri ⇒ Object
Methods included from Pagination
Methods included from Finder
Methods included from Query
Methods included from Logging
Constructor Details
#initialize(klass, options = {}) ⇒ Relation
Returns a new instance of Relation.
19 20 21 22 23 |
# File 'lib/pdc/resource/relation.rb', line 19 def initialize(klass, = {}) @klass = klass @options = @params = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
67 68 69 70 71 72 |
# File 'lib/pdc/resource/relation.rb', line 67 def method_missing(name, *args, &block) # pass anything that relation doesn't know to the klass super unless klass.respond_to? name with_scope { klass.send(name, *args, &block) } end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
13 14 15 |
# File 'lib/pdc/resource/relation.rb', line 13 def klass @klass end |
#params ⇒ Object
25 26 27 |
# File 'lib/pdc/resource/relation.rb', line 25 def params @params.symbolize_keys end |
Instance Method Details
#all! ⇒ Object
all! returns all records of the Resource
NOTE: use this method for resources with small footprint
57 58 59 |
# File 'lib/pdc/resource/relation.rb', line 57 def all! where(page_size: -1).contents! end |
#count ⇒ Object
61 62 63 |
# File 'lib/pdc/resource/relation.rb', line 61 def count result.pagination[:resource_count] end |
#each(&block) ⇒ Object
TODO: handle pagination here
46 47 48 49 50 51 52 53 |
# File 'lib/pdc/resource/relation.rb', line 46 def each(&block) return to_enum(:each) unless block_given? each_page do |relation| resources = relation.contents! resources.each(&block) end end |
#find(id, vars = {}) ⇒ Object
TODO: need to scale this so that mulitle variables in a uri can be passed - e.g.
ReleaseVarant.uri is 'v1/release-variant/(:release)/(:id)'
so find(id, release: ‘rel’) need to work
38 39 40 41 42 43 |
# File 'lib/pdc/resource/relation.rb', line 38 def find(id, vars = {}) raise PDC::ResourceNotFound if id.blank? where(primary_key => id) .where(vars) .find_one! end |
#uri ⇒ Object
29 30 31 |
# File 'lib/pdc/resource/relation.rb', line 29 def uri @options[:uri] || klass.uri end |