Class: Enumeration::Base

Inherits:
Struct
  • Object
show all
Defined in:
lib/enumerations.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/enumerations.rb', line 121

def method_missing(method_name, *args)
  symbol = method_name.to_s.gsub(/[?]$/, '')
  if self.class.find(symbol) && method_name.to_s.last=="?"
    self.symbol == symbol.to_sym
  else
    super(method_name, args)
  end
end

Instance Attribute Details

#idObject

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



50
51
52
# File 'lib/enumerations.rb', line 50

def id
  @id
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



50
51
52
# File 'lib/enumerations.rb', line 50

def name
  @name
end

#symbolObject

Returns the value of attribute symbol

Returns:

  • (Object)

    the current value of symbol



50
51
52
# File 'lib/enumerations.rb', line 50

def symbol
  @symbol
end

Class Method Details

.find(id) ⇒ Object

lookup a specific enum



85
86
87
88
89
90
91
# File 'lib/enumerations.rb', line 85

def self.find(id)
  if id.is_a?(Fixnum)
    self.id_index[id]
  elsif id.is_a?(Symbol) || id.is_a?(String)
    self.symbol_index[id.to_sym]
  end
end

.values(values) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/enumerations.rb', line 59

def self.values(values)
  # all values
  self.all = []
  # for id based lookup
  self.id_index = {}
  # for symbol based lookup
  self.symbol_index = {}
  
  values.each_pair do |symbol, attributes|
    attributes[:name] ||= symbol.to_s.humanize
    object = self.new(attributes[:id], attributes[:name], symbol)

    self.singleton_class.send(:define_method, symbol) do
      object
    end
    
    raise "Duplicate id #{attributes[:id]}" if self.id_index[attributes[:id]]
    raise "Duplicate symbol #{symbol}" if self.symbol_index[symbol]        
    
    self.id_index[attributes[:id]] = object
    self.symbol_index[symbol] = object
    self.all << object
  end
end

Instance Method Details

#==(object) ⇒ Object

FIXME for some reason this doesn’t work for case..when expressions



94
95
96
97
98
99
100
101
102
# File 'lib/enumerations.rb', line 94

def ==(object)  
  if object.is_a?(Fixnum)
    object == self.id
  elsif object.is_a?(Symbol)
    object == self.symbol
  elsif object.is_a?(self.class)
    object.id == self.id
  end
end

#singleton_classObject



53
54
55
56
57
# File 'lib/enumerations.rb', line 53

def singleton_class
  class << self
    self
  end
end

#to_iObject



109
110
111
# File 'lib/enumerations.rb', line 109

def to_i
  id
end

#to_paramObject



117
118
119
# File 'lib/enumerations.rb', line 117

def to_param
  id  
end

#to_sObject

TODO alias method??



105
106
107
# File 'lib/enumerations.rb', line 105

def to_s
  name
end

#to_symObject



113
114
115
# File 'lib/enumerations.rb', line 113

def to_sym
  symbol 
end