Class: DHS::Record::Chainable::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/dhs/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/dhs/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, **keyword_args, &block) ⇒ Object (protected)



345
346
347
348
349
# File 'lib/dhs/concerns/record/chainable.rb', line 345

def method_missing(name, *args, **keyword_args, &block)
  scope = @record_class.scopes[name]
  return instance_exec(*args, &scope) if scope
  resolve.send(name, *args, **keyword_args, &block)
end

Instance Attribute Details

Returns the value of attribute _links.



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

def _links
  @_links
end

Class Method Details

.unfold(args) ⇒ Object



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

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

Instance Method Details

#all(hash = nil) ⇒ Object



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

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

#create(data = {}) ⇒ Object



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

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

#create!(data = {}) ⇒ Object



167
168
169
# File 'lib/dhs/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/dhs/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



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

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

#fetchObject



339
340
341
# File 'lib/dhs/concerns/record/chainable.rb', line 339

def fetch
  resolve
end

#find(*args) ⇒ Object



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

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

#find_by(params = {}) ⇒ Object



294
295
296
# File 'lib/dhs/concerns/record/chainable.rb', line 294

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

#find_by!(params = {}) ⇒ Object



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

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

#first!Object



302
303
304
# File 'lib/dhs/concerns/record/chainable.rb', line 302

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

#ignore(*error_classes) ⇒ Object



251
252
253
254
255
256
257
# File 'lib/dhs/concerns/record/chainable.rb', line 251

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



333
334
335
336
337
# File 'lib/dhs/concerns/record/chainable.rb', line 333

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

#includes(*args) ⇒ Object



280
281
282
283
284
# File 'lib/dhs/concerns/record/chainable.rb', line 280

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

#includes_first_page(*args) ⇒ Object



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

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

#includes_valuesObject

Returns a hash of include conditions



322
323
324
# File 'lib/dhs/concerns/record/chainable.rb', line 322

def includes_values
  chain_includes
end

#limit(argument = nil) ⇒ Object



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

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

#option_values_hashObject

Returns a hash of options



312
313
314
# File 'lib/dhs/concerns/record/chainable.rb', line 312

def option_values_hash
  chain_options
end

#options(hash = nil) ⇒ Object



247
248
249
# File 'lib/dhs/concerns/record/chainable.rb', line 247

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

#order(*args) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/dhs/concerns/record/chainable.rb', line 225

def order(*args)
  order_params = {}
  args.each do |arg|
    if arg.is_a?(Hash)
      arg.each do |key, value|
        order_params[key] = value
      end
    else
      order_params[arg.to_s] = 'asc'
    end
  end
  push(Parameter.new(order: order_params))
end

#page(page) ⇒ Object



259
260
261
# File 'lib/dhs/concerns/record/chainable.rb', line 259

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

#pagination_values_hashObject

Returns a hash of pagination values



317
318
319
# File 'lib/dhs/concerns/record/chainable.rb', line 317

def pagination_values_hash
  chain_pagination
end

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



201
202
203
204
# File 'lib/dhs/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/dhs/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



263
264
265
# File 'lib/dhs/concerns/record/chainable.rb', line 263

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

#references(*args) ⇒ Object



286
287
288
# File 'lib/dhs/concerns/record/chainable.rb', line 286

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

#references_valuesObject

Returns a hash of reference options



327
328
329
# File 'lib/dhs/concerns/record/chainable.rb', line 327

def references_values
  chain_references
end

#rescue(error_class, handler) ⇒ Object



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

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

#save(options = nil) ⇒ Object



176
177
178
179
# File 'lib/dhs/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/dhs/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/dhs/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/dhs/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/dhs/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/dhs/concerns/record/chainable.rb', line 217

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

#where_values_hashObject

Returns a hash of where conditions



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

def where_values_hash
  chain_parameters
end