class RailsStuff::Statusable::MappedHelper

Helper to hold

Attributes

indifferent_mapping[R]
inverse_mapping[R]
mapping[R]

Public Class Methods

new(*) click to toggle source
Calls superclass method RailsStuff::Statusable::Helper::new
# 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

Public Instance Methods

map(val) click to toggle source
# File lib/rails_stuff/statusable/mapped_helper.rb, line 26
def map(val)
  map_with(indifferent_mapping, val)
end
mapping_values() click to toggle source
# 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) click to toggle source
# File lib/rails_stuff/statusable/mapped_helper.rb, line 15
def select_options(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) click to toggle source
# File lib/rails_stuff/statusable/mapped_helper.rb, line 30
def unmap(val)
  map_with(inverse_mapping, val)
end

Protected Instance Methods

map_with(map, val) click to toggle source

Maps single value or array with given map.

# File lib/rails_stuff/statusable/mapped_helper.rb, line 37
def map_with(map, val)
  if val.is_a?(Array)
    val.map { |x| map.fetch(x, x) }
  else
    map.fetch(val, val)
  end
end