Class: Paramoid::Object
- Inherits:
-
Object
- Object
- Paramoid::Object
- Defined in:
- lib/paramoid/object.rb
Instance Attribute Summary collapse
-
#alias_name ⇒ Symbol
readonly
The parameter alias.
-
#default ⇒ Object
readonly
The parameter default value.
-
#name ⇒ Symbol
readonly
The parameter name.
-
#nested ⇒ Object
readonly
The parameter nested.
-
#required ⇒ TrueClass, FalseClass
readonly
Required.
-
#transformer ⇒ Lambda, NilClass
readonly
The transformer.
Instance Method Summary collapse
- #<<(value) ⇒ Object
- #ensure_required_params!(params) ⇒ Object
-
#initialize(name, alias_name, nested: nil, transformer: nil, default: nil, required: false) ⇒ Object
constructor
A new instance of Object.
- #nested? ⇒ Boolean
- #output_key ⇒ Object
- #to_defaults ⇒ Object
- #to_params ⇒ Object
- #to_required_params ⇒ Object
- #transform_params!(params) ⇒ Object
Constructor Details
#initialize(name, alias_name, nested: nil, transformer: nil, default: nil, required: false) ⇒ Object
Returns a new instance of Object.
22 23 24 25 26 27 28 29 |
# File 'lib/paramoid/object.rb', line 22 def initialize(name, alias_name, nested: nil, transformer: nil, default: nil, required: false) @name = name @alias = alias_name @nested = nested @default = default @transformer = transformer @required = required end |
Instance Attribute Details
#alias_name ⇒ Symbol (readonly)
Returns the parameter alias.
6 7 8 |
# File 'lib/paramoid/object.rb', line 6 def alias_name @alias_name end |
#default ⇒ Object (readonly)
Returns the parameter default value.
10 11 12 |
# File 'lib/paramoid/object.rb', line 10 def default @default end |
#name ⇒ Symbol (readonly)
Returns the parameter name.
4 5 6 |
# File 'lib/paramoid/object.rb', line 4 def name @name end |
#nested ⇒ Object (readonly)
Returns the parameter nested.
8 9 10 |
# File 'lib/paramoid/object.rb', line 8 def nested @nested end |
#required ⇒ TrueClass, FalseClass (readonly)
Returns required.
14 15 16 |
# File 'lib/paramoid/object.rb', line 14 def required @required end |
#transformer ⇒ Lambda, NilClass (readonly)
Returns the transformer.
12 13 14 |
# File 'lib/paramoid/object.rb', line 12 def transformer @transformer end |
Instance Method Details
#<<(value) ⇒ Object
86 87 88 |
# File 'lib/paramoid/object.rb', line 86 def <<(value) @nested << value end |
#ensure_required_params!(params) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/paramoid/object.rb', line 62 def ensure_required_params!(params) if @required raise ActionController::ParameterMissing, output_key unless params&.key?(output_key) raise ActionController::ParameterMissing, output_key if params[output_key].nil? end @nested.ensure_required_params!(params[output_key]) if nested? params end |
#nested? ⇒ Boolean
90 91 92 |
# File 'lib/paramoid/object.rb', line 90 def nested? !@nested.nil? end |
#output_key ⇒ Object
82 83 84 |
# File 'lib/paramoid/object.rb', line 82 def output_key @output_key ||= (@alias || @name).to_s end |
#to_defaults ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/paramoid/object.rb', line 73 def to_defaults if nested? nested_defaults = @nested.to_defaults (nested_defaults.present? ? { output_key => @nested.to_defaults } : {}).with_indifferent_access else (@default ? { output_key => @default } : {}).with_indifferent_access end end |
#to_params ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/paramoid/object.rb', line 46 def to_params if nested? { @name.to_sym => @nested.to_params } else @name end end |
#to_required_params ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/paramoid/object.rb', line 54 def to_required_params if nested? @nested.to_required_params else @required ? [@name] : [] end end |
#transform_params!(params) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/paramoid/object.rb', line 32 def transform_params!(params) return if @alias == @name if params.is_a?(Array) params.each { |param| transform_params!(param) } return end return unless params.key?(@name) @nested.transform_params!(params[@name]) if nested? params[@alias] = params.delete(@name) unless @alias == @name end |