Class: ActiveEnum::Base

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

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.



8
9
10
# File 'lib/active_enum/base.rb', line 8

def store=(value)
  @store = value
end

Class Method Details

.allObject



36
37
38
# File 'lib/active_enum/base.rb', line 36

def all
  store.values
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.



57
58
59
60
61
62
63
64
65
# File 'lib/active_enum/base.rb', line 57

def get(index)
  if index.is_a?(Fixnum)
    row = store.get_by_id(index)
    row[1] if row
  else
    row = store.get_by_name(index)
    row[0] if row
  end
end

.idsObject

Array of all enum id values



41
42
43
# File 'lib/active_enum/base.rb', line 41

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

.include?(value) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/active_enum/base.rb', line 68

def include?(value)
  !get(value).nil?
end

.inherited(subclass) ⇒ Object



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

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

.meta(index) ⇒ Object

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



73
74
75
76
77
78
79
80
# File 'lib/active_enum/base.rb', line 73

def meta(index)
  row = if index.is_a?(Fixnum)
    store.get_by_id(index)
  else
    store.get_by_name(index)
  end
  row[2] || {} if row
end

.namesObject

Array of all enum name values



46
47
48
# File 'lib/active_enum/base.rb', line 46

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_selectObject

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



51
52
53
# File 'lib/active_enum/base.rb', line 51

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


21
22
23
# File 'lib/active_enum/base.rb', line 21

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