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)



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

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
# File 'lib/lhs/concerns/record/chainable.rb', line 118

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

#find(args) ⇒ Object



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

def find(args)
  @record_class.find(args, chain_options)
end

#find_by(params = {}) ⇒ Object



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

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

#handle(error_class, handler) ⇒ Object



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

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

#limit(argument = nil) ⇒ Object



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

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

#option_values_hashObject

Returns a hash of options



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

def option_values_hash
  chain_options
end

#options(hash = nil) ⇒ Object



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

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

#page(page) ⇒ Object



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

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

#pagination_values_hashObject

Returns a hash of pagination values



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

def pagination_values_hash
  chain_pagination
end

#per(per) ⇒ Object



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

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



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

def update(data = {}, options = nil)
  options ||= {}
  @record.update(data, 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

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

Returns:

  • (Boolean)


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

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

#where(hash = nil) ⇒ Object



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

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

#where_values_hashObject

Returns a hash of where conditions



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

def where_values_hash
  chain_parameters
end