class Spyke::Relation

Attributes

klass[R]
params[R]

Public Class Methods

new(klass, options = {}) click to toggle source
# File lib/spyke/relation.rb, line 11
def initialize(klass, options = {})
  @klass = klass
  @options = options
  @params = {}
  @should_fallback = false
end

Public Instance Methods

each(&block) click to toggle source
# File lib/spyke/relation.rb, line 60
def each(&block)
  find_some.each(&block)
end
find(id) click to toggle source

Overrides Enumerable find

# File lib/spyke/relation.rb, line 44
def find(id)
  scoping { klass.find(id) }
end
find_one() click to toggle source
# File lib/spyke/relation.rb, line 48
def find_one
  @find_one ||= klass.new_instance_from_result(fetch)
rescue ConnectionError => error
  fallback_or_reraise(error, default: nil)
end
find_some() click to toggle source
# File lib/spyke/relation.rb, line 54
def find_some
  @find_some ||= klass.new_collection_from_result(fetch)
rescue ConnectionError => error
  fallback_or_reraise(error, default: [])
end
params=(params) click to toggle source
# File lib/spyke/relation.rb, line 24
def params=(params)
  @params = params.symbolize_keys
end
uri() click to toggle source
# File lib/spyke/relation.rb, line 64
def uri
  @options[:uri]
end
where(conditions = {}) click to toggle source
# File lib/spyke/relation.rb, line 18
def where(conditions = {})
  relation = clone
  relation.params = params.merge(conditions)
  relation
end
with(uri) click to toggle source
# File lib/spyke/relation.rb, line 28
def with(uri)
  if uri.is_a? Symbol
    @options[:uri] = File.join @options[:uri], uri.to_s
  else
    @options[:uri] = uri
  end
  where
end
with_fallback(fallback = nil) click to toggle source
# File lib/spyke/relation.rb, line 37
def with_fallback(fallback = nil)
  @should_fallback = true
  @fallback = fallback
  where
end

Private Instance Methods

fallback_or_reraise(error, default:) click to toggle source
# File lib/spyke/relation.rb, line 86
def fallback_or_reraise(error, default:)
  if should_fallback?
    @fallback || default
  else
    raise error
  end
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/spyke/relation.rb, line 70
def method_missing(name, *args, &block)
  if klass.respond_to?(name)
    scoping { klass.send(name, *args) }
  else
    super
  end
end
scoping() { || ... } click to toggle source

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

# File lib/spyke/relation.rb, line 79
def scoping
  previous, klass.current_scope = klass.current_scope, self
  yield
ensure
  klass.current_scope = previous
end
should_fallback?() click to toggle source
# File lib/spyke/relation.rb, line 94
def should_fallback?
  @should_fallback
end