Class: LHS::Record::Chainable::Chain
- Inherits:
-
Object
- Object
- LHS::Record::Chainable::Chain
show all
- Defined in:
- lib/lhs/concerns/record/chainable.rb
Overview
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(record_class, link, record = nil) ⇒ Chain
Returns a new instance of Chain.
130
131
132
133
134
|
# File 'lib/lhs/concerns/record/chainable.rb', line 130
def initialize(record_class, link, record = nil)
@record_class = record_class
@record = record
self._links = [link].compact
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
278
279
280
281
282
|
# File 'lib/lhs/concerns/record/chainable.rb', line 278
def method_missing(name, *args, &block)
scope = @record_class.scopes[name]
return instance_exec(*args, &scope) if scope
resolve.send(name, *args, &block)
end
|
Instance Attribute Details
#_links ⇒ Object
Returns the value of attribute _links.
124
125
126
|
# File 'lib/lhs/concerns/record/chainable.rb', line 124
def _links
@_links
end
|
Class Method Details
.unfold(args) ⇒ Object
126
127
128
|
# File 'lib/lhs/concerns/record/chainable.rb', line 126
def self.unfold(args)
args.size == 1 ? args[0] : args
end
|
Instance Method Details
#all(hash = nil) ⇒ Object
184
185
186
|
# File 'lib/lhs/concerns/record/chainable.rb', line 184
def all(hash = nil)
push([Parameter.new(hash), Option.new(all: true)])
end
|
#create(data = {}) ⇒ Object
136
137
138
|
# File 'lib/lhs/concerns/record/chainable.rb', line 136
def create(data = {})
@record_class.create(data, resolved_options)
end
|
#create!(data = {}) ⇒ Object
140
141
142
|
# File 'lib/lhs/concerns/record/chainable.rb', line 140
def create!(data = {})
@record_class.create!(data, resolved_options)
end
|
#destroy(options = nil) ⇒ Object
154
155
156
157
158
159
160
161
162
|
# File 'lib/lhs/concerns/record/chainable.rb', line 154
def destroy(options = nil)
options ||= {}
options = options.respond_to?(:to_h) ? options : { id: options }
if @record
@record.destroy(resolved_options.merge(options))
else
@record_class.destroy(options, resolved_options)
end
end
|
#expanded(options = nil) ⇒ Object
188
189
190
|
# File 'lib/lhs/concerns/record/chainable.rb', line 188
def expanded(options = nil)
push(Option.new(expanded: options || true))
end
|
#find(*args) ⇒ Object
227
228
229
|
# File 'lib/lhs/concerns/record/chainable.rb', line 227
def find(*args)
@record_class.find(*args.push(resolved_options))
end
|
#find_by(params = {}) ⇒ Object
231
232
233
|
# File 'lib/lhs/concerns/record/chainable.rb', line 231
def find_by(params = {})
@record_class.find_by(params, resolved_options)
end
|
#find_by!(params = {}) ⇒ Object
235
236
237
|
# File 'lib/lhs/concerns/record/chainable.rb', line 235
def find_by!(params = {})
@record_class.find_by!(params, resolved_options)
end
|
#first! ⇒ Object
239
240
241
|
# File 'lib/lhs/concerns/record/chainable.rb', line 239
def first!
@record_class.first!(resolved_options)
end
|
#handle(error_class, handler) ⇒ Object
209
210
211
|
# File 'lib/lhs/concerns/record/chainable.rb', line 209
def handle(error_class, handler)
push(ErrorHandling.new(error_class => handler))
end
|
#include_all!(args) ⇒ Object
Adds additional .references(name_of_linked_resource: { all: true }) to all linked resources included with includes_all
270
271
272
273
274
|
# File 'lib/lhs/concerns/record/chainable.rb', line 270
def include_all!(args)
includes_all_to_references(args).each do |reference|
_links.push(reference)
end
end
|
#includes(*args) ⇒ Object
213
214
215
|
# File 'lib/lhs/concerns/record/chainable.rb', line 213
def includes(*args)
push(Include.new(Chain.unfold(args)))
end
|
#includes_all(*args) ⇒ Object
217
218
219
220
221
|
# File 'lib/lhs/concerns/record/chainable.rb', line 217
def includes_all(*args)
chain = push(Include.new(Chain.unfold(args)))
chain.include_all!(args)
chain
end
|
#includes_values ⇒ Object
Returns a hash of include conditions
259
260
261
|
# File 'lib/lhs/concerns/record/chainable.rb', line 259
def includes_values
chain_includes
end
|
#limit(argument = nil) ⇒ Object
204
205
206
207
|
# File 'lib/lhs/concerns/record/chainable.rb', line 204
def limit(argument = nil)
return resolve.limit if argument.blank?
push(.new(per: argument))
end
|
#option_values_hash ⇒ Object
Returns a hash of options
249
250
251
|
# File 'lib/lhs/concerns/record/chainable.rb', line 249
def option_values_hash
chain_options
end
|
#options(hash = nil) ⇒ Object
192
193
194
|
# File 'lib/lhs/concerns/record/chainable.rb', line 192
def options(hash = nil)
push(Option.new(hash))
end
|
#page(page) ⇒ Object
196
197
198
|
# File 'lib/lhs/concerns/record/chainable.rb', line 196
def page(page)
push(.new(page: page))
end
|
Returns a hash of pagination values
254
255
256
|
# File 'lib/lhs/concerns/record/chainable.rb', line 254
def
end
|
#per(per) ⇒ Object
200
201
202
|
# File 'lib/lhs/concerns/record/chainable.rb', line 200
def per(per)
push(.new(per: per))
end
|
#references(*args) ⇒ Object
223
224
225
|
# File 'lib/lhs/concerns/record/chainable.rb', line 223
def references(*args)
push(Reference.new(Chain.unfold(args)))
end
|
#references_values ⇒ Object
Returns a hash of reference options
264
265
266
|
# File 'lib/lhs/concerns/record/chainable.rb', line 264
def references_values
chain_references
end
|
#save(options = nil) ⇒ Object
149
150
151
152
|
# File 'lib/lhs/concerns/record/chainable.rb', line 149
def save(options = nil)
options ||= {}
@record.save(resolved_options.merge(options))
end
|
#save!(options = nil) ⇒ Object
144
145
146
147
|
# File 'lib/lhs/concerns/record/chainable.rb', line 144
def save!(options = nil)
options ||= {}
@record.save!(resolved_options.merge(options))
end
|
#update(data = {}, options = nil) ⇒ Object
164
165
166
167
|
# File 'lib/lhs/concerns/record/chainable.rb', line 164
def update(data = {}, options = nil)
options ||= {}
@record.update(data, resolved_options.merge(options))
end
|
#update!(data = {}, options = nil) ⇒ Object
169
170
171
172
|
# File 'lib/lhs/concerns/record/chainable.rb', line 169
def update!(data = {}, options = nil)
options ||= {}
@record.update!(data, resolved_options.merge(options))
end
|
#valid?(options = nil) ⇒ Boolean
Also known as:
validate
174
175
176
177
|
# File 'lib/lhs/concerns/record/chainable.rb', line 174
def valid?(options = nil)
options ||= {}
@record.valid?(resolved_options.merge(options))
end
|
#where(hash = nil) ⇒ Object
180
181
182
|
# File 'lib/lhs/concerns/record/chainable.rb', line 180
def where(hash = nil)
push(Parameter.new(hash))
end
|
#where_values_hash ⇒ Object
Returns a hash of where conditions
244
245
246
|
# File 'lib/lhs/concerns/record/chainable.rb', line 244
def where_values_hash
chain_parameters
end
|