Class: RailsAttrEnum::EnumAccumulator

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_attr_enum/enum_accumulator.rb

Overview

Class to build up enum values

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnumAccumulator

Returns a new instance of EnumAccumulator.



9
10
11
12
13
# File 'lib/rails_attr_enum/enum_accumulator.rb', line 9

def initialize
  @entries = []
  @validation_rules = {}
  @values = SortedSet.new
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



7
8
9
# File 'lib/rails_attr_enum/enum_accumulator.rb', line 7

def entries
  @entries
end

#validation_rulesObject (readonly)

Returns the value of attribute validation_rules.



7
8
9
# File 'lib/rails_attr_enum/enum_accumulator.rb', line 7

def validation_rules
  @validation_rules
end

Instance Method Details

#add(key) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rails_attr_enum/enum_accumulator.rb', line 15

def add(key)
  if key.is_a?(Hash)
    key, value = key.first

    case value
    when String
      label = value
      value = next_free_value
    when Hash
      label = value[:label] || key.to_s.titleize
      value = value[:value] || next_free_value
    when Numeric
      label = key.to_s.titleize
    end
  else
    value = next_free_value
    label = key.to_s.titleize
  end

  const_name = key.to_s.upcase

  add_entry(const_name, key, value, label)
end

#validates(rules = {}) ⇒ Object



39
40
41
# File 'lib/rails_attr_enum/enum_accumulator.rb', line 39

def validates(rules = {})
  @validation_rules = rules
end