Class: MotionRecord::Scope
- Inherits:
-
Object
- Object
- MotionRecord::Scope
- Defined in:
- lib/motion_record/scope.rb
Instance Attribute Summary collapse
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
- #average(column) ⇒ Object
-
#count(column = nil) ⇒ Object
Calculations.
- #delete_all ⇒ Object
-
#exists? ⇒ Boolean
Read-only queries.
- #find(id) ⇒ Object
- #find_all ⇒ Object
- #first ⇒ Object
-
#initialize(klass, options = {}) ⇒ Scope
constructor
A new instance of Scope.
- #limit(limit_value) ⇒ Object
- #maximum(column) ⇒ Object
- #minimum(column) ⇒ Object
- #order(ordering_term) ⇒ Object
- #pluck(attribute) ⇒ Object
- #predicate ⇒ Object
-
#predicate? ⇒ Boolean
SQL helpers.
- #predicate_values ⇒ Object
- #sum(column) ⇒ Object
-
#update_all(params) ⇒ Object
Persistence queries.
-
#where(conditions = {}) ⇒ Object
Scope builder.
Constructor Details
#initialize(klass, options = {}) ⇒ Scope
Returns a new instance of Scope.
10 11 12 13 14 15 |
# File 'lib/motion_record/scope.rb', line 10 def initialize(klass, = {}) @klass = klass @conditions = [:conditions] || {} # TODO: freeze? @order = [:order] @limit = [:limit] end |
Instance Attribute Details
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
8 9 10 |
# File 'lib/motion_record/scope.rb', line 8 def conditions @conditions end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
7 8 9 |
# File 'lib/motion_record/scope.rb', line 7 def klass @klass end |
Instance Method Details
#average(column) ⇒ Object
85 86 87 |
# File 'lib/motion_record/scope.rb', line 85 def average(column) calculate(:average, column) end |
#count(column = nil) ⇒ Object
Calculations
69 70 71 |
# File 'lib/motion_record/scope.rb', line 69 def count(column=nil) calculate(:count, column) end |
#delete_all ⇒ Object
63 64 65 |
# File 'lib/motion_record/scope.rb', line 63 def delete_all connection.delete(self) end |
#exists? ⇒ Boolean
Read-only queries
33 34 35 |
# File 'lib/motion_record/scope.rb', line 33 def exists? count > 0 end |
#find(id) ⇒ Object
41 42 43 |
# File 'lib/motion_record/scope.rb', line 41 def find(id) self.where(@klass.primary_key => id).first end |
#find_all ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/motion_record/scope.rb', line 45 def find_all connection.select(self).map do |row| record = @klass.new(@klass.deserialize_table_params(row)) record.mark_persisted! record end end |
#first ⇒ Object
37 38 39 |
# File 'lib/motion_record/scope.rb', line 37 def first limit(1).find_all.first end |
#limit(limit_value) ⇒ Object
27 28 29 |
# File 'lib/motion_record/scope.rb', line 27 def limit(limit_value) Scope.new(@klass, :conditions => @conditions, :order => @order, :limit => limit_value) end |
#maximum(column) ⇒ Object
73 74 75 |
# File 'lib/motion_record/scope.rb', line 73 def maximum(column) calculate(:maximum, column) end |
#minimum(column) ⇒ Object
77 78 79 |
# File 'lib/motion_record/scope.rb', line 77 def minimum(column) calculate(:minimum, column) end |
#order(ordering_term) ⇒ Object
23 24 25 |
# File 'lib/motion_record/scope.rb', line 23 def order(ordering_term) Scope.new(@klass, :conditions => @conditions, :order => ordering_term, :limit => @limit) end |
#pluck(attribute) ⇒ Object
53 54 55 |
# File 'lib/motion_record/scope.rb', line 53 def pluck(attribute) connection.select(self).map { |row| row[attribute] } end |
#predicate ⇒ Object
95 96 97 |
# File 'lib/motion_record/scope.rb', line 95 def predicate predicate_segments.join(" ") end |
#predicate? ⇒ Boolean
SQL helpers
91 92 93 |
# File 'lib/motion_record/scope.rb', line 91 def predicate? predicate_segments.any? end |
#predicate_values ⇒ Object
99 100 101 |
# File 'lib/motion_record/scope.rb', line 99 def predicate_values condition_columns.map { |column| @conditions[column] } end |
#sum(column) ⇒ Object
81 82 83 |
# File 'lib/motion_record/scope.rb', line 81 def sum(column) calculate(:sum, column) end |
#update_all(params) ⇒ Object
Persistence queries
59 60 61 |
# File 'lib/motion_record/scope.rb', line 59 def update_all(params) connection.update(self, params) end |