Class: HoboFields::Types::EnumString

Inherits:
String
  • Object
show all
Defined in:
lib/hobo_fields/types/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.



79
80
81
# File 'lib/hobo_fields/types/enum_string.rb', line 79

def initialize(value)
  super(self.class.detranslated_values.nil? ? value : self.class.detranslated_values[value.to_s])
end

Class Attribute Details

.detranslated_valuesObject

Returns the value of attribute detranslated_values.



42
43
44
# File 'lib/hobo_fields/types/enum_string.rb', line 42

def detranslated_values
  @detranslated_values
end

.translated_valuesObject

Returns the value of attribute translated_values.



40
41
42
# File 'lib/hobo_fields/types/enum_string.rb', line 40

def translated_values
  @translated_values
end

.valuesObject

Returns the value of attribute values.



38
39
40
# File 'lib/hobo_fields/types/enum_string.rb', line 38

def values
  @values
end

Class Method Details

.for(*values) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hobo_fields/types/enum_string.rb', line 44

def for(*values)
  options = values.extract_options!
  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

.inspectObject Also known as: to_s



62
63
64
# File 'lib/hobo_fields/types/enum_string.rb', line 62

def inspect
  name.blank? ? "#<EnumString #{(values || []) * ' '}>" : name
end

.nameObject



71
72
73
# File 'lib/hobo_fields/types/enum_string.rb', line 71

def name
  @name || super
end

.set_name(name) ⇒ Object



67
68
69
# File 'lib/hobo_fields/types/enum_string.rb', line 67

def set_name(name)
  @name = name
end

.with_values(*values) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hobo_fields/types/enum_string.rb', line 15

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



91
92
93
94
95
96
97
# File 'lib/hobo_fields/types/enum_string.rb', line 91

def ==(other)
  if other.is_a?(Symbol)
    super(other.to_s)
  else
    super
  end
end

#to_html(xmldoctype = true) ⇒ Object



87
88
89
# File 'lib/hobo_fields/types/enum_string.rb', line 87

def to_html(xmldoctype = true)
  self.class.translated_values[self].html_safe
end

#validateObject



83
84
85
# File 'lib/hobo_fields/types/enum_string.rb', line 83

def validate
  "must be one of #{self.class.values.map{|v| v.blank? ? '\'\'' : v} * ', '}" unless self.in?(self.class.values)
end