Class: SimpleParams::Params
- Inherits:
-
Object
- Object
- SimpleParams::Params
show all
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Validations, Validations
- Defined in:
- lib/simple_params/params.rb
Constant Summary
collapse
- TYPES =
[
:integer,
:string,
:decimal,
:datetime,
:date,
:time,
:float,
:boolean,
:array,
:hash,
:object
]
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#valid?, #validate!
Constructor Details
#initialize(params = {}, parent = nil) ⇒ Params
Returns a new instance of Params.
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/simple_params/params.rb', line 130
def initialize(params={}, parent = nil)
if self.class.strict_enforcement.nil?
self.class.strict_enforcement = true
end
@parent = parent
@original_params = hash_to_symbolized_hash(params)
define_attributes(@original_params)
@nested_params = nested_hashes.keys
@nested_arrays = nested_arrays.keys
set_accessors(params)
initialize_nested_classes
initialize_nested_array_classes
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
Overriding this method to allow for non-strict enforcement!
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/simple_params/params.rb', line 204
def method_missing(method_name, *arguments, &block)
if strict_enforcement?
raise SimpleParamsError, "parameter #{method_name} is not defined."
else
if @original_params.include?(method_name.to_sym)
value = @original_params[method_name.to_sym]
if value.is_a?(Hash)
define_anonymous_class(method_name, value)
else
Attribute.new(self, method_name).value = value
end
end
end
end
|
Class Attribute Details
.options ⇒ Object
Returns the value of attribute options.
33
34
35
|
# File 'lib/simple_params/params.rb', line 33
def options
@options
end
|
.strict_enforcement ⇒ Object
Returns the value of attribute strict_enforcement.
33
34
35
|
# File 'lib/simple_params/params.rb', line 33
def strict_enforcement
@strict_enforcement
end
|
Class Method Details
.allow_undefined_params ⇒ Object
47
48
49
|
# File 'lib/simple_params/params.rb', line 47
def allow_undefined_params
@strict_enforcement = false
end
|
.api_pie_documentation ⇒ Object
.defined_attributes ⇒ Object
80
81
82
|
# File 'lib/simple_params/params.rb', line 80
def defined_attributes
@define_attributes ||= {}
end
|
.model_name ⇒ Object
35
36
37
|
# File 'lib/simple_params/params.rb', line 35
def model_name
ActiveModel::Name.new(self)
end
|
.nested_array(name, opts = {}, &block) ⇒ Object
69
70
71
72
73
74
|
# File 'lib/simple_params/params.rb', line 69
def nested_array(name, opts={}, &block)
attr_accessor name
nested_array_class = define_nested_class(name, opts, &block)
@nested_arrays ||= {}
@nested_arrays[name.to_sym] = nested_array_class
end
|
.nested_arrays ⇒ Object
76
77
78
|
# File 'lib/simple_params/params.rb', line 76
def nested_arrays
@nested_arrays ||= {}
end
|
.nested_hash(name, opts = {}, &block) ⇒ Object
Also known as:
nested_param, nested
56
57
58
59
60
61
|
# File 'lib/simple_params/params.rb', line 56
def nested_hash(name, opts={}, &block)
attr_accessor name
nested_class = define_nested_class(name, opts, &block)
@nested_hashes ||= {}
@nested_hashes[name.to_sym] = nested_class
end
|
.nested_hashes ⇒ Object
65
66
67
|
# File 'lib/simple_params/params.rb', line 65
def nested_hashes
@nested_hashes ||= {}
end
|
.param(name, opts = {}) ⇒ Object
51
52
53
54
|
# File 'lib/simple_params/params.rb', line 51
def param(name, opts={})
define_attribute(name, opts)
add_validations(name, opts)
end
|
.strict ⇒ Object
43
44
45
|
# File 'lib/simple_params/params.rb', line 43
def strict
@strict_enforcement = true
end
|
Instance Method Details
#attributes ⇒ Object
159
160
161
|
# File 'lib/simple_params/params.rb', line 159
def attributes
(defined_attributes.keys + nested_hashes.keys + nested_arrays.keys).flatten
end
|
#define_attributes(params) ⇒ Object
153
154
155
156
157
|
# File 'lib/simple_params/params.rb', line 153
def define_attributes(params)
self.class.defined_attributes.each_pair do |key, opts|
send("#{key}_attribute=", Attribute.new(self, key, opts))
end
end
|
#errors ⇒ Object
189
190
191
192
193
194
195
196
197
198
199
200
201
|
# File 'lib/simple_params/params.rb', line 189
def errors
nested_errors_hash = {}
@nested_params.each do |param|
nested_errors_hash[param.to_sym] = send(param).errors
end
nested_arrays_hash = {}
@nested_arrays.each do |array|
nested_arrays_hash[array.to_sym] = send(array).map(&:errors)
end
@errors ||= SimpleParams::Errors.new(self, nested_errors_hash, nested_arrays_hash)
end
|
#original_params ⇒ Object
Also known as:
original_hash, raw_params
163
164
165
|
# File 'lib/simple_params/params.rb', line 163
def original_params
@original_params ||= {}
end
|
#respond_to?(method_name, include_private = false) ⇒ Boolean
219
220
221
222
223
224
225
|
# File 'lib/simple_params/params.rb', line 219
def respond_to?(method_name, include_private = false)
if strict_enforcement?
super
else
@original_params.include?(method_name.to_sym) || super
end
end
|
#to_hash ⇒ Object
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/simple_params/params.rb', line 169
def to_hash
hash = {}
attributes.each do |attribute|
raw_attribute = send(attribute)
if raw_attribute.is_a?(SimpleParams::Params)
hash[attribute] = send(attribute).to_hash
elsif raw_attribute.is_a?(Array)
attribute_array = []
raw_attribute.each do |r_attr|
attribute_array << r_attr.to_hash
end
hash[attribute] = attribute_array
else
hash[attribute] = send(attribute)
end
end
hash
end
|