Class: ActiveEnum::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_enum/base.rb

Constant Summary collapse

DEFAULT_GROUPED_SELECT_TRANSFORM =
proc { |group| group&.html_safe }

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.store=(value) ⇒ Object

Sets the attribute store

Parameters:

  • value

    the value to set the attribute store to.



10
11
12
# File 'lib/active_enum/base.rb', line 10

def store=(value)
  @store = value
end

Class Method Details

.each(&block) ⇒ Object



44
45
46
# File 'lib/active_enum/base.rb', line 44

def each(&block)
  all.each(&block)
end

.get(index) ⇒ Object Also known as: []

Access id or name value. Pass an id number to retrieve the name or a symbol or string to retrieve the matching id.



85
86
87
88
89
# File 'lib/active_enum/base.rb', line 85

def get(index)
  row = get_value(index)
  return if row.nil?
  index.is_a?(Integer) ? row[1] : row[0]
end

.idsObject

Array of all enum id values



49
50
51
# File 'lib/active_enum/base.rb', line 49

def ids
  store.values.map { |v| v[0] }
end

.include?(value) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/active_enum/base.rb', line 92

def include?(value)
  !get_value(value, false).nil?
end

.inherited(subclass) ⇒ Object



12
13
14
# File 'lib/active_enum/base.rb', line 12

def inherited(subclass)
  ActiveEnum.enum_classes << subclass
end

.lengthObject Also known as: size

Return count of values defined



78
79
80
# File 'lib/active_enum/base.rb', line 78

def length
  store.values.length
end

.meta(index) ⇒ Object

Access any meta data defined for a given id or name. Returns a hash.



97
98
99
100
# File 'lib/active_enum/base.rb', line 97

def meta(index)
  row = get_value(index)
  row[2] || {} if row
end

.namesObject

Array of all enum name values



54
55
56
# File 'lib/active_enum/base.rb', line 54

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



30
31
32
33
34
35
36
# File 'lib/active_enum/base.rb', line 30

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: DEFAULT_GROUPED_SELECT_TRANSFORM) ⇒ Object

Return enum values in a nested array suitable to pass to a Rails form grouped select helper.



64
65
66
67
68
# File 'lib/active_enum/base.rb', line 64

def to_grouped_select(group_by, group_transform: DEFAULT_GROUPED_SELECT_TRANSFORM)
  store.values.group_by { |(_id, _name, meta)| (meta || {})[group_by] }.map { |group, collection|
    [ group_transform.call(group), collection.map { |(id, name, _meta)| [ name.html_safe, id ] } ]
  }
end

.to_hObject

Return a simple hash of key value pairs id => name for each value



71
72
73
74
75
# File 'lib/active_enum/base.rb', line 71

def to_h
  store.values.inject({}) { |hash, row|
    hash.merge(row[0] => row[1])
  }
end

.to_selectObject

Return enum values in an array suitable to pass to a Rails form select helper.



59
60
61
# File 'lib/active_enum/base.rb', line 59

def to_select
  store.values.map { |v| [v[1].html_safe, v[0]] }
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'


23
24
25
# File 'lib/active_enum/base.rb', line 23

def value(enum_value)
  store.set(*id_and_name_and_meta(enum_value))
end

.valuesObject Also known as: all

Array of arrays of stored values defined id, name, meta values hash



39
40
41
# File 'lib/active_enum/base.rb', line 39

def values
  store.values
end