Class: LHS::Record::Chainable::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/lhs/concerns/record/chainable.rb

Overview

A sequence of links

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.



157
158
159
160
161
# File 'lib/lhs/concerns/record/chainable.rb', line 157

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 (protected)



331
332
333
334
335
# File 'lib/lhs/concerns/record/chainable.rb', line 331

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

Returns the value of attribute _links.



151
152
153
# File 'lib/lhs/concerns/record/chainable.rb', line 151

def _links
  @_links
end

Class Method Details

.unfold(args) ⇒ Object



153
154
155
# File 'lib/lhs/concerns/record/chainable.rb', line 153

def self.unfold(args)
  (args.size == 1) ? args[0] : args
end

Instance Method Details

#all(hash = nil) ⇒ Object



225
226
227
# File 'lib/lhs/concerns/record/chainable.rb', line 225

def all(hash = nil)
  push([Parameter.new(hash), Option.new(all: true)])
end

#create(data = {}) ⇒ Object



163
164
165
# File 'lib/lhs/concerns/record/chainable.rb', line 163

def create(data = {})
  @record_class.create(data, resolved_options)
end

#create!(data = {}) ⇒ Object



167
168
169
# File 'lib/lhs/concerns/record/chainable.rb', line 167

def create!(data = {})
  @record_class.create!(data, resolved_options)
end

#destroy(options = nil) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/lhs/concerns/record/chainable.rb', line 181

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



229
230
231
# File 'lib/lhs/concerns/record/chainable.rb', line 229

def expanded(options = nil)
  push(Option.new(expanded: options || true))
end

#fetchObject



325
326
327
# File 'lib/lhs/concerns/record/chainable.rb', line 325

def fetch
  resolve
end

#find(*args) ⇒ Object



276
277
278
# File 'lib/lhs/concerns/record/chainable.rb', line 276

def find(*args)
  @record_class.find(*args.push(resolved_options))
end

#find_by(params = {}) ⇒ Object



280
281
282
# File 'lib/lhs/concerns/record/chainable.rb', line 280

def find_by(params = {})
  @record_class.find_by(params, resolved_options)
end

#find_by!(params = {}) ⇒ Object



284
285
286
# File 'lib/lhs/concerns/record/chainable.rb', line 284

def find_by!(params = {})
  @record_class.find_by!(params, resolved_options)
end

#first!Object



288
289
290
# File 'lib/lhs/concerns/record/chainable.rb', line 288

def first!
  @record_class.first!(resolved_options)
end

#ignore(*error_classes) ⇒ Object



237
238
239
240
241
242
243
# File 'lib/lhs/concerns/record/chainable.rb', line 237

def ignore(*error_classes)
  chain = push(IgnoredError.new(error_classes.shift))
  error_classes.each do |error_class|
    chain._links.push(IgnoredError.new(error_class))
  end
  chain
end

#include_all!(args) ⇒ Object

Adds additional .references(name_of_linked_resource: { all: true }) to all linked resources included with includes_all



319
320
321
322
323
# File 'lib/lhs/concerns/record/chainable.rb', line 319

def include_all!(args)
  includes_all_to_references(args).each do |reference|
    _links.push(reference)
  end
end

#includes(*args) ⇒ Object



266
267
268
269
270
# File 'lib/lhs/concerns/record/chainable.rb', line 266

def includes(*args)
  chain = push(Include.new(Chain.unfold(args)))
  chain.include_all!(args)
  chain
end

#includes_first_page(*args) ⇒ Object



262
263
264
# File 'lib/lhs/concerns/record/chainable.rb', line 262

def includes_first_page(*args)
  push(Include.new(Chain.unfold(args)))
end

#includes_valuesObject

Returns a hash of include conditions



308
309
310
# File 'lib/lhs/concerns/record/chainable.rb', line 308

def includes_values
  chain_includes
end

#limit(argument = nil) ⇒ Object



253
254
255
256
# File 'lib/lhs/concerns/record/chainable.rb', line 253

def limit(argument = nil)
  return resolve.limit if argument.blank?
  push(Pagination.new(per: argument))
end

#option_values_hashObject

Returns a hash of options



298
299
300
# File 'lib/lhs/concerns/record/chainable.rb', line 298

def option_values_hash
  chain_options
end

#options(hash = nil) ⇒ Object



233
234
235
# File 'lib/lhs/concerns/record/chainable.rb', line 233

def options(hash = nil)
  push(Option.new(hash))
end

#page(page) ⇒ Object



245
246
247
# File 'lib/lhs/concerns/record/chainable.rb', line 245

def page(page)
  push(Pagination.new(page: page))
end

#pagination_values_hashObject

Returns a hash of pagination values



303
304
305
# File 'lib/lhs/concerns/record/chainable.rb', line 303

def pagination_values_hash
  chain_pagination
end

#partial_update(data = {}, options = nil) ⇒ Object



201
202
203
204
# File 'lib/lhs/concerns/record/chainable.rb', line 201

def partial_update(data = {}, options = nil)
  options ||= {}
  @record.update(data, resolved_options.merge(options), true)
end

#partial_update!(data = {}, options = nil) ⇒ Object



206
207
208
209
# File 'lib/lhs/concerns/record/chainable.rb', line 206

def partial_update!(data = {}, options = nil)
  options ||= {}
  @record.update!(data, resolved_options.merge(options), true)
end

#per(per) ⇒ Object



249
250
251
# File 'lib/lhs/concerns/record/chainable.rb', line 249

def per(per)
  push(Pagination.new(per: per))
end

#references(*args) ⇒ Object



272
273
274
# File 'lib/lhs/concerns/record/chainable.rb', line 272

def references(*args)
  push(Reference.new(Chain.unfold(args)))
end

#references_valuesObject

Returns a hash of reference options



313
314
315
# File 'lib/lhs/concerns/record/chainable.rb', line 313

def references_values
  chain_references
end

#rescue(error_class, handler) ⇒ Object



258
259
260
# File 'lib/lhs/concerns/record/chainable.rb', line 258

def rescue(error_class, handler)
  push(ErrorHandling.new(error_class => handler))
end

#save(options = nil) ⇒ Object



176
177
178
179
# File 'lib/lhs/concerns/record/chainable.rb', line 176

def save(options = nil)
  options ||= {}
  @record.save(resolved_options.merge(options))
end

#save!(options = nil) ⇒ Object



171
172
173
174
# File 'lib/lhs/concerns/record/chainable.rb', line 171

def save!(options = nil)
  options ||= {}
  @record.save!(resolved_options.merge(options))
end

#update(data = {}, options = nil) ⇒ Object



191
192
193
194
# File 'lib/lhs/concerns/record/chainable.rb', line 191

def update(data = {}, options = nil)
  options ||= {}
  @record.update(data, resolved_options.merge(options))
end

#update!(data = {}, options = nil) ⇒ Object



196
197
198
199
# File 'lib/lhs/concerns/record/chainable.rb', line 196

def update!(data = {}, options = nil)
  options ||= {}
  @record.update!(data, resolved_options.merge(options))
end

#valid?(options = nil) ⇒ Boolean Also known as: validate

Returns:

  • (Boolean)


211
212
213
214
# File 'lib/lhs/concerns/record/chainable.rb', line 211

def valid?(options = nil)
  options ||= {}
  @record.valid?(resolved_options.merge(options))
end

#where(args = nil) ⇒ Object



217
218
219
220
221
222
223
# File 'lib/lhs/concerns/record/chainable.rb', line 217

def where(args = nil)
  if LHS::Record.href?(args)
    push(Option.new(url: args))
  else
    push(Parameter.new(args))
  end
end

#where_values_hashObject

Returns a hash of where conditions



293
294
295
# File 'lib/lhs/concerns/record/chainable.rb', line 293

def where_values_hash
  chain_parameters
end