Module: Symbolize

Defined in:
lib/symbolize/railtie.rb,
lib/symbolize.rb

Overview

Rails 3 initialization

Defined Under Namespace

Modules: ClassMethods Classes: Railtie

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
# File 'lib/symbolize.rb', line 2

def self.included base
  base.extend(ClassMethods)
end

Instance Method Details

#read_and_symbolize_attribute(attr_name) ⇒ Object

Return an attribute’s value as a symbol or nil



154
155
156
# File 'lib/symbolize.rb', line 154

def read_and_symbolize_attribute attr_name
  symbolize_attribute self[attr_name]
end

#read_i18n_attribute(attr_name) ⇒ Object

Return an attribute’s i18n



159
160
161
162
# File 'lib/symbolize.rb', line 159

def read_i18n_attribute attr_name
  return nil unless attr = read_attribute(attr_name)
  I18n.translate("activerecord.attributes.#{ActiveSupport::Inflector.underscore(self.class)}.enums.#{attr_name}.#{attr}") #.to_sym rescue nila
end

#symbolize_attribute(attr) ⇒ Object

String becomes symbol, booleans string and nil nil.



145
146
147
148
149
150
151
# File 'lib/symbolize.rb', line 145

def symbolize_attribute attr
  case attr
    when String then attr.empty? ? nil : attr.to_sym
    when Symbol, TrueClass, FalseClass, Numeric then attr
    else nil
  end
end

#write_symbolized_attribute(attr_name, value) ⇒ Object

Write a symbolized value. Watch out for booleans.



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/symbolize.rb', line 165

def write_symbolized_attribute attr_name, value
  val = { "true" => true, "false" => false }[value]
  val = symbolize_attribute(value) if val.nil?

  current_value = self.send(attr_name)
  if current_value == val
    current_value
  else
    self[attr_name] = val
    val
  end
end