Class: ActiveHash::Relation
- Inherits:
-
Object
- Object
- ActiveHash::Relation
show all
- Includes:
- Enumerable
- Defined in:
- lib/active_hash/relation.rb
Defined Under Namespace
Classes: Condition, Conditions, WhereChain
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(klass, all_records, conditions = nil, order_values = nil) ⇒ Relation
Returns a new instance of Relation.
12
13
14
15
16
17
|
# File 'lib/active_hash/relation.rb', line 12
def initialize(klass, all_records, conditions = nil, order_values = nil)
self.klass = klass
self.all_records = all_records
self.conditions = Conditions.wrap(conditions || [])
self.order_values = order_values || []
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
166
167
168
169
170
|
# File 'lib/active_hash/relation.rb', line 166
def method_missing(method_name, *args)
return super unless klass.scopes.key?(method_name)
instance_exec(*args, &klass.scopes[method_name])
end
|
Instance Attribute Details
#all_records ⇒ Object
Returns the value of attribute all_records.
10
11
12
|
# File 'lib/active_hash/relation.rb', line 10
def all_records
@all_records
end
|
#conditions ⇒ Object
Returns the value of attribute conditions.
10
11
12
|
# File 'lib/active_hash/relation.rb', line 10
def conditions
@conditions
end
|
#klass ⇒ Object
Returns the value of attribute klass.
10
11
12
|
# File 'lib/active_hash/relation.rb', line 10
def klass
@klass
end
|
#order_values ⇒ Object
Returns the value of attribute order_values.
10
11
12
|
# File 'lib/active_hash/relation.rb', line 10
def order_values
@order_values
end
|
Instance Method Details
#all(options = {}) ⇒ Object
91
92
93
94
95
96
97
|
# File 'lib/active_hash/relation.rb', line 91
def all(options = {})
if options.key?(:conditions)
where(options[:conditions])
else
where({})
end
end
|
#count ⇒ Object
133
134
135
|
# File 'lib/active_hash/relation.rb', line 133
def count
length
end
|
#find(id = nil, *args, &block) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/active_hash/relation.rb', line 107
def find(id = nil, *args, &block)
case id
when :all
all
when :first
all(*args).first
when Array
id.map { |i| find(i) }
when nil
raise RecordNotFound.new("Couldn't find #{klass.name} without an ID", klass.name, "id") unless block_given?
records.find(&block) else
find_by_id(id) || begin
raise RecordNotFound.new("Couldn't find #{klass.name} with ID=#{id}", klass.name, "id", id)
end
end
end
|
#find_by(options) ⇒ Object
99
100
101
|
# File 'lib/active_hash/relation.rb', line 99
def find_by(options)
where(options).first
end
|
#find_by!(options) ⇒ Object
103
104
105
|
# File 'lib/active_hash/relation.rb', line 103
def find_by!(options)
find_by(options) || (raise RecordNotFound.new("Couldn't find #{klass.name}", klass.name))
end
|
#find_by_id(id) ⇒ Object
125
126
127
128
129
130
131
|
# File 'lib/active_hash/relation.rb', line 125
def find_by_id(id)
index = klass.send(:record_index)[id.to_s] return unless index
record = all_records[index]
record if conditions.matches?(record)
end
|
#ids ⇒ Object
154
155
156
|
# File 'lib/active_hash/relation.rb', line 154
def ids
pluck(:id)
end
|
#invert_where ⇒ Object
51
52
53
|
# File 'lib/active_hash/relation.rb', line 51
def invert_where
spawn.invert_where!
end
|
#invert_where! ⇒ Object
55
56
57
58
|
# File 'lib/active_hash/relation.rb', line 55
def invert_where!
conditions.map(&:invert!)
self
end
|
#order(*options) ⇒ Object
38
39
40
|
# File 'lib/active_hash/relation.rb', line 38
def order(*options)
spawn.order!(*options)
end
|
#order!(*options) ⇒ Object
64
65
66
67
68
|
# File 'lib/active_hash/relation.rb', line 64
def order!(*options)
check_if_method_has_arguments!(:order, options)
self.order_values += preprocess_order_args(options)
self
end
|
#pick(*column_names) ⇒ Object
158
159
160
|
# File 'lib/active_hash/relation.rb', line 158
def pick(*column_names)
pluck(*column_names).first
end
|
#pluck(*column_names) ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/active_hash/relation.rb', line 141
def pluck(*column_names)
symbolized_column_names = column_names.map(&:to_sym)
if symbolized_column_names.length == 1
column_name = symbolized_column_names.first
all.map { |record| record[column_name] }
else
all.map do |record|
symbolized_column_names.map { |column_name| record[column_name] }
end
end
end
|
#records ⇒ Object
79
80
81
82
83
84
|
# File 'lib/active_hash/relation.rb', line 79
def records
@records ||= begin
filtered_records = apply_conditions(all_records, conditions)
ordered_records = apply_order_values(filtered_records, order_values) end
end
|
#reload ⇒ Object
86
87
88
89
|
# File 'lib/active_hash/relation.rb', line 86
def reload
@records = nil self
end
|
#reorder(*options) ⇒ Object
42
43
44
|
# File 'lib/active_hash/relation.rb', line 42
def reorder(*options)
spawn.reorder!(*options)
end
|
#reorder!(*options) ⇒ Object
70
71
72
73
74
75
76
77
|
# File 'lib/active_hash/relation.rb', line 70
def reorder!(*options)
check_if_method_has_arguments!(:order, options)
self.order_values = preprocess_order_args(options)
@records = apply_order_values(records, order_values)
self
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
172
173
174
|
# File 'lib/active_hash/relation.rb', line 172
def respond_to_missing?(method_name, include_private = false)
klass.scopes.key?(method_name) || super
end
|
#size ⇒ Object
137
138
139
|
# File 'lib/active_hash/relation.rb', line 137
def size
length
end
|
#spawn ⇒ Object
60
61
62
|
# File 'lib/active_hash/relation.rb', line 60
def spawn
self.class.new(klass, all_records, conditions, order_values)
end
|
#to_ary ⇒ Object
162
163
164
|
# File 'lib/active_hash/relation.rb', line 162
def to_ary
records.dup
end
|
#where(conditions_hash = :chain) ⇒ Object
19
20
21
22
23
|
# File 'lib/active_hash/relation.rb', line 19
def where(conditions_hash = :chain)
return WhereChain.new(self) if conditions_hash == :chain
spawn.where!(conditions_hash)
end
|
#where!(conditions_hash, inverted = false) ⇒ Object
46
47
48
49
|
# File 'lib/active_hash/relation.rb', line 46
def where!(conditions_hash, inverted = false)
self.conditions << Condition.new(conditions_hash)
self
end
|