Class: Reform::Form::ActiveModel::ModelValidations::Mapping
- Inherits:
-
Object
- Object
- Reform::Form::ActiveModel::ModelValidations::Mapping
- Defined in:
- lib/reform/form/active_model/model_validations.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#add(from, to) ⇒ Object
from is a symbol attribute to is an 1 or 2 element array, depending on whether the attribute is ‘namespaced’, as it is with composite forms.
- #forward(attr) ⇒ Object
- #forward_image(attrs) ⇒ Object
-
#initialize ⇒ Mapping
constructor
A new instance of Mapping.
- #inverse(attr) ⇒ Object
- #inverse_image(attrs) ⇒ Object
Constructor Details
#initialize ⇒ Mapping
Returns a new instance of Mapping.
73 74 75 76 |
# File 'lib/reform/form/active_model/model_validations.rb', line 73 def initialize @forward_map = {} @inverse_map = {} end |
Class Method Details
.from_representable_attrs(attrs) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/reform/form/active_model/model_validations.rb', line 63 def self.from_representable_attrs(attrs) new.tap do |mapping| attrs.each do |dfn| from = dfn[:name].to_sym to = [dfn[:on], (dfn[:private_name] || dfn[:name])].compact.map(&:to_sym) mapping.add(from, to) end end end |
Instance Method Details
#add(from, to) ⇒ Object
from is a symbol attribute to is an 1 or 2 element array, depending on whether the attribute is ‘namespaced’, as it is with composite forms. eg, add(:phone_number, [:person, :phone])
81 82 83 84 85 |
# File 'lib/reform/form/active_model/model_validations.rb', line 81 def add(from, to) raise 'Mapping is not one-to-one' if @forward_map.has_key?(from) || @inverse_map.has_key?(to) @forward_map[from] = to @inverse_map[to] = from end |
#forward(attr) ⇒ Object
91 92 93 |
# File 'lib/reform/form/active_model/model_validations.rb', line 91 def forward(attr) @forward_map[attr] end |
#forward_image(attrs) ⇒ Object
87 88 89 |
# File 'lib/reform/form/active_model/model_validations.rb', line 87 def forward_image(attrs) @forward_map.values_at(*attrs).compact end |
#inverse(attr) ⇒ Object
99 100 101 |
# File 'lib/reform/form/active_model/model_validations.rb', line 99 def inverse(attr) @inverse_map[attr] end |
#inverse_image(attrs) ⇒ Object
95 96 97 |
# File 'lib/reform/form/active_model/model_validations.rb', line 95 def inverse_image(attrs) @inverse_map.values_at(*attrs).compact end |