Class: FrozenRecord::Scope

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

Defined Under Namespace

Classes: WhereChain

Constant Summary collapse

BLACKLISTED_ARRAY_METHODS =
[
  :compact!, :flatten!, :reject!, :reverse!, :rotate!, :map!,
  :shuffle!, :slice!, :sort!, :sort_by!, :delete_if,
  :keep_if, :pop, :shift, :delete_at, :compact
].to_set

Instance Method Summary collapse

Constructor Details

#initialize(klass, records) ⇒ Scope

Returns a new instance of Scope.



21
22
23
24
25
26
27
28
29
# File 'lib/frozen_record/scope.rb', line 21

def initialize(klass, records)
  @klass = klass
  @records = records
  @where_values = []
  @where_not_values = []
  @order_values = []
  @limit = nil
  @offset = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (protected)



181
182
183
184
185
186
187
188
189
# File 'lib/frozen_record/scope.rb', line 181

def method_missing(method_name, *args, &block)
  if array_delegable?(method_name)
    to_a.public_send(method_name, *args, &block)
  elsif @klass.respond_to?(method_name)
    delegate_to_class(method_name, *args, &block)
  else
    super
  end
end

Instance Method Details

#average(attribute) ⇒ Object



75
76
77
# File 'lib/frozen_record/scope.rb', line 75

def average(attribute)
  pluck(attribute).sum.to_f / count
end

#exists?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/frozen_record/scope.rb', line 87

def exists?
  !empty?
end

#find(id) ⇒ Object

Raises:



35
36
37
38
# File 'lib/frozen_record/scope.rb', line 35

def find(id)
  raise RecordNotFound, "Can't lookup record without ID" unless id
  find_by_id(id) or raise RecordNotFound, "Couldn't find a record with ID = #{id.inspect}"
end

#find_by(criterias) ⇒ Object



40
41
42
# File 'lib/frozen_record/scope.rb', line 40

def find_by(criterias)
  where(criterias).first
end

#find_by!(criterias) ⇒ Object



44
45
46
# File 'lib/frozen_record/scope.rb', line 44

def find_by!(criterias)
  where(criterias).first!
end

#find_by_id(id) ⇒ Object



31
32
33
# File 'lib/frozen_record/scope.rb', line 31

def find_by_id(id)
  matching_records.find { |r| r.id == id }
end

#first!Object



48
49
50
# File 'lib/frozen_record/scope.rb', line 48

def first!
  first or raise RecordNotFound, "No record matched"
end

#last!Object



52
53
54
# File 'lib/frozen_record/scope.rb', line 52

def last!
  last or raise RecordNotFound, "No record matched"
end

#limit(amount) ⇒ Object



107
108
109
# File 'lib/frozen_record/scope.rb', line 107

def limit(amount)
  spawn.limit!(amount)
end

#maximum(attribute) ⇒ Object



83
84
85
# File 'lib/frozen_record/scope.rb', line 83

def maximum(attribute)
  pluck(attribute).max
end

#minimum(attribute) ⇒ Object



79
80
81
# File 'lib/frozen_record/scope.rb', line 79

def minimum(attribute)
  pluck(attribute).min
end

#offset(amount) ⇒ Object



111
112
113
# File 'lib/frozen_record/scope.rb', line 111

def offset(amount)
  spawn.offset!(amount)
end

#order(*ordering) ⇒ Object



103
104
105
# File 'lib/frozen_record/scope.rb', line 103

def order(*ordering)
  spawn.order!(*ordering)
end

#pluck(*attributes) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/frozen_record/scope.rb', line 60

def pluck(*attributes)
  case attributes.length
  when 1
    to_a.map(&attributes.first)
  when 0
    raise NotImplementedError, '`.pluck` without arguments is not supported yet'
  else
    to_a.map { |r| attributes.map { |a| r[a] }}
  end
end

#respond_to_missing(method_name) ⇒ Object



115
116
117
# File 'lib/frozen_record/scope.rb', line 115

def respond_to_missing(method_name, *)
  array_delegable?(method_name) || super
end

#sum(attribute) ⇒ Object



71
72
73
# File 'lib/frozen_record/scope.rb', line 71

def sum(attribute)
  pluck(attribute).sum
end

#to_aObject



56
57
58
# File 'lib/frozen_record/scope.rb', line 56

def to_a
  @results ||= query_results
end

#where(criterias = :chain) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/frozen_record/scope.rb', line 91

def where(criterias = :chain)
  if criterias == :chain
    WhereChain.new(self)
  else
    spawn.where!(criterias)
  end
end

#where_not(criterias) ⇒ Object



99
100
101
# File 'lib/frozen_record/scope.rb', line 99

def where_not(criterias)
  spawn.where_not!(criterias)
end