Class: Filterameter::ParametersBase
- Inherits:
-
Object
- Object
- Filterameter::ParametersBase
- Includes:
- ActiveModel::Validations, Validators
- Defined in:
- lib/filterameter/parameters_base.rb
Overview
# Parameters
Class Parameters is sub-classed to provide controller-specific validations.
Class Method Summary collapse
- .add_validation(parameter_name, validations) ⇒ Object
- .build_sub_class(declarations) ⇒ Object
- .name ⇒ Object
Instance Method Summary collapse
-
#initialize(attributes) ⇒ ParametersBase
constructor
A new instance of ParametersBase.
Constructor Details
#initialize(attributes) ⇒ ParametersBase
Returns a new instance of ParametersBase.
35 36 37 |
# File 'lib/filterameter/parameters_base.rb', line 35 def initialize(attributes) attributes.each { |k, v| assign_attribute(k, v) } end |
Class Method Details
.add_validation(parameter_name, validations) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/filterameter/parameters_base.rb', line 26 def self.add_validation(parameter_name, validations) attr_accessor parameter_name = { allow_nil: true } validations.each do |validation| validates parameter_name, .merge(validation) end end |
.build_sub_class(declarations) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/filterameter/parameters_base.rb', line 14 def self.build_sub_class(declarations) Class.new(self).tap do |sub_class| declarations.select(&:validations?).each do |declaration| sub_class.add_validation(declaration.parameter_name, declaration.validations) end end end |
.name ⇒ Object
22 23 24 |
# File 'lib/filterameter/parameters_base.rb', line 22 def self.name 'ControllerParameters' end |