Class: RubyPitaya::Parameters
- Inherits:
-
ActiveSupport::HashWithIndifferentAccess
- Object
- ActiveSupport::HashWithIndifferentAccess
- RubyPitaya::Parameters
- Defined in:
- lib/rubypitaya/core/parameters.rb
Constant Summary collapse
- NEVER_UNPERMITTED_PARAMS =
Never raise an UnpermittedParameters exception because of these params are present. They are added by Rails and it’s of no concern.
%w( controller action )
Instance Attribute Summary collapse
-
#permitted ⇒ Object
(also: #permitted?)
Returns the value of attribute permitted.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #dup ⇒ Object
- #fetch(key, *args) ⇒ Object
-
#initialize(attributes = nil) ⇒ Parameters
constructor
A new instance of Parameters.
- #permit(*filters) ⇒ Object
- #permit! ⇒ Object
- #require(key) ⇒ Object (also: #required)
- #slice(*keys) ⇒ Object
Constructor Details
#initialize(attributes = nil) ⇒ Parameters
Returns a new instance of Parameters.
39 40 41 42 |
# File 'lib/rubypitaya/core/parameters.rb', line 39 def initialize(attributes = nil) super(attributes) @permitted = false end |
Instance Attribute Details
#permitted ⇒ Object Also known as: permitted?
Returns the value of attribute permitted.
30 31 32 |
# File 'lib/rubypitaya/core/parameters.rb', line 30 def permitted @permitted end |
Instance Method Details
#[](key) ⇒ Object
79 80 81 |
# File 'lib/rubypitaya/core/parameters.rb', line 79 def [](key) convert_hashes_to_parameters(key, super) end |
#dup ⇒ Object
95 96 97 98 99 100 |
# File 'lib/rubypitaya/core/parameters.rb', line 95 def dup self.class.new(self).tap do |duplicate| duplicate.default = default duplicate.instance_variable_set :@permitted, @permitted end end |
#fetch(key, *args) ⇒ Object
83 84 85 86 87 |
# File 'lib/rubypitaya/core/parameters.rb', line 83 def fetch(key, *args) convert_hashes_to_parameters(key, super, false) rescue KeyError, IndexError raise ParameterMissing.new(key) end |
#permit(*filters) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rubypitaya/core/parameters.rb', line 62 def permit(*filters) params = self.class.new filters.flatten.each do |filter| case filter when Symbol, String permitted_scalar_filter(params, filter) when Hash then hash_filter(params, filter) end end unpermitted_parameters!(params) if self.class.action_on_unpermitted_parameters params.permit! end |
#permit! ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rubypitaya/core/parameters.rb', line 44 def permit! each_pair do |key, value| value = convert_hashes_to_parameters(key, value) Array.wrap(value).each do |_| _.permit! if _.respond_to? :permit! end end @permitted = true self end |
#require(key) ⇒ Object Also known as: required
56 57 58 |
# File 'lib/rubypitaya/core/parameters.rb', line 56 def require(key) self[key].presence || raise(ParameterMissing.new(key)) end |
#slice(*keys) ⇒ Object
89 90 91 92 93 |
# File 'lib/rubypitaya/core/parameters.rb', line 89 def slice(*keys) self.class.new(super).tap do |new_instance| new_instance.instance_variable_set :@permitted, @permitted end end |