Module: ActiveRecordEncoding::ExtendedClassMethods

Defined in:
lib/active_record_encoding.rb

Overview

ExtendedClassMethods defines class methods for inclusion in models sub-classed from ActiveRecord::Base to do the dirty work. It is only included in models that use ActiveRecordEncoding.

Instance Method Summary collapse

Instance Method Details

#active_record_encodingsObject

:nodoc:



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

def active_record_encodings #:nodoc:
  @active_record_encodings ||= Hash.new { |h, k| h[k] = Hash.new }
end

#active_record_external_encoding(attr_name = nil) ⇒ Object

:nodoc:



164
165
166
167
# File 'lib/active_record_encoding.rb', line 164

def active_record_external_encoding (attr_name = nil) #:nodoc:
  active_record_encodings[attr_name][:ext] ||
      @active_record_external_encoding
end

#active_record_internal_encoding(attr_name = nil) ⇒ Object

:nodoc:



169
170
171
172
173
174
175
176
# File 'lib/active_record_encoding.rb', line 169

def active_record_internal_encoding (attr_name = nil) #:nodoc:
  active_record_encodings[attr_name][:int] ||
      @active_record_internal_encoding ||
      ActiveRecordEncoding.internal_encoding ||
      Encoding.default_internal ||
      Encoding.default_external ||
      'UTF-8'
end

#encoding_aware_define_read_method(symbol, attr_name, column) ⇒ Object

Redefine the attribute read method to do the conversion.



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/active_record_encoding.rb', line 180

def encoding_aware_define_read_method (symbol, attr_name, column) #:nodoc:
  pre_encoding_aware_define_read_method(symbol, attr_name, column)
  return if active_record_external_encoding(attr_name).nil?
  method_name = "encoding_aware_attr_#{symbol}".to_sym
  old_method_name = "pre_#{method_name}".to_sym
  code = "    encoding_aware_attribute_cast!(\#{attr_name.inspect}, \#{old_method_name})\n  __EOM__\n  evaluate_attribute_method attr_name, \"def \#{method_name}; \#{code}; end\"\n  alias_method \"pre_\#{method_name}\".to_sym, symbol\n  alias_method symbol, method_name\nend\n"