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

Instance Method Summary collapse

Constructor Details

#initialize(record_class, link, record = nil) ⇒ Chain

Returns a new instance of Chain.



94
95
96
97
98
# File 'lib/lhs/concerns/record/chainable.rb', line 94

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)



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

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.



92
93
94
# File 'lib/lhs/concerns/record/chainable.rb', line 92

def _links
  @_links
end

Instance Method Details

#create(data = {}) ⇒ Object



100
101
102
# File 'lib/lhs/concerns/record/chainable.rb', line 100

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

#create!(data = {}) ⇒ Object



104
105
106
# File 'lib/lhs/concerns/record/chainable.rb', line 104

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

#destroy(options = nil) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/lhs/concerns/record/chainable.rb', line 118

def destroy(options = nil)
  options ||= {}
  options = options.respond_to?(:to_h) ? options : { id: options }
  if @record
    @record.destroy(chain_options.merge(options))
  else
    @record_class.destroy(options, chain_options)
  end
end

#find(*args) ⇒ Object



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

def find(*args)
  options = chain_options
  options = options.merge(error_handler: chain_error_handler) if chain_error_handler.any?
  @record_class.find(*args.push(options))
end

#find_by(params = {}) ⇒ Object



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

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

#find_by!(params = {}) ⇒ Object



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

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

#handle(error_class, handler) ⇒ Object



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

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

#limit(argument = nil) ⇒ Object



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

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

#option_values_hashObject

Returns a hash of options



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

def option_values_hash
  chain_options
end

#options(hash = nil) ⇒ Object



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

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

#page(page) ⇒ Object



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

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

#pagination_values_hashObject

Returns a hash of pagination values



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

def pagination_values_hash
  chain_pagination
end

#per(per) ⇒ Object



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

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

#save(options = nil) ⇒ Object



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

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

#save!(options = nil) ⇒ Object



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

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

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



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

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

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



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

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

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

Returns:

  • (Boolean)


138
139
140
141
# File 'lib/lhs/concerns/record/chainable.rb', line 138

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

#where(hash = nil) ⇒ Object



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

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

#where_values_hashObject

Returns a hash of where conditions



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

def where_values_hash
  chain_parameters
end