Module: ActiveRecordEncoding::IncludedInstanceMethods

Defined in:
lib/active_record_encoding.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model_class) ⇒ Object

:nodoc:



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/active_record_encoding.rb', line 203

def self.included (model_class) #:nodoc:
  class << model_class
    alias_method :pre_encoding_aware_define_read_method, :define_read_method
    alias_method :define_read_method, :encoding_aware_define_read_method
  end

  model_class.class_eval do
    alias_method :pre_encoding_aware_read_attribute, :read_attribute
    alias_method :read_attribute, :encoding_aware_read_attribute
  end
end

Instance Method Details

#encoding_aware_attribute_cast!(attr_name, value) ⇒ Object

Method that casts the Binary data into Unicode, if necessary.



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/active_record_encoding.rb', line 216

def encoding_aware_attribute_cast! (attr_name, value) #:nodoc:
  if not value.frozen? and
      not value.instance_variable_get(:@active_record_encoded) \
  then
    if value.respond_to? :encoding and
        ext_encoding = self.class.active_record_external_encoding(attr_name) \
    then
      int_encoding = self.class.active_record_internal_encoding(attr_name)
      value.force_encoding(ext_encoding).encode!(int_encoding)
    end

    value.instance_variable_set(:@active_record_encoded, true)
  end

  value
end

#encoding_aware_read_attribute(attr_name) ⇒ Object

:nodoc:



256
257
258
259
260
261
262
263
264
265
# File 'lib/active_record_encoding.rb', line 256

def encoding_aware_read_attribute (attr_name) #:nodoc:
  # We need to behave differently if called from
  # #attributes_with_quotes because that is how Rails knows what value
  # to write out.  Doing it this way is an unfortunate kludge.
  rc = if caller.grep(/`attributes_with_quotes'$/).empty?
    pure_encoding_aware_read_attribute(attr_name)
  else
    encoding_aware_read_attribute_for_write(attr_name)
  end
end