Class: FrozenRecord::Scope

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

Defined Under Namespace

Classes: WhereChain

Constant Summary collapse

DISALLOWED_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) ⇒ Scope

Returns a new instance of Scope.



26
27
28
29
30
31
32
33
# File 'lib/frozen_record/scope.rb', line 26

def initialize(klass)
  @klass = klass
  @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)



221
222
223
224
225
226
227
228
229
# File 'lib/frozen_record/scope.rb', line 221

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

#==(other) ⇒ Object



131
132
133
134
# File 'lib/frozen_record/scope.rb', line 131

def ==(other)
  self.class === other &&
  comparable_attributes == other.comparable_attributes
end

#average(attribute) ⇒ Object



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

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

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  !empty?
end

#find(id) ⇒ Object

Raises:



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

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



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

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

#find_by!(criterias) ⇒ Object



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

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

#find_by_id(id) ⇒ Object



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

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

#first!Object



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

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

#hashObject



127
128
129
# File 'lib/frozen_record/scope.rb', line 127

def hash
  comparable_attributes.hash
end

#idsObject



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

def ids
  pluck(primary_key)
end

#last!Object



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

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

#limit(amount) ⇒ Object



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

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

#maximum(attribute) ⇒ Object



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

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

#minimum(attribute) ⇒ Object



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

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

#offset(amount) ⇒ Object



119
120
121
# File 'lib/frozen_record/scope.rb', line 119

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

#order(*ordering) ⇒ Object



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

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

#pluck(*attributes) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/frozen_record/scope.rb', line 64

def pluck(*attributes)
  case attributes.length
  when 1
    to_a.map(&attributes.first.to_sym)
  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) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/frozen_record/scope.rb', line 123

def respond_to_missing?(method_name, *)
  array_delegable?(method_name) || @klass.respond_to?(method_name) || super
end

#sum(attribute) ⇒ Object



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

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

#to_aObject



60
61
62
# File 'lib/frozen_record/scope.rb', line 60

def to_a
  query_results
end

#where(criterias = :chain) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/frozen_record/scope.rb', line 99

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

#where_not(criterias) ⇒ Object



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

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