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.



77
78
79
80
81
# File 'lib/lhs/concerns/record/chainable.rb', line 77

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)



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

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.



75
76
77
# File 'lib/lhs/concerns/record/chainable.rb', line 75

def _links
  @_links
end

Instance Method Details

#create(data = {}) ⇒ Object



83
84
85
# File 'lib/lhs/concerns/record/chainable.rb', line 83

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

#create!(data = {}) ⇒ Object



87
88
89
# File 'lib/lhs/concerns/record/chainable.rb', line 87

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

#destroy(options = nil) ⇒ Object



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

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

#find(args) ⇒ Object



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

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

#find_by(params = {}) ⇒ Object



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

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

#limit(argument = nil) ⇒ Object



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

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

#option_values_hashObject

Returns a hash of options



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

def option_values_hash
  chain_options
end

#options(hash = nil) ⇒ Object



126
127
128
# File 'lib/lhs/concerns/record/chainable.rb', line 126

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

#page(page) ⇒ Object



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

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

#pagination_values_hashObject

Returns a hash of pagination values



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

def pagination_values_hash
  chain_pagination
end

#per(per) ⇒ Object



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

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

#save(options = nil) ⇒ Object



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

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

#save!(options = nil) ⇒ Object



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

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

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



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

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

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



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

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

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

Returns:

  • (Boolean)


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

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

#where(hash = nil) ⇒ Object



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

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

#where_values_hashObject

Returns a hash of where conditions



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

def where_values_hash
  chain_parameters
end