Class: MassiveRecord::ORM::Finders::Scope
- Inherits:
-
Object
- Object
- MassiveRecord::ORM::Finders::Scope
- 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
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#loaded ⇒ Object
(also: #loaded?)
Returns the value of attribute loaded.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #all(options = {}) ⇒ Object
- #find(*args) ⇒ Object
- #first(options = {}) ⇒ Object
-
#initialize(klass, options = {}) ⇒ Scope
constructor
A new instance of Scope.
- #last(*args) ⇒ Object
-
#limit(limit) ⇒ Object
Single value options.
- #reset ⇒ Object
-
#select(*select) ⇒ Object
Multi value options.
- #to_a ⇒ Object
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, = {}) @klass = klass = {} reset ([:find_options]) if .has_key? :find_options end |
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
23 24 25 |
# File 'lib/massive_record/orm/finders/scope.rb', line 23 def klass @klass end |
#loaded ⇒ Object 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( = {}) () 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) = args.. () args << .merge() klass.do_find(*args) end |
#first(options = {}) ⇒ Object
97 98 99 100 |
# File 'lib/massive_record/orm/finders/scope.rb', line 97 def first( = {}) () 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 |
#reset ⇒ Object
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_a ⇒ Object
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 |