Class: Enum::EnumValue
- Inherits:
-
BasicObject
- Defined in:
- lib/enum/enum_value.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(enum, name = nil, value) ⇒ EnumValue
Returns a new instance of EnumValue.
10
11
12
|
# File 'lib/enum/enum_value.rb', line 10
def initialize(enum, name = nil, value)
@enum, @name, @value = enum, name, value
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/enum/enum_value.rb', line 79
def method_missing(method, *args, &block)
match = method.to_s.match(/^(.+)\?$/)
enum_name = match[1].to_sym if match
if @enum.names.include?(enum_name)
@name.==(enum_name, *args, &block)
elsif @value.respond_to?(method)
@value.send(method, *args, &block)
else
super
end
end
|
Instance Attribute Details
Returns the value of attribute enum.
8
9
10
|
# File 'lib/enum/enum_value.rb', line 8
def enum
@enum
end
|
Returns the value of attribute name.
8
9
10
|
# File 'lib/enum/enum_value.rb', line 8
def name
@name
end
|
Returns the value of attribute value.
8
9
10
|
# File 'lib/enum/enum_value.rb', line 8
def value
@value
end
|
Instance Method Details
#==(other) ⇒ Object
24
25
26
27
28
|
# File 'lib/enum/enum_value.rb', line 24
def ==(other)
return @value == other if @name.nil?
@name == other or @value == other
end
|
#===(other) ⇒ Object
30
31
32
33
34
|
# File 'lib/enum/enum_value.rb', line 30
def ===(other)
return @value === other if @name.nil?
@name === other or @value === other
end
|
#enum_value? ⇒ Boolean
14
15
16
|
# File 'lib/enum/enum_value.rb', line 14
def enum_value?
true
end
|
18
19
20
21
22
|
# File 'lib/enum/enum_value.rb', line 18
def inspect
return @value.inspect if @name.nil?
"#{@enum.to_s}.#{@name}"
end
|
#t(options = {}) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/enum/enum_value.rb', line 36
def t(options = {})
return @value.t(options) if @name.nil?
if not defined?(::I18n)
if @name.to_s.respond_to?(:titleize)
return @name.to_s.titleize
else
raise NotImplementedError, "I18n and String#titleize are not available"
end
end
scope_without_klass = "enums.#{const_to_translation(@enum.name)}"
if @enum.klass
scope_with_klass = "enums.#{const_to_translation(@enum.klass.name)}.#{const_to_translation(@enum.name)}"
::I18n.t(
@name,
{ scope: scope_with_klass
}.merge(options).merge( default: ::I18n.t(
@name,
{ scope: scope_without_klass,
default: @name.to_s.respond_to?(:titleize) ? @name.to_s.titleize : ::I18n.t(
@name,
{ scope: scope_with_klass
}.merge(options)
)
}.merge(options)
)
)
)
else
::I18n.t(
@name,
{ scope: scope_without_klass,
default: (@name.to_s.titleize if @name.to_s.respond_to?(:titleize))
}.merge(options)
)
end
end
|