Class: ProgneTapera::EnumItem

Inherits:
Object
  • Object
show all
Defined in:
lib/progne_tapera/enum_item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, name, options = {}) ⇒ EnumItem

code = HA (value) name = han options = { localized_name: '汉' } -> constant: HAN

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/progne_tapera/enum_item.rb', line 16

def initialize(code, name, options = {})

  raise ArgumentError.new('The code argument is required.') if code.blank?
  raise ArgumentError.new('The name argument is required.') if name.blank?

  @code    = code
  @name    = name
  @options = options

  @options.each do |key, value|
    EnumItem.class_eval do
      define_method(key.to_sym) do
        @options[key] #value
      end
    end
  end

end

Instance Attribute Details

#code ⇒ Object (readonly)

Returns the value of attribute code.



9
10
11
# File 'lib/progne_tapera/enum_item.rb', line 9

def code
  @code
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/progne_tapera/enum_item.rb', line 9

def name
  @name
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/progne_tapera/enum_item.rb', line 9

def options
  @options
end

Instance Method Details

#<=>(that) ⇒ Object

根据 #code 字段的值,比较大小。



43
44
45
# File 'lib/progne_tapera/enum_item.rb', line 43

def <=>(that)
  self.code<=>that.code
end

#==(that) ⇒ Object

判断两个枚举项的 #code 字段是否相等。



37
38
39
# File 'lib/progne_tapera/enum_item.rb', line 37

def ==(that)
  self.code==that.code
end

#constant_name ⇒ Object



47
48
49
# File 'lib/progne_tapera/enum_item.rb', line 47

def constant_name
  name.to_s.upcase
end