cosine-active_record_encoding 0.9.2 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/active_record_encoding.rb +26 -3
  2. metadata +1 -1
@@ -167,16 +167,33 @@ module ActiveRecordEncoding::ActiveRecordExtensionClassMethods
167
167
  Encoding.default_external ||
168
168
  'UTF-8'
169
169
  end
170
+
171
+
172
+ # Redefine the attribute read method to do the conversion.
173
+ def encoding_aware_define_read_method (symbol, attr_name, column) #:nodoc:
174
+ pre_encoding_aware_define_read_method(symbol, attr_name, column)
175
+ method_name = "encoding_aware_attr_#{symbol}".to_sym
176
+ old_method_name = "pre_#{method_name}".to_sym
177
+ code = <<-__EOM__
178
+ encoding_aware_attribute_cast!(#{attr_name.inspect}, #{old_method_name})
179
+ __EOM__
180
+ evaluate_attribute_method attr_name, "def #{method_name}; #{code}; end"
181
+ alias_method "pre_#{method_name}".to_sym, symbol
182
+ alias_method symbol, method_name
183
+ end
170
184
  end
171
185
 
172
186
 
173
187
  class ActiveRecord::Base #:nodoc:
174
188
  extend ActiveRecordEncoding::ActiveRecordExtensionClassMethods
175
189
 
176
- # Normal replacement method for read_attribute.
177
- def pure_encoding_aware_read_attribute (attr_name) #:nodoc:
178
- value = pre_encoding_aware_read_attribute(attr_name)
190
+ class << self
191
+ alias_method :pre_encoding_aware_define_read_method, :define_read_method
192
+ alias_method :define_read_method, :encoding_aware_define_read_method
193
+ end
179
194
 
195
+ # Method that casts the Binary data into Unicode, if necessary.
196
+ def encoding_aware_attribute_cast! (attr_name, value) #:nodoc:
180
197
  if value.respond_to? :encoding and
181
198
  value.encoding.to_s.eql?('ASCII-8BIT') and
182
199
  ext_encoding = self.class.active_record_external_encoding(attr_name) \
@@ -187,6 +204,12 @@ class ActiveRecord::Base #:nodoc:
187
204
 
188
205
  value
189
206
  end
207
+
208
+ # Normal replacement method for read_attribute.
209
+ def pure_encoding_aware_read_attribute (attr_name) #:nodoc:
210
+ value = pre_encoding_aware_read_attribute(attr_name)
211
+ encoding_aware_attribute_cast!(attr_name, value)
212
+ end
190
213
  private :pure_encoding_aware_read_attribute
191
214
 
192
215
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cosine-active_record_encoding
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael H. Buselli