Class: SimpleParams::Params
- Inherits:
-
Object
- Object
- SimpleParams::Params
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Validations, ActiveModel::Validations::Callbacks, DateTimeHelpers, HasAttributes, HasTypedParams, HashHelpers, RailsHelpers, StrictParams, Validations
- Defined in:
- lib/simple_params/params.rb
Direct Known Subclasses
Constant Summary
Constants included from HasTypedParams
Class Attribute Summary collapse
-
.options ⇒ Object
Returns the value of attribute options.
Instance Attribute Summary collapse
-
#original_params ⇒ Object
(also: #original_hash, #raw_params)
Returns the value of attribute original_params.
Class Method Summary collapse
- .api_pie_documentation ⇒ Object
- .model_name ⇒ Object
- .nested_array(name, opts = {}, &block) ⇒ Object
- .nested_arrays ⇒ Object
- .nested_classes ⇒ Object
- .nested_hash(name, opts = {}, &block) ⇒ Object (also: nested_param, nested)
- .nested_hashes ⇒ Object
- .param(name, opts = {}) ⇒ Object
Instance Method Summary collapse
- #errors ⇒ Object
-
#initialize(params = {}) ⇒ Params
constructor
A new instance of Params.
- #to_hash ⇒ Object
Methods included from Validations
Constructor Details
#initialize(params = {}) ⇒ Params
Returns a new instance of Params.
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/simple_params/params.rb', line 122 def initialize(params={}) set_strictness @original_params = hash_to_symbolized_hash(params) define_attributes(@original_params) # Nested Classes @nested_classes = nested_classes.keys set_accessors(params) end |
Class Attribute Details
.options ⇒ Object
Returns the value of attribute options.
19 20 21 |
# File 'lib/simple_params/params.rb', line 19 def end |
Instance Attribute Details
#original_params ⇒ Object Also known as: original_hash, raw_params
Returns the value of attribute original_params.
118 119 120 |
# File 'lib/simple_params/params.rb', line 118 def original_params @original_params end |
Class Method Details
.api_pie_documentation ⇒ Object
25 26 27 |
# File 'lib/simple_params/params.rb', line 25 def api_pie_documentation SimpleParams::ApiPieDoc.new(self).build end |
.model_name ⇒ Object
21 22 23 |
# File 'lib/simple_params/params.rb', line 21 def model_name ActiveModel::Name.new(self) end |
.nested_array(name, opts = {}, &block) ⇒ Object
53 54 55 56 |
# File 'lib/simple_params/params.rb', line 53 def nested_array(name, opts={}, &block) klass = NestedParams.define_new_array_class(self, name, opts, &block) add_nested_class(name, klass, opts) end |
.nested_arrays ⇒ Object
42 43 44 |
# File 'lib/simple_params/params.rb', line 42 def nested_arrays nested_classes.select { |key, klass| klass.array? } end |
.nested_classes ⇒ Object
34 35 36 |
# File 'lib/simple_params/params.rb', line 34 def nested_classes @nested_classes ||= {} end |
.nested_hash(name, opts = {}, &block) ⇒ Object Also known as: nested_param, nested
46 47 48 49 |
# File 'lib/simple_params/params.rb', line 46 def nested_hash(name, opts={}, &block) klass = NestedParams.define_new_hash_class(self, name, opts, &block) add_nested_class(name, klass, opts) end |
.nested_hashes ⇒ Object
38 39 40 |
# File 'lib/simple_params/params.rb', line 38 def nested_hashes nested_classes.select { |key, klass| klass.hash? } end |
.param(name, opts = {}) ⇒ Object
29 30 31 32 |
# File 'lib/simple_params/params.rb', line 29 def param(name, opts={}) define_attribute(name, opts) add_validations(name, opts) end |
Instance Method Details
#errors ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'lib/simple_params/params.rb', line 154 def errors nested_class_hash = {} @nested_classes.each do |param| nested_class_hash[param.to_sym] = send(param) end @errors ||= SimpleParams::Errors.new(self, nested_class_hash) end |
#to_hash ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/simple_params/params.rb', line 134 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 |