class EnumerateIt::Base

Attributes

sort_mode[R]

Public Class Methods

associate_values(*args) click to toggle source
# File lib/enumerate_it/base.rb, line 12
def associate_values(*args)
  values = values_hash(args)

  register_enumeration(normalize_enumeration(values))

  values.each_pair do |value_name, attributes|
    define_enumeration_constant value_name, attributes[0]
  end
end
each_translation(&block) click to toggle source
# File lib/enumerate_it/base.rb, line 46
def each_translation(&block)
  each_value { |value| block.call t(value) }
end
each_value(&block) click to toggle source
# File lib/enumerate_it/base.rb, line 54
def each_value(&block)
  list.each(&block)
end
enumeration() click to toggle source
# File lib/enumerate_it/base.rb, line 34
def enumeration
  @registered_enumerations[self]
end
key_for(value) click to toggle source
# File lib/enumerate_it/base.rb, line 83
def key_for(value)
  enumeration.map { |e| e[0] if e[1][0] == value }.compact.first
end
length() click to toggle source
# File lib/enumerate_it/base.rb, line 42
def length
  list.length
end
list() click to toggle source
# File lib/enumerate_it/base.rb, line 26
def list
  sorted_map.map { |_k, v| v.first }
end
sort_by(sort_mode) click to toggle source
# File lib/enumerate_it/base.rb, line 22
def sort_by(sort_mode)
  @sort_mode = sort_mode
end
t(value) click to toggle source
# File lib/enumerate_it/base.rb, line 62
def t(value)
  target = to_a.detect { |item| item[1] == value }
  target ? target[0] : value
end
to_a() click to toggle source
# File lib/enumerate_it/base.rb, line 38
def to_a
  sorted_map.map { |_k, v| [translate(v[1]), v[0]] }
end
to_h() click to toggle source
# File lib/enumerate_it/base.rb, line 30
def to_h
  sorted_map.transform_values(&:first)
end
to_json(options = nil) click to toggle source
# File lib/enumerate_it/base.rb, line 58
def to_json(options = nil)
  sorted_map.map { |_k, v| { value: v[0], label: translate(v[1]) } }.to_json(options)
end
to_range() click to toggle source
# File lib/enumerate_it/base.rb, line 87
def to_range
  (list.min..list.max)
end
translate(value) click to toggle source
# File lib/enumerate_it/base.rb, line 91
def translate(value)
  return value unless value.is_a? Symbol

  default = value.to_s.tr('_', ' ').split.map(&:capitalize).join(' ')
  I18n.t("enumerations.#{name.underscore}.#{value.to_s.underscore}", default: default)
end
translations() click to toggle source
# File lib/enumerate_it/base.rb, line 50
def translations
  list.map { |value| t(value) }
end
value_for(value) click to toggle source
# File lib/enumerate_it/base.rb, line 71
def value_for(value)
  const_get(value.to_sym, false)
rescue NameError
  nil
end
value_from_key(key) click to toggle source
# File lib/enumerate_it/base.rb, line 77
def value_from_key(key)
  return if key.nil?

  (enumeration[key.to_sym] || []).first
end
values_for(values) click to toggle source
# File lib/enumerate_it/base.rb, line 67
def values_for(values)
  values.map { |v| value_for v.to_sym }
end

Private Class Methods

define_enumeration_constant(name, value) click to toggle source
# File lib/enumerate_it/base.rb, line 125
def define_enumeration_constant(name, value)
  const_set name.to_s.tr('-', '_').gsub(/\p{blank}/, '_').upcase, value
end
normalize_enumeration(values_hash) click to toggle source
# File lib/enumerate_it/base.rb, line 114
def normalize_enumeration(values_hash)
  values_hash.each_pair do |key, value|
    values_hash[key] = [value, key] unless value.is_a? Array
  end
end
register_enumeration(values_hash) click to toggle source
# File lib/enumerate_it/base.rb, line 120
def register_enumeration(values_hash)
  @registered_enumerations ||= {}
  @registered_enumerations[self] = values_hash
end
sort_lambda() click to toggle source
# File lib/enumerate_it/base.rb, line 106
def sort_lambda
  {
    value:       ->(_k, v) { v[0] },
    name:        ->(k, _v) { k },
    translation: ->(_k, v) { translate(v[1]) }
  }[sort_mode]
end
sorted_map() click to toggle source
# File lib/enumerate_it/base.rb, line 100
def sorted_map
  return enumeration if sort_mode.nil? || sort_mode == :none

  enumeration.sort_by { |k, v| sort_lambda.call(k, v) }
end
values_hash(args) click to toggle source
# File lib/enumerate_it/base.rb, line 129
def values_hash(args)
  return args.first if args.first.is_a?(Hash)

  args.each_with_object({}) do |value, hash|
    hash[value] = value.to_s
  end
end