Class: PDC::Resource::Relation

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

Direct Known Subclasses

Associations::Association

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.



19
20
21
22
23
# File 'lib/pdc/resource/relation.rb', line 19

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)



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

#klassObject (readonly)

Returns the value of attribute klass.



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

def klass
  @klass
end

#paramsObject



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

#countObject



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

#uriObject



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

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