Class: SimpleParams::Params
- Inherits:
-
Object
- Object
- SimpleParams::Params
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Validations, ActiveModel::Validations::Callbacks, HasAttributes, HasTypedParams, 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
- .add_validations(name, opts = {}) ⇒ Object
- .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
- #all_nested_classes ⇒ Object
- #define_attributes(params) ⇒ Object
- #errors ⇒ Object
-
#initialize(params = {}) ⇒ Params
constructor
A new instance of Params.
- #nested_class_attributes ⇒ Object
- #nested_class_hash ⇒ Object
- #to_hash ⇒ Object
Methods included from Validations
Constructor Details
#initialize(params = {}) ⇒ Params
111 112 113 114 115 116 117 |
# File 'lib/simple_params/params.rb', line 111 def initialize(params={}) set_strictness params = InitializationHash.new(params) @original_params = params.original_params define_attributes(@original_params) set_accessors(params) end |
Class Attribute Details
.options ⇒ Object
Returns the value of attribute options.
17 18 19 |
# File 'lib/simple_params/params.rb', line 17 def end |
Instance Attribute Details
#original_params ⇒ Object Also known as: original_hash, raw_params
Returns the value of attribute original_params.
107 108 109 |
# File 'lib/simple_params/params.rb', line 107 def original_params @original_params end |
Class Method Details
.add_validations(name, opts = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/simple_params/params.rb', line 32 def add_validations(name, opts = {}) conditional_validation = opts.fetch(:validations, {}).fetch(:if, nil) if conditional_validation opts[:optional] = false end validations = ValidationBuilder.new(opts).build validates name, validations unless validations.empty? end |
.api_pie_documentation ⇒ Object
23 24 25 |
# File 'lib/simple_params/params.rb', line 23 def api_pie_documentation SimpleParams::ApiPieDoc.new(self).build end |
.model_name ⇒ Object
19 20 21 |
# File 'lib/simple_params/params.rb', line 19 def model_name ActiveModel::Name.new(self) end |
.nested_array(name, opts = {}, &block) ⇒ Object
63 64 65 66 |
# File 'lib/simple_params/params.rb', line 63 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
52 53 54 |
# File 'lib/simple_params/params.rb', line 52 def nested_arrays nested_classes.select { |key, klass| klass.array? } end |
.nested_classes ⇒ Object
44 45 46 |
# File 'lib/simple_params/params.rb', line 44 def nested_classes @nested_classes ||= {} end |
.nested_hash(name, opts = {}, &block) ⇒ Object Also known as: nested_param, nested
56 57 58 59 |
# File 'lib/simple_params/params.rb', line 56 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
48 49 50 |
# File 'lib/simple_params/params.rb', line 48 def nested_hashes nested_classes.select { |key, klass| klass.hash? } end |
.param(name, opts = {}) ⇒ Object
27 28 29 30 |
# File 'lib/simple_params/params.rb', line 27 def param(name, opts={}) define_attribute(name, opts) add_validations(name, opts) end |
Instance Method Details
#all_nested_classes ⇒ Object
137 138 139 |
# File 'lib/simple_params/params.rb', line 137 def all_nested_classes nested_class_list.class_instances end |
#define_attributes(params) ⇒ Object
119 120 121 122 123 |
# File 'lib/simple_params/params.rb', line 119 def define_attributes(params) self.class.defined_attributes.each_pair do |key, opts| self.send("#{key}_attribute=", Attribute.new(self, key, opts)) end end |
#errors ⇒ Object
141 142 143 |
# File 'lib/simple_params/params.rb', line 141 def errors @errors ||= SimpleParams::Errors.new(self, nested_class_hash) end |
#nested_class_attributes ⇒ Object
129 130 131 |
# File 'lib/simple_params/params.rb', line 129 def nested_class_attributes nested_classes.keys end |
#nested_class_hash ⇒ Object
133 134 135 |
# File 'lib/simple_params/params.rb', line 133 def nested_class_hash nested_class_list.to_hash end |
#to_hash ⇒ Object
125 126 127 |
# File 'lib/simple_params/params.rb', line 125 def to_hash HashBuilder.new(self).build end |