Class: EnumerateIt::Base
- Inherits:
-
Object
- Object
- EnumerateIt::Base
- Extended by:
- Forwardable
- Defined in:
- lib/enumerate_it/base.rb
Class Attribute Summary collapse
-
.sort_mode ⇒ Object
readonly
Returns the value of attribute sort_mode.
Class Method Summary collapse
- .associate_values(*args) ⇒ Object
- .each_translation(&block) ⇒ Object
- .each_value(&block) ⇒ Object
- .enumeration ⇒ Object
- .key_for(value) ⇒ Object
- .length ⇒ Object
- .list ⇒ Object
- .sort_by(sort_mode) ⇒ Object
- .t(value) ⇒ Object
- .to_a ⇒ Object
- .to_h ⇒ Object
- .to_json(options = nil) ⇒ Object
- .to_range ⇒ Object
- .translate(value) ⇒ Object
- .translations ⇒ Object
- .value_for(value) ⇒ Object
- .value_from_key(key) ⇒ Object
- .values_for(values) ⇒ Object
Class Attribute Details
.sort_mode ⇒ Object (readonly)
Returns the value of attribute sort_mode.
8 9 10 |
# File 'lib/enumerate_it/base.rb', line 8 def sort_mode @sort_mode end |
Class Method Details
.associate_values(*args) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# 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) ⇒ Object
46 47 48 |
# File 'lib/enumerate_it/base.rb', line 46 def each_translation(&block) each_value { |value| block.call t(value) } end |
.each_value(&block) ⇒ Object
54 55 56 |
# File 'lib/enumerate_it/base.rb', line 54 def each_value(&block) list.each(&block) end |
.enumeration ⇒ Object
34 35 36 |
# File 'lib/enumerate_it/base.rb', line 34 def enumeration @registered_enumerations[self] end |
.key_for(value) ⇒ Object
83 84 85 |
# 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 ⇒ Object
42 43 44 |
# File 'lib/enumerate_it/base.rb', line 42 def length list.length end |
.list ⇒ Object
26 27 28 |
# File 'lib/enumerate_it/base.rb', line 26 def list sorted_map.map { |_k, v| v.first } end |
.sort_by(sort_mode) ⇒ Object
22 23 24 |
# File 'lib/enumerate_it/base.rb', line 22 def sort_by(sort_mode) @sort_mode = sort_mode end |
.t(value) ⇒ Object
62 63 64 65 |
# 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 ⇒ Object
38 39 40 |
# File 'lib/enumerate_it/base.rb', line 38 def to_a sorted_map.map { |_k, v| [translate(v[1]), v[0]] } end |
.to_h ⇒ Object
30 31 32 |
# File 'lib/enumerate_it/base.rb', line 30 def to_h sorted_map.transform_values(&:first) end |
.to_json(options = nil) ⇒ Object
58 59 60 |
# File 'lib/enumerate_it/base.rb', line 58 def to_json( = nil) sorted_map.map { |_k, v| { value: v[0], label: translate(v[1]) } }.to_json() end |
.to_range ⇒ Object
87 88 89 |
# File 'lib/enumerate_it/base.rb', line 87 def to_range (list.min..list.max) end |
.translate(value) ⇒ Object
91 92 93 94 95 96 |
# 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 ⇒ Object
50 51 52 |
# File 'lib/enumerate_it/base.rb', line 50 def translations list.map { |value| t(value) } end |
.value_for(value) ⇒ Object
71 72 73 74 75 |
# 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) ⇒ Object
77 78 79 80 81 |
# 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) ⇒ Object
67 68 69 |
# File 'lib/enumerate_it/base.rb', line 67 def values_for(values) values.map { |v| value_for v.to_sym } end |