Class: MotionRecord::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/motion_record/scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @klass = klass
  @conditions = options[:conditions] || {} # TODO: freeze?
  @order = options[:order]
  @limit = options[:limit]
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



8
9
10
# File 'lib/motion_record/scope.rb', line 8

def conditions
  @conditions
end

#klassObject (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_allObject



63
64
65
# File 'lib/motion_record/scope.rb', line 63

def delete_all
  connection.delete(self)
end

#exists?Boolean

Read-only queries

Returns:

  • (Boolean)


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_allObject



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

#firstObject



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

#predicateObject



95
96
97
# File 'lib/motion_record/scope.rb', line 95

def predicate
  predicate_segments.join(" ")
end

#predicate?Boolean

SQL helpers

Returns:

  • (Boolean)


91
92
93
# File 'lib/motion_record/scope.rb', line 91

def predicate?
  predicate_segments.any?
end

#predicate_valuesObject



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

#where(conditions = {}) ⇒ Object

Scope builder



19
20
21
# File 'lib/motion_record/scope.rb', line 19

def where(conditions={})
  Scope.new(@klass, :conditions => @conditions.merge(conditions), :order => @order, :limit => @limit)
end