Class: EnumerateIt::Base
- Inherits:
-
Object
- Object
- EnumerateIt::Base
- Defined in:
- lib/enumerate_it/base.rb
Constant Summary collapse
- @@registered_enumerations =
{}
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 ⇒ Object
- .each_value ⇒ Object
- .enumeration ⇒ Object
- .key_for(value) ⇒ Object
- .keys ⇒ Object
- .length ⇒ Object
- .list ⇒ Object
- .sort_by(sort_mode) ⇒ Object
- .t(value) ⇒ Object
- .to_a ⇒ Object
- .to_json ⇒ Object
- .to_range ⇒ 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.
7 8 9 |
# File 'lib/enumerate_it/base.rb', line 7 def sort_mode @sort_mode end |
Class Method Details
.associate_values(*args) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/enumerate_it/base.rb', line 10 def self.associate_values(*args) values_hash = args.first.is_a?(Hash) ? args.first : args.inject({}) { |h, v| h[v] = v.to_s; h } register_enumeration normalize_enumeration(values_hash) values_hash.each_pair { |value_name, attributes| define_enumeration_constant value_name, attributes[0] } end |
.each_translation ⇒ Object
37 38 39 |
# File 'lib/enumerate_it/base.rb', line 37 def self.each_translation each_value { |value| yield t(value) } end |
.each_value ⇒ Object
45 46 47 |
# File 'lib/enumerate_it/base.rb', line 45 def self.each_value list.each { |value| yield value } end |
.enumeration ⇒ Object
25 26 27 |
# File 'lib/enumerate_it/base.rb', line 25 def self.enumeration @@registered_enumerations[self] end |
.key_for(value) ⇒ Object
76 77 78 |
# File 'lib/enumerate_it/base.rb', line 76 def self.key_for(value) enumeration.map {|e| e[0] if e[1][0] == value }.compact.first end |
.keys ⇒ Object
72 73 74 |
# File 'lib/enumerate_it/base.rb', line 72 def self.keys enumeration.keys end |
.length ⇒ Object
33 34 35 |
# File 'lib/enumerate_it/base.rb', line 33 def self.length list.length end |
.list ⇒ Object
21 22 23 |
# File 'lib/enumerate_it/base.rb', line 21 def self.list enumeration.values.map { |value| value[0] }.sort end |
.sort_by(sort_mode) ⇒ Object
17 18 19 |
# File 'lib/enumerate_it/base.rb', line 17 def self.sort_by(sort_mode) @sort_mode = sort_mode end |
.t(value) ⇒ Object
53 54 55 56 |
# File 'lib/enumerate_it/base.rb', line 53 def self.t(value) target = to_a.detect { |item| item[1] == value } target ? target[0] : value end |
.to_a ⇒ Object
29 30 31 |
# File 'lib/enumerate_it/base.rb', line 29 def self.to_a sorted_map.map { |k, v| [translate(v[1]), v[0]] } end |
.to_json ⇒ Object
49 50 51 |
# File 'lib/enumerate_it/base.rb', line 49 def self.to_json sorted_map.map { |k, v| { :value => v[0], :label => translate(v[1]) } }.to_json end |
.to_range ⇒ Object
80 81 82 |
# File 'lib/enumerate_it/base.rb', line 80 def self.to_range (list.min..list.max) end |
.translations ⇒ Object
41 42 43 |
# File 'lib/enumerate_it/base.rb', line 41 def self.translations list.map { |value| t(value) } end |
.value_for(value) ⇒ Object
62 63 64 65 66 |
# File 'lib/enumerate_it/base.rb', line 62 def self.value_for(value) self.const_get(value.to_sym) rescue NameError nil end |
.value_from_key(key) ⇒ Object
68 69 70 |
# File 'lib/enumerate_it/base.rb', line 68 def self.value_from_key(key) (enumeration[key.to_sym] || []).first end |
.values_for(values) ⇒ Object
58 59 60 |
# File 'lib/enumerate_it/base.rb', line 58 def self.values_for(values) values.map { |v| self.value_for v.to_sym } end |