class PDC::Resource::Relation

Attributes

klass[R]
params[W]

Public Class Methods

new(klass, options = {}) click to toggle source
# File lib/pdc/resource/relation.rb, line 19
def initialize(klass, options = {})
  @klass = klass
  @options = options
  @params = {}
end

Public Instance Methods

all!() click to toggle source

all! returns all records of the Resource NOTE: use this method for resources with small footprint

# File lib/pdc/resource/relation.rb, line 57
def all!
  where(page_size: -1).contents!
end
count() click to toggle source
# File lib/pdc/resource/relation.rb, line 61
def count
  result.pagination[:resource_count]
end
each(&block) click to toggle source

TODO: handle pagination here

# 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 = {}) click to toggle source

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

# 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
params() click to toggle source
# File lib/pdc/resource/relation.rb, line 25
def params
  @params.symbolize_keys
end
uri() click to toggle source
# File lib/pdc/resource/relation.rb, line 29
def uri
  @options[:uri] || klass.uri
end

Private Instance Methods

method_missing(name, *args, &block) click to toggle source
Calls superclass method
# 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
respond_to_missing?(name) click to toggle source
# File lib/pdc/resource/relation.rb, line 74
def respond_to_missing?(name)
  klass.respond_to? name
end
with_scope() { || ... } click to toggle source

Keep hold of current scope while running a method on the class

# File lib/pdc/resource/relation.rb, line 79
def with_scope
  previous = klass.current_scope
  klass.current_scope = self
  yield
ensure
  klass.current_scope = previous
end