Class: HoboFields::EnumString
- Inherits:
-
String
- Object
- String
- HoboFields::EnumString
show all
- Defined in:
- lib/hobo_fields/enum_string.rb
Defined Under Namespace
Modules: DeclarationHelper
Constant Summary
collapse
- COLUMN_TYPE =
:string
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(value) ⇒ EnumString
Returns a new instance of EnumString.
84
85
86
|
# File 'lib/hobo_fields/enum_string.rb', line 84
def initialize(value)
super(self.class.detranslated_values.nil? ? value: (self.class.detranslated_values[value.to_s] || value))
end
|
Class Attribute Details
.detranslated_values ⇒ Object
Returns the value of attribute detranslated_values.
47
48
49
|
# File 'lib/hobo_fields/enum_string.rb', line 47
def detranslated_values
@detranslated_values
end
|
.translated_values ⇒ Object
Returns the value of attribute translated_values.
45
46
47
|
# File 'lib/hobo_fields/enum_string.rb', line 45
def translated_values
@translated_values
end
|
.values ⇒ Object
Returns the value of attribute values.
43
44
45
|
# File 'lib/hobo_fields/enum_string.rb', line 43
def values
@values
end
|
Class Method Details
.for(*values) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/hobo_fields/enum_string.rb', line 49
def for(*values)
options = values.
values = values.*.to_s
c = Class.new(EnumString) do
values.each do |v|
const_name = v.upcase.gsub(/[^a-z0-9_]/i, '_').gsub(/_+/, '_')
const_name = "V" + const_name if const_name =~ /^[0-9_]/ || const_name.blank?
const_set(const_name, self.new(v)) unless const_defined?(const_name)
method_name = "is_#{v.underscore}?"
define_method(method_name) { self == v } unless self.respond_to?(method_name)
end
end
c.with_values(*values)
c.set_name(options[:name]) if options[:name]
c
end
|
.inspect ⇒ Object
Also known as:
to_s
67
68
69
|
# File 'lib/hobo_fields/enum_string.rb', line 67
def inspect
name.blank? ? "#<EnumString #{(values || []) * ' '}>" : name
end
|
.name ⇒ Object
76
77
78
|
# File 'lib/hobo_fields/enum_string.rb', line 76
def name
@name || super
end
|
.set_name(name) ⇒ Object
72
73
74
|
# File 'lib/hobo_fields/enum_string.rb', line 72
def set_name(name)
@name = name
end
|
.with_values(*values) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/hobo_fields/enum_string.rb', line 20
def with_values(*values)
@values = values.*.to_s
@translated_values = Hash.new do |hash, value|
if name.blank? || value.blank?
hash[value] = value
else
hash[value] = I18n.t("#{name.tableize}.#{value}", :default => value)
@detranslated_values[hash[value]] = value
hash[value]
end
end
@detranslated_values = Hash.new do |hash, value|
if name.blank?
hash[value] = value
else
hash[value] = @values.detect(proc { value } ) {|v|
@translated_values[v]==value
}
end
end
end
|
Instance Method Details
#==(other) ⇒ Object
96
97
98
99
100
101
102
|
# File 'lib/hobo_fields/enum_string.rb', line 96
def ==(other)
if other.is_a?(Symbol)
super(other.to_s)
else
super
end
end
|
#to_html(xmldoctype = true) ⇒ Object
92
93
94
|
# File 'lib/hobo_fields/enum_string.rb', line 92
def to_html(xmldoctype = true)
self.class.translated_values[self]
end
|
#validate ⇒ Object
88
89
90
|
# File 'lib/hobo_fields/enum_string.rb', line 88
def validate
"must be one of #{self.class.values.map{|v| v.blank? ? '\'\'' : v} * ', '}" unless self.in?(self.class.values)
end
|