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



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

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)



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

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.



145
146
147
# File 'lib/lhs/concerns/record/chainable.rb', line 145

def _links
  @_links
end

Class Method Details

.unfold(args) ⇒ Object



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

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

Instance Method Details

#all(hash = nil) ⇒ Object



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

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

#create(data = {}) ⇒ Object



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

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

#create!(data = {}) ⇒ Object



161
162
163
# File 'lib/lhs/concerns/record/chainable.rb', line 161

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

#destroy(options = nil) ⇒ Object



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

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



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

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

#fetchObject



305
306
307
# File 'lib/lhs/concerns/record/chainable.rb', line 305

def fetch
  resolve
end

#find(*args) ⇒ Object



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

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

#find_by(params = {}) ⇒ Object



260
261
262
# File 'lib/lhs/concerns/record/chainable.rb', line 260

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

#find_by!(params = {}) ⇒ Object



264
265
266
# File 'lib/lhs/concerns/record/chainable.rb', line 264

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

#first!Object



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

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

#handle(error_class, handler) ⇒ Object



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

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

#ignore(*error_classes) ⇒ Object



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

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



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

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

#includes(*args) ⇒ Object



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

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

#includes_all(*args) ⇒ Object



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

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



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

def includes_values
  chain_includes
end

#limit(argument = nil) ⇒ Object



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

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

#option_values_hashObject

Returns a hash of options



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

def option_values_hash
  chain_options
end

#options(hash = nil) ⇒ Object



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

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

#page(page) ⇒ Object



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

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

#pagination_values_hashObject

Returns a hash of pagination values



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

def pagination_values_hash
  chain_pagination
end

#per(per) ⇒ Object



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

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

#references(*args) ⇒ Object



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

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

#references_valuesObject

Returns a hash of reference options



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

def references_values
  chain_references
end

#save(options = nil) ⇒ Object



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

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

#save!(options = nil) ⇒ Object



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

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

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



185
186
187
188
# File 'lib/lhs/concerns/record/chainable.rb', line 185

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

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



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

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

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



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

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

#where(hash = nil) ⇒ Object



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

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

#where_values_hashObject

Returns a hash of where conditions



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

def where_values_hash
  chain_parameters
end