Class: RailsStuff::Statusable::MappedHelper
- Defined in:
- lib/rails_stuff/statusable/mapped_helper.rb
Overview
Helper to hold
Instance Attribute Summary collapse
-
#indifferent_mapping ⇒ Object
readonly
Returns the value of attribute indifferent_mapping.
-
#inverse_mapping ⇒ Object
readonly
Returns the value of attribute inverse_mapping.
-
#mapping ⇒ Object
readonly
Returns the value of attribute mapping.
Attributes inherited from Helper
Instance Method Summary collapse
-
#initialize ⇒ MappedHelper
constructor
A new instance of MappedHelper.
- #map(val) ⇒ Object
- #mapping_values ⇒ Object
- #select_options(original: false, only: nil, except: nil) ⇒ Object
- #unmap(val) ⇒ Object
Methods inherited from Helper
#attach, #define_class_method, #define_method, #define_scope, #translate
Constructor Details
#initialize ⇒ MappedHelper
Returns a new instance of MappedHelper.
7 8 9 10 11 12 13 |
# File 'lib/rails_stuff/statusable/mapped_helper.rb', line 7 def initialize(*) super @mapping = @list @indifferent_mapping = mapping.with_indifferent_access @list = mapping.keys.freeze @inverse_mapping = mapping.invert.freeze end |
Instance Attribute Details
#indifferent_mapping ⇒ Object (readonly)
Returns the value of attribute indifferent_mapping.
5 6 7 |
# File 'lib/rails_stuff/statusable/mapped_helper.rb', line 5 def indifferent_mapping @indifferent_mapping end |
#inverse_mapping ⇒ Object (readonly)
Returns the value of attribute inverse_mapping.
5 6 7 |
# File 'lib/rails_stuff/statusable/mapped_helper.rb', line 5 def inverse_mapping @inverse_mapping end |
#mapping ⇒ Object (readonly)
Returns the value of attribute mapping.
5 6 7 |
# File 'lib/rails_stuff/statusable/mapped_helper.rb', line 5 def mapping @mapping end |
Instance Method Details
#map(val) ⇒ Object
26 27 28 |
# File 'lib/rails_stuff/statusable/mapped_helper.rb', line 26 def map(val) map_with(indifferent_mapping, val) end |
#mapping_values ⇒ Object
22 23 24 |
# File 'lib/rails_stuff/statusable/mapped_helper.rb', line 22 def mapping_values @mapping_values ||= mapping.values end |
#select_options(original: false, only: nil, except: nil) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/rails_stuff/statusable/mapped_helper.rb', line 15 def (original: false, only: nil, except: nil) return super(only: only, except: except) unless original only ||= mapping_values only -= except if except only.map { |x| [translate(inverse_mapping.fetch(x)), x] } end |
#unmap(val) ⇒ Object
30 31 32 |
# File 'lib/rails_stuff/statusable/mapped_helper.rb', line 30 def unmap(val) map_with(inverse_mapping, val) end |