Class: Enum

Inherits:
Object show all
Defined in:
lib/enum.rb

Overview

:nodoc:

Direct Known Subclasses

Cardia::TransactionStatus

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a_code) ⇒ Enum

Returns a new instance of Enum.



22
23
24
# File 'lib/enum.rb', line 22

def initialize(a_code)
  @code = a_code.to_i unless a_code.nil?
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



20
21
22
# File 'lib/enum.rb', line 20

def code
  @code
end

Class Method Details

.define_constructor(name, a_code) ⇒ Object



45
46
47
48
49
# File 'lib/enum.rb', line 45

def self.define_constructor(name, a_code)
  meta_def(name.to_sym) do
    return self.new(a_code)
  end
end

.enumsObject



30
31
32
# File 'lib/enum.rb', line 30

def self.enums
  @enums ||= {}
end

.map(name, a_code, description = nil) ⇒ Object

Adds a new enum code. If description isn’t provided, name is used instead



36
37
38
39
40
41
42
43
# File 'lib/enum.rb', line 36

def self.map(name, a_code, description=nil)
  enum_description = (description || name.to_s)
  self.enums[a_code] = enum_description
  self.define_constructor(name, a_code)
  define_method "#{name}?" do
    return @code == a_code
  end
end

Instance Method Details

#==(an_object) ⇒ Object



26
27
28
# File 'lib/enum.rb', line 26

def ==(an_object)
  self.class == an_object.class and self.code == an_object.code
end

#descriptionObject



55
56
57
# File 'lib/enum.rb', line 55

def description
  self.class.enums[@code]
end

#to_sObject



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

def to_s
  description
end