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)


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

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


215
216
217
# File 'lib/lhs/concerns/record/chainable.rb', line 215

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


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

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

#fetchObject


315
316
317
# File 'lib/lhs/concerns/record/chainable.rb', line 315

def fetch
  resolve
end

#find(*args) ⇒ Object


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

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

#find_by(params = {}) ⇒ Object


270
271
272
# File 'lib/lhs/concerns/record/chainable.rb', line 270

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

#find_by!(params = {}) ⇒ Object


274
275
276
# File 'lib/lhs/concerns/record/chainable.rb', line 274

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

#first!Object


278
279
280
# File 'lib/lhs/concerns/record/chainable.rb', line 278

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

#handle(error_class, handler) ⇒ Object


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

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

#ignore(*error_classes) ⇒ Object


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

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


309
310
311
312
313
# File 'lib/lhs/concerns/record/chainable.rb', line 309

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

#includes(*args) ⇒ Object


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

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

#includes_all(*args) ⇒ Object


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

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

#includes_valuesObject

Returns a hash of include conditions


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

def includes_values
  chain_includes
end

#limit(argument = nil) ⇒ Object


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

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

#option_values_hashObject

Returns a hash of options


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

def option_values_hash
  chain_options
end

#options(hash = nil) ⇒ Object


223
224
225
# File 'lib/lhs/concerns/record/chainable.rb', line 223

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

#page(page) ⇒ Object


235
236
237
# File 'lib/lhs/concerns/record/chainable.rb', line 235

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

#pagination_values_hashObject

Returns a hash of pagination values


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

def pagination_values_hash
  chain_pagination
end

#per(per) ⇒ Object


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

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

#references(*args) ⇒ Object


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

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

#references_valuesObject

Returns a hash of reference options


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

def references_values
  chain_references
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)

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

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

#where(args = nil) ⇒ Object


207
208
209
210
211
212
213
# File 'lib/lhs/concerns/record/chainable.rb', line 207

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


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

def where_values_hash
  chain_parameters
end