Class: ActiveEnum::Base
- Inherits:
-
Object
- Object
- ActiveEnum::Base
- Defined in:
- lib/active_enum/base.rb
Class Attribute Summary collapse
-
.store ⇒ Object
writeonly
Sets the attribute store.
Class Method Summary collapse
- .[](index) ⇒ Object
- .each(&block) ⇒ Object
-
.get(index, raise_on_not_found: ActiveEnum.raise_on_not_found) ⇒ Object
Access id or name value.
-
.ids ⇒ Object
Array of all enum id values.
- .include?(value) ⇒ Boolean
- .inherited(subclass) ⇒ Object
-
.length ⇒ Object
(also: size)
Return count of values defined.
-
.meta(index, raise_on_not_found: ActiveEnum.raise_on_not_found) ⇒ Object
Access any meta data defined for a given id or name.
-
.names ⇒ Object
Array of all enum name values.
-
.order(order) ⇒ Object
Specify order enum values are returned.
-
.to_grouped_select(group_by, group_transform: ActiveEnum.default_select_group_transform, value_transform: ActiveEnum.default_select_value_transform) ⇒ Object
Return enum values in a nested array suitable to pass to a Rails form grouped select helper.
-
.to_h ⇒ Object
Return a simple hash of key value pairs id => name for each value.
-
.to_select(value_transform: ActiveEnum.default_select_value_transform) ⇒ Object
Return enum values in an array suitable to pass to a Rails form select helper.
-
.value(enum_value) ⇒ Object
Define enum values.
-
.values ⇒ Object
(also: all)
Array of arrays of stored values defined id, name, meta values hash.
Class Attribute Details
.store=(value) ⇒ Object
Sets the attribute store
8 9 10 |
# File 'lib/active_enum/base.rb', line 8 def store=(value) @store = value end |
Class Method Details
.[](index) ⇒ Object
89 90 91 92 93 |
# File 'lib/active_enum/base.rb', line 89 def [](index) row = get_value(index) return if row.nil? index.is_a?(Integer) ? row[1] : row[0] end |
.each(&block) ⇒ Object
42 43 44 |
# File 'lib/active_enum/base.rb', line 42 def each(&block) all.each(&block) end |
.get(index, raise_on_not_found: ActiveEnum.raise_on_not_found) ⇒ Object
Access id or name value. Pass an id number to retrieve the name or a symbol or string to retrieve the matching id.
83 84 85 86 87 |
# File 'lib/active_enum/base.rb', line 83 def get(index, raise_on_not_found: ActiveEnum.raise_on_not_found) row = get_value(index, raise_on_not_found) return if row.nil? index.is_a?(Integer) ? row[1] : row[0] end |
.ids ⇒ Object
Array of all enum id values
47 48 49 |
# File 'lib/active_enum/base.rb', line 47 def ids store.values.map { |v| v[0] } end |
.include?(value) ⇒ Boolean
95 96 97 |
# File 'lib/active_enum/base.rb', line 95 def include?(value) !get_value(value, false).nil? end |
.inherited(subclass) ⇒ Object
10 11 12 |
# File 'lib/active_enum/base.rb', line 10 def inherited(subclass) ActiveEnum.enum_classes << subclass end |
.length ⇒ Object Also known as: size
Return count of values defined
76 77 78 |
# File 'lib/active_enum/base.rb', line 76 def length store.values.length end |
.meta(index, raise_on_not_found: ActiveEnum.raise_on_not_found) ⇒ Object
Access any meta data defined for a given id or name. Returns a hash.
100 101 102 103 |
# File 'lib/active_enum/base.rb', line 100 def (index, raise_on_not_found: ActiveEnum.raise_on_not_found) row = get_value(index, raise_on_not_found) row[2] || {} if row end |
.names ⇒ Object
Array of all enum name values
52 53 54 |
# File 'lib/active_enum/base.rb', line 52 def names store.values.map { |v| v[1] } end |
.order(order) ⇒ Object
Specify order enum values are returned. Allowed values are :asc, :desc or :natural
28 29 30 31 32 33 34 |
# File 'lib/active_enum/base.rb', line 28 def order(order) if order == :as_defined ActiveSupport::Deprecation.warn("You are using the order :as_defined which has been deprecated. Use :natural.") order = :natural end @order = order end |
.to_grouped_select(group_by, group_transform: ActiveEnum.default_select_group_transform, value_transform: ActiveEnum.default_select_value_transform) ⇒ Object
Return enum values in a nested array suitable to pass to a Rails form grouped select helper.
62 63 64 65 66 |
# File 'lib/active_enum/base.rb', line 62 def to_grouped_select(group_by, group_transform: ActiveEnum.default_select_group_transform, value_transform: ActiveEnum.default_select_value_transform) store.values.group_by { |(_id, _name, )| ( || {})[group_by] }.map { |group, collection| [ group_transform.call(group), collection.map { |(id, name, )| [ name.html_safe, id ] } ] } end |
.to_h ⇒ Object
Return a simple hash of key value pairs id => name for each value
69 70 71 72 73 |
# File 'lib/active_enum/base.rb', line 69 def to_h store.values.inject({}) { |hash, row| hash.merge(row[0] => row[1]) } end |
.to_select(value_transform: ActiveEnum.default_select_value_transform) ⇒ Object
Return enum values in an array suitable to pass to a Rails form select helper.
57 58 59 |
# File 'lib/active_enum/base.rb', line 57 def to_select(value_transform: ActiveEnum.default_select_value_transform) store.values.map(&value_transform) end |
.value(enum_value) ⇒ Object
Define enum values.
Examples:
value :id => 1, :name => 'Foo'
value :name => 'Foo' # implicit id, incrementing from 1.
value 1 => 'Foo'
21 22 23 |
# File 'lib/active_enum/base.rb', line 21 def value(enum_value) store.set(*(enum_value)) end |
.values ⇒ Object Also known as: all
Array of arrays of stored values defined id, name, meta values hash
37 38 39 |
# File 'lib/active_enum/base.rb', line 37 def values store.values end |