Class: MassiveRecord::ORM::Finders::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/massive_record/orm/finders/scope.rb

Overview

A finder scope’s jobs is to contain and build up limitations and meta data of a DB query about to being executed, allowing us to call for instance

User.select(:info).limit(5) or User.select(:info).find(a_user_id)

Each call adds restrictions or info about the query about to be executed, and the proxy will act as an Enumerable object when asking for multiple values

Constant Summary collapse

MULTI_VALUE_METHODS =
%w(select)
SINGLE_VALUE_METHODS =
%w(limit)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Scope.



30
31
32
33
34
35
36
37
38
39
# File 'lib/massive_record/orm/finders/scope.rb', line 30

def initialize(klass, options = {})
  @klass = klass
  @extra_finder_options = {}

  reset
  reset_single_values_options
  reset_multi_values_options

  apply_finder_options(options[:find_options]) if options.has_key? :find_options
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



23
24
25
# File 'lib/massive_record/orm/finders/scope.rb', line 23

def klass
  @klass
end

#loadedObject Also known as: loaded?

Returns the value of attribute loaded.



23
24
25
# File 'lib/massive_record/orm/finders/scope.rb', line 23

def loaded
  @loaded
end

Instance Method Details

#==(other) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/massive_record/orm/finders/scope.rb', line 71

def ==(other)
  case other
  when Scope
    object_id == other.object_id
  when Array
    to_a == other
  else
    raise "Don't know how to compare #{self.class} with #{other.class}"
  end
end

#all(options = {}) ⇒ Object



92
93
94
95
# File 'lib/massive_record/orm/finders/scope.rb', line 92

def all(options = {})
  apply_finder_options(options)
  to_a
end

#find(*args) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/massive_record/orm/finders/scope.rb', line 84

def find(*args)
  options = args.extract_options!.to_options
  apply_finder_options(options)
  args << options.merge(find_options)

  klass.do_find(*args)
end

#first(options = {}) ⇒ Object



97
98
99
100
# File 'lib/massive_record/orm/finders/scope.rb', line 97

def first(options = {})
  apply_finder_options(options)
  limit(1).to_a.first
end

#last(*args) ⇒ Object



102
103
104
# File 'lib/massive_record/orm/finders/scope.rb', line 102

def last(*args)
  raise "Sorry, not implemented!"
end

#limit(limit) ⇒ Object

Single value options



62
63
64
65
# File 'lib/massive_record/orm/finders/scope.rb', line 62

def limit(limit)
  self.limit_value = limit
  self
end

#resetObject



42
43
44
# File 'lib/massive_record/orm/finders/scope.rb', line 42

def reset
  @loaded = false
end

#select(*select) ⇒ Object

Multi value options



52
53
54
55
# File 'lib/massive_record/orm/finders/scope.rb', line 52

def select(*select)
  self.select_values |= select.flatten.compact.collect(&:to_s)
  self
end

#to_aObject



108
109
110
111
112
113
# File 'lib/massive_record/orm/finders/scope.rb', line 108

def to_a
  return @records if loaded?
  @records = load_records
  @records = [@records] unless @records.is_a? Array
  @records
end