Class: PDC::Resource::Relation
- Inherits:
-
Object
- Object
- PDC::Resource::Relation
- Includes:
- Enumerable, Logging, Finder, Pagination, Query
- Defined in:
- lib/pdc/resource/relation.rb
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
ResourceNOTE: 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.
18 19 20 21 22 |
# File 'lib/pdc/resource/relation.rb', line 18 def initialize(klass, = {}) @klass = klass = @params = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
66 67 68 69 70 71 |
# File 'lib/pdc/resource/relation.rb', line 66 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
24 25 26 |
# File 'lib/pdc/resource/relation.rb', line 24 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
56 57 58 |
# File 'lib/pdc/resource/relation.rb', line 56 def all! where(page_size: -1).contents! end |
#count ⇒ Object
60 61 62 |
# File 'lib/pdc/resource/relation.rb', line 60 def count result.pagination[:resource_count] end |
#each(&block) ⇒ Object
TODO: handle pagination here
45 46 47 48 49 50 51 52 |
# File 'lib/pdc/resource/relation.rb', line 45 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
37 38 39 40 41 42 |
# File 'lib/pdc/resource/relation.rb', line 37 def find(id, vars = {}) raise PDC::ResourceNotFound if id.blank? where(primary_key => id) .where(vars) .find_one! end |
#uri ⇒ Object
28 29 30 |
# File 'lib/pdc/resource/relation.rb', line 28 def uri [:uri] || klass.uri end |