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.



114
115
116
117
118
# File 'lib/lhs/concerns/record/chainable.rb', line 114

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)



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

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.



108
109
110
# File 'lib/lhs/concerns/record/chainable.rb', line 108

def _links
  @_links
end

Class Method Details

.unfold(args) ⇒ Object



110
111
112
# File 'lib/lhs/concerns/record/chainable.rb', line 110

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

Instance Method Details

#create(data = {}) ⇒ Object



120
121
122
# File 'lib/lhs/concerns/record/chainable.rb', line 120

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

#create!(data = {}) ⇒ Object



124
125
126
# File 'lib/lhs/concerns/record/chainable.rb', line 124

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

#destroy(options = nil) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/lhs/concerns/record/chainable.rb', line 138

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

#find(*args) ⇒ Object



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

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

#find_by(params = {}) ⇒ Object



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

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

#find_by!(params = {}) ⇒ Object



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

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

#first!Object



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

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

#handle(error_class, handler) ⇒ Object



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

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

#includes(*args) ⇒ Object



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

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

#includes_valuesObject

Returns a hash of include conditions



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

def includes_values
  chain_includes
end

#limit(argument = nil) ⇒ Object



180
181
182
183
# File 'lib/lhs/concerns/record/chainable.rb', line 180

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

#option_values_hashObject

Returns a hash of options



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

def option_values_hash
  chain_options
end

#options(hash = nil) ⇒ Object



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

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

#page(page) ⇒ Object



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

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

#pagination_values_hashObject

Returns a hash of pagination values



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

def pagination_values_hash
  chain_pagination
end

#per(per) ⇒ Object



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

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

#references(*args) ⇒ Object



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

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

#references_valuesObject

Returns a hash of reference options



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

def references_values
  chain_references
end

#save(options = nil) ⇒ Object



133
134
135
136
# File 'lib/lhs/concerns/record/chainable.rb', line 133

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

#save!(options = nil) ⇒ Object



128
129
130
131
# File 'lib/lhs/concerns/record/chainable.rb', line 128

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

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



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

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

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



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

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

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

Returns:

  • (Boolean)


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

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

#where(hash = nil) ⇒ Object



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

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

#where_values_hashObject

Returns a hash of where conditions



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

def where_values_hash
  chain_parameters
end