Class: PDC::Resource::Relation

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Logging, Finder, Pagination, Query
Defined in:
lib/pdc/resource/relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Pagination

#each_page

Methods included from Finder

#contents!, #find_one!

Methods included from Query

#where

Methods included from Logging

#logger

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, options = {})
  @klass = klass
  @options = options
  @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

#klassObject (readonly)

Returns the value of attribute klass.



13
14
15
# File 'lib/pdc/resource/relation.rb', line 13

def klass
  @klass
end

#paramsObject



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

#countObject



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

#uriObject



28
29
30
# File 'lib/pdc/resource/relation.rb', line 28

def uri
  @options[:uri] || klass.uri
end