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.



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

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)



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

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.



147
148
149
# File 'lib/lhs/concerns/record/chainable.rb', line 147

def _links
  @_links
end

Class Method Details

.unfold(args) ⇒ Object



149
150
151
# File 'lib/lhs/concerns/record/chainable.rb', line 149

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

Instance Method Details

#all(hash = nil) ⇒ Object



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

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

#create(data = {}) ⇒ Object



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

def create(data = {})
  @record_class.create(data, resolved_options)
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

#destroy(options = nil) ⇒ Object



177
178
179
180
181
182
183
184
185
# File 'lib/lhs/concerns/record/chainable.rb', line 177

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



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

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

#fetchObject



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

def fetch
  resolve
end

#find(*args) ⇒ Object



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

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

#find_by(params = {}) ⇒ Object



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

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

#find_by!(params = {}) ⇒ Object



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

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

#first!Object



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

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

#handle(error_class, handler) ⇒ Object



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

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

#ignore(*error_classes) ⇒ Object



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

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



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

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

#includes(*args) ⇒ Object



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

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

#includes_all(*args) ⇒ Object



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

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



290
291
292
# File 'lib/lhs/concerns/record/chainable.rb', line 290

def includes_values
  chain_includes
end

#limit(argument = nil) ⇒ Object



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

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

#option_values_hashObject

Returns a hash of options



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

def option_values_hash
  chain_options
end

#options(hash = nil) ⇒ Object



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

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

#page(page) ⇒ Object



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

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

#pagination_values_hashObject

Returns a hash of pagination values



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

def pagination_values_hash
  chain_pagination
end

#per(per) ⇒ Object



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

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

#references(*args) ⇒ Object



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

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

#references_valuesObject

Returns a hash of reference options



295
296
297
# File 'lib/lhs/concerns/record/chainable.rb', line 295

def references_values
  chain_references
end

#save(options = nil) ⇒ Object



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

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

#save!(options = nil) ⇒ Object



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

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

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



187
188
189
190
# File 'lib/lhs/concerns/record/chainable.rb', line 187

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

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



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

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

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

Returns:

  • (Boolean)


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

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

#where(hash = nil) ⇒ Object



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

def where(hash = nil)
  push(Parameter.new(hash))
end

#where_values_hashObject

Returns a hash of where conditions



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

def where_values_hash
  chain_parameters
end