cosine-active_record_encoding 0.9.1

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 (3) hide show
  1. data/LICENSE +21 -0
  2. data/lib/active_record_encoding.rb +150 -0
  3. metadata +55 -0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2009, Michael H. Buselli
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ * Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ''AS IS'' AND ANY
13
+ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
16
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,150 @@
1
+ #
2
+ # Copyright (c) 2009, Michael H. Buselli
3
+ # See LICENSE for details. All other rights reserved.
4
+ #
5
+ #######
6
+
7
+ #
8
+ # ActiveRecordEncoding — Module to make ActiveRecord aware of Unicode
9
+ # encoding issues.
10
+ #
11
+ # ActiveRecordEncoding keeps two variables on the default encoding to
12
+ # use when accessing the database.
13
+ #
14
+ # ActiveRecordEncoding.external_encoding = 'ISO-8859-1'
15
+ # ActiveRecordEncoding.internal_encoding = 'UTF-8'
16
+ #
17
+ # If the external_encoding is not explicitly set then no conversions
18
+ # will be done. The internal_encoding value defaults to
19
+ # Encoding.default_internal if not explicitly set.
20
+ #
21
+ # The internal_encoding value is the encoding of the Strings that are
22
+ # returned by ActiveRecord from String-based columns. The
23
+ # external_encoding value tells ActiveRecord how the database is
24
+ # encoding the data. A conversion is done if necessary.
25
+ #
26
+ # When data is being saved back to the database, the internal_encoding
27
+ # value is ignored and the encoding of the input is used to determine
28
+ # how to encode the data in the external_encoding.
29
+ #
30
+ # Encodings may also be defined on a table-by-table basis in the model
31
+ # definition. A future version of ActiveRecordEncoding may support
32
+ # setting the encoding on a column-by-column basis, but that is not
33
+ # currently possible.
34
+ #
35
+ module ActiveRecordEncoding
36
+
37
+ class << self
38
+ attr_accessor :external_encoding, :internal_encoding
39
+ end
40
+
41
+ #
42
+ # Set both ActiveRecordEncoding.external_encoding and
43
+ # ActiveRecordEncoding.internal_encoding in a single method.
44
+ #
45
+ # ActiveRecordEncoding.encoding = 'UTF-8'
46
+ #
47
+ def encoding= (new_encoding)
48
+ @internal_encoding = @external_encoding = new_encoding
49
+ end
50
+ module_function :encoding=
51
+ end
52
+
53
+
54
+
55
+ module ActiveRecordEncoding::ActiveRecordExtensionClassMethods
56
+
57
+ #
58
+ # Set the external_encoding value for this model class.
59
+ #
60
+ # class User < ActiveRecord::Base
61
+ # external_encoding 'ISO-8859-1'
62
+ # end
63
+ #
64
+ # When data is retrieved from the database, it will be assumed it is
65
+ # encoded in the given format.
66
+ #
67
+ def external_encoding (new_encoding)
68
+ @active_record_external_encoding = new_encoding
69
+ end
70
+
71
+ #
72
+ # Set the internal_encoding value for this model class.
73
+ #
74
+ # class User < ActiveRecord::Base
75
+ # internal_encoding 'UTF-8'
76
+ # end
77
+ #
78
+ # When String objects are returned to the user as a result of an
79
+ # ActiveRecord database lookup, they will be in the given format.
80
+ #
81
+ def internal_encoding (new_encoding)
82
+ @active_record_internal_encoding = new_encoding
83
+ end
84
+
85
+ #
86
+ # Set both the external_encoding and the internal_encoding values for
87
+ # this model class.
88
+ #
89
+ # class User < ActiveRecord::Base
90
+ # encoding 'UTF-8'
91
+ # end
92
+ #
93
+ # When data is retrived from the database, it will be assumed it is
94
+ # encoded in the given format and returned in the same format.
95
+ #
96
+ def encoding (new_encoding)
97
+ @active_record_external_encoding = new_encoding
98
+ @active_record_internal_encoding = new_encoding
99
+ end
100
+
101
+ def active_record_external_encoding #:nodoc:
102
+ @active_record_external_encoding ||
103
+ ActiveRecordEncoding.external_encoding
104
+ end
105
+
106
+ def active_record_internal_encoding #:nodoc:
107
+ @active_record_internal_encoding ||
108
+ ActiveRecordEncoding.internal_encoding ||
109
+ Encoding.default_internal ||
110
+ Encoding.default_external ||
111
+ 'UTF-8'
112
+ end
113
+ end
114
+
115
+
116
+ class ActiveRecord::Base #:nodoc:
117
+ extend ActiveRecordEncoding::ActiveRecordExtensionClassMethods
118
+
119
+
120
+ def encoding_aware_read_attribute (attr_name)
121
+ value = pre_encoding_aware_read_attribute(attr_name)
122
+
123
+ if value.respond_to? :encoding and value.encoding.to_s.eql?('ASCII-8BIT')
124
+ external_encoding = self.class.active_record_external_encoding
125
+
126
+ if external_encoding = self.class.active_record_external_encoding
127
+ internal_encoding = self.class.active_record_internal_encoding
128
+ value.force_encoding(external_encoding).encode!(internal_encoding)
129
+ end
130
+ end
131
+
132
+ value
133
+ end
134
+
135
+ alias_method :pre_encoding_aware_read_attribute, :read_attribute
136
+ alias_method :read_attribute, :encoding_aware_read_attribute
137
+
138
+
139
+ def encoding_aware_write_attribute (attr_name, value)
140
+ if value.respond_to? :encoding and
141
+ external_encoding = self.class.active_record_external_encoding
142
+ value = value.encode(external_encoding)
143
+ end
144
+
145
+ pre_encoding_aware_write_attribute(attr_name, value)
146
+ end
147
+
148
+ alias_method :pre_encoding_aware_write_attribute, :write_attribute
149
+ alias_method :write_attribute, :encoding_aware_write_attribute
150
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cosine-active_record_encoding
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael H. Buselli
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Library to monkey-patch ActiveRecord and add some Unicode awareness
17
+ email: cosine@cosine.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - LICENSE
26
+ - lib/active_record_encoding.rb
27
+ has_rdoc: true
28
+ homepage: http://cosine.org/
29
+ licenses:
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project: active_record_encoding
50
+ rubygems_version: 1.3.5
51
+ signing_key:
52
+ specification_version: 2
53
+ summary: Library to monkey-patch ActiveRecord and add some Unicode awareness
54
+ test_files: []
55
+