Class: Spyke::Relation

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/spyke/relation.rb

Direct Known Subclasses

Associations::Association

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options = {}) ⇒ Relation

Returns a new instance of Relation.



11
12
13
14
15
16
# File 'lib/spyke/relation.rb', line 11

def initialize(klass, options = {})
  @klass = klass
  @options = options
  @params = {}
  @should_fallback = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



70
71
72
73
74
75
76
# 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

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



7
8
9
# File 'lib/spyke/relation.rb', line 7

def klass
  @klass
end

#paramsObject

Returns the value of attribute params.



8
9
10
# File 'lib/spyke/relation.rb', line 8

def params
  @params
end

Instance Method Details

#each(&block) ⇒ Object



60
61
62
# File 'lib/spyke/relation.rb', line 60

def each(&block)
  find_some.each(&block)
end

#find(id = nil, &block) ⇒ Object

Overrides Enumerable find



44
45
46
# File 'lib/spyke/relation.rb', line 44

def find(id = nil, &block)
  scoping { klass.find(id, &block) }
end

#find_oneObject



48
49
50
51
52
# 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_someObject



54
55
56
57
58
# 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

#uriObject



64
65
66
# File 'lib/spyke/relation.rb', line 64

def uri
  @options[:uri]
end

#where(conditions = {}) ⇒ Object



18
19
20
21
22
# File 'lib/spyke/relation.rb', line 18

def where(conditions = {})
  relation = clone
  relation.params = params.merge(conditions)
  relation
end

#with(uri) ⇒ Object



28
29
30
31
32
33
34
35
# 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) ⇒ Object



37
38
39
40
41
# File 'lib/spyke/relation.rb', line 37

def with_fallback(fallback = nil)
  @should_fallback = true
  @fallback = fallback
  where
end