hiroeorz-custom-active-record 0.1.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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 hiroeorz
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = custom-active-record
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 hiroeorz. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "custom-active-record"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "hiroe.orz@gmail.com"
10
+ gem.homepage = "http://github.com/hiroeorz/custom-active-record"
11
+ gem.authors = ["hiroeorz"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ end
24
+
25
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.pattern = 'spec/**/*_spec.rb'
28
+ spec.rcov = true
29
+ end
30
+
31
+
32
+ task :default => :spec
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ if File.exist?('VERSION.yml')
37
+ config = YAML.load(File.read('VERSION.yml'))
38
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
39
+ else
40
+ version = ""
41
+ end
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "custom-active-record #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 1
@@ -0,0 +1,50 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{custom-active-record}
5
+ s.version = "0.1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["hiroeorz"]
9
+ s.date = %q{2009-06-29}
10
+ s.email = %q{hiroe.orz@gmail.com}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION.yml",
22
+ "custom-active-record.gemspec",
23
+ "lib/custom-active-record.rb",
24
+ "lib/custom-active-record/no-id-base.rb",
25
+ "lib/custom-active-record/plurals-pkeys-base.rb",
26
+ "lib/custom-active-record/sjis-base.rb",
27
+ "spec/custom-active-record_spec.rb",
28
+ "spec/spec_helper.rb"
29
+ ]
30
+ s.has_rdoc = true
31
+ s.homepage = %q{http://github.com/hiroeorz/custom-active-record}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.1}
35
+ s.summary = %q{TODO}
36
+ s.test_files = [
37
+ "spec/spec_helper.rb",
38
+ "spec/custom-active-record_spec.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 2
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ else
47
+ end
48
+ else
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ require "rubygems"
2
+ require "active_record"
3
+ require "custom-active-record/sjis-base.rb"
4
+ require "custom-active-record/no-id-base.rb"
5
+ require "custom-active-record/plurals-pkeys-base.rb"
@@ -0,0 +1,72 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "custom-active-record/sjis-base"
4
+
5
+ module ActiveRecord
6
+ class NOIDBase < ActiveRecord::SJISBase
7
+
8
+ def id
9
+ read_attribute(self.class.primary_key)
10
+ end
11
+
12
+ def id=(value)
13
+ write_attribute(self.class.primary_key, value.to_mssql_encode)
14
+ end
15
+
16
+ def initialize(attr = nil)
17
+ attr_sjis = attr.to_mssql_encode
18
+
19
+ super(attr)
20
+
21
+ unless attr.nil?
22
+ write_attribute(self.class.primary_key,
23
+ attr_sjis[self.class.primary_key])
24
+ end
25
+
26
+ count = self.class.count(:conditions =>
27
+ ["[#{self.class.primary_key.to_display_encode}] = ? ",
28
+ attr_sjis[self.class.primary_key]])
29
+ @new_record = count.zero?
30
+ end
31
+ end
32
+ end
33
+
34
+ class Object
35
+ def to_mssql_encode
36
+ return self
37
+ end
38
+
39
+ def to_display_encode
40
+ return self
41
+ end
42
+ end
43
+
44
+ class Array
45
+ def to_mssql_encode
46
+ return self.collect { |a|
47
+ a.to_mssql_encode
48
+ }
49
+ end
50
+ end
51
+
52
+ class Hash
53
+ def to_mssql_encode
54
+ new_hash = {}
55
+
56
+ self.each do |key, value|
57
+ new_hash[key.to_mssql_encode] = value.to_mssql_encode
58
+ end
59
+
60
+ return new_hash
61
+ end
62
+ end
63
+
64
+ class String
65
+ def to_mssql_encode
66
+ NKF.nkf("-Wsx --cp932", self)
67
+ end
68
+
69
+ def to_display_encode
70
+ NKF.nkf("-Swx --cp932", self).strip
71
+ end
72
+ end
@@ -0,0 +1,62 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "custom-active-record/sjis-base"
4
+
5
+ module ActiveRecord
6
+ class PluralsPKeysBase < ActiveRecord::SJISBase
7
+
8
+ @@primary_key_list = {}
9
+
10
+ def self.set_primary_key_list(*keys)
11
+ @@primary_key_list[self] = keys.to_mssql_encode
12
+ end
13
+
14
+ def destroy
15
+ unless new_record?
16
+ where = ""
17
+ first = true
18
+
19
+ primary_key_list.each do |key|
20
+ value = read_attribute(key)
21
+
22
+ where << " and " unless first
23
+ where << " [#{key}] = #{quote_value(value)}"
24
+ first = false
25
+ end
26
+
27
+ connection.delete <<-end_sql, "#{self.class.name} Destroy"
28
+ DELETE FROM #{self.class.table_name}
29
+ WHERE #{where}
30
+ end_sql
31
+ end
32
+
33
+ freeze
34
+ end
35
+
36
+ private
37
+
38
+ def primary_key_list
39
+ return @@primary_key_list[self.class]
40
+ end
41
+
42
+ def update
43
+ where = ""
44
+ first = true
45
+
46
+ primary_key_list.each do |key|
47
+ value = read_attribute(key)
48
+
49
+ where << " and " unless first
50
+ where << "[#{key}] = #{quote_value(value)}"
51
+ first = false
52
+ end
53
+
54
+ connection.update(
55
+ "UPDATE #{self.class.table_name} " +
56
+ "SET #{quoted_comma_pair_list(connection,
57
+ attributes_with_quotes(false))} " +
58
+ "WHERE #{where}",
59
+ "#{self.class.name} Update")
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,245 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module ActiveRecord
4
+ module AttributeMethods #:nodoc:
5
+ module ClassMethods
6
+ private
7
+ def define_write_method(attr_name)
8
+ evaluate_attribute_method attr_name, "def #{attr_name}=(new_value);write_attribute('#{attr_name}', new_value.to_mssql_encode);end", "#{attr_name}="
9
+ end
10
+
11
+ def define_read_method(symbol, attr_name, column)
12
+ cast_code = column.type_cast_code('v') if column
13
+ access_code = cast_code ? "(v=@attributes['#{attr_name}']) && #{cast_code}" : "@attributes['#{attr_name}'].to_display_encode"
14
+
15
+ unless attr_name.to_s == self.primary_key.to_s
16
+ access_code = access_code.insert(0, "missing_attribute('#{attr_name}', caller) unless @attributes.has_key?('#{attr_name}'); ")
17
+ end
18
+
19
+ if cache_attribute?(attr_name)
20
+ access_code = "@attributes_cache['#{attr_name}'] ||= (#{access_code})"
21
+ end
22
+
23
+ evaluate_attribute_method attr_name, "def #{symbol}; #{access_code}; end"
24
+ end
25
+ end
26
+ end
27
+
28
+ class SJISBase < ActiveRecord::Base
29
+
30
+ # ClassName.exist? search all record if exist return true
31
+ # ClassName.exist?(:name => "hanako") search hanako in field name
32
+ # ClassName.exist?(:name => "hanako", :age => 18)
33
+
34
+ @@picture_dir = nil
35
+
36
+ def self.set_picture_dir(value)
37
+ @@picture_dir = value
38
+ end
39
+
40
+ def self.exist?(conditions = nil)
41
+ if conditions.nil?
42
+ return !self.find(:first).nil?
43
+ end
44
+
45
+ if conditions.is_a?(String) or conditions.is_a?(Array)
46
+ return !self.find(:first, :conditions => conditions).nil?
47
+ end
48
+
49
+ if conditions.is_a?(Hash)
50
+ where = ""
51
+ where_array = []
52
+ first = true
53
+
54
+ conditions.each do |column_name, value|
55
+ where << " and " unless first
56
+
57
+ if value.is_a?(Array)
58
+ q_array = []
59
+
60
+ value.each do |v1|
61
+ q_array.push("?")
62
+ end
63
+
64
+ where << " [#{column_name}] in (#{q_array.join(',')})"
65
+ where_array += value
66
+ else
67
+ where << " [#{column_name}] in (?) "
68
+ where_array.push(value)
69
+ end
70
+
71
+ first = false
72
+ end
73
+
74
+ !self.find(:first, :conditions => [where].concat(where_array)).nil?
75
+ end
76
+ end
77
+
78
+ def self.delete(id)
79
+ delete_all([ "#{connection.quote_column_name(primary_key)} IN (?)", id ],
80
+ :sjis)
81
+ end
82
+
83
+ def self.set_table_name(value = nil, &block)
84
+ super(value.to_mssql_encode, &block)
85
+ end
86
+
87
+ def self.set_primary_key(value = nil, &block)
88
+ define_attr_method :primary_key, value.to_mssql_encode, &block
89
+ end
90
+
91
+ def self.find(*args)
92
+ super(*(args.to_mssql_encode))
93
+ end
94
+
95
+ def attributes=(new_attributes, guard_protected_attributes = true)
96
+ return if new_attributes.nil?
97
+
98
+ new_attributes.to_mssql_encode.each do |k, v|
99
+ write_attribute(k, v)
100
+ end
101
+ end
102
+
103
+ def self.destroy_all(conditions)
104
+ super(conditions.to_mssql_encode)
105
+ end
106
+
107
+ def self.delete_all(conditions, encode = :utf8)
108
+ if encode == :utf8
109
+ super(conditions.to_mssql_encode)
110
+ else
111
+ super(conditions)
112
+ end
113
+ end
114
+
115
+ def self.update_all(conditions)
116
+ super(conditions.to_mssql_encode)
117
+ end
118
+
119
+ def self.calculate(operation, column_name, options = {})
120
+ super(operation, column_name, options.to_mssql_encode)
121
+ end
122
+
123
+ def [](attr_name)
124
+ super(attr_name.to_mssql_encode).to_display_encode
125
+ end
126
+
127
+ def []=(attr_name, value)
128
+ super(attr_name.to_mssql_encode, value.to_mssql_encode)
129
+ end
130
+
131
+ private
132
+
133
+ def str2time(field_name)
134
+ if self[field_name].to_s =~ /\d{8}/
135
+ year = self[field_name][0, 4].to_i
136
+ month = self[field_name][4, 2].to_i
137
+ day = self[field_name][6, 2].to_i
138
+
139
+ return Time.mktime(year, month, day)
140
+ end
141
+
142
+ return NILTIME.new
143
+ end
144
+
145
+ def time2str(time)
146
+ return (" " * 8) if time.nil? or time == ""
147
+
148
+ return time.year.to_s +
149
+ format("%02d", time.month) +
150
+ format("%02d", time.day)
151
+ end
152
+ end
153
+ end
154
+
155
+ class Object
156
+ def to_mssql_encode
157
+ return self
158
+ end
159
+
160
+ def to_display_encode
161
+ return self
162
+ end
163
+ end
164
+
165
+ class Array
166
+ def to_mssql_encode
167
+ return self.collect { |a|
168
+ a.to_mssql_encode
169
+ }
170
+ end
171
+
172
+ def to_display_encode
173
+ return self.collect { |a|
174
+ a.to_display_encode
175
+ }
176
+ end
177
+ end
178
+
179
+ class Hash
180
+ def to_mssql_encode
181
+ new_hash = {}
182
+
183
+ self.each do |key, value|
184
+ new_hash[key.to_mssql_encode] = value.to_mssql_encode
185
+ end
186
+
187
+ return new_hash
188
+ end
189
+
190
+ def to_display_encode
191
+ new_hash = {}
192
+
193
+ self.each do |key, value|
194
+ new_hash[key.to_display_encode] = value.to_display_encode
195
+ end
196
+
197
+ return new_hash
198
+ end
199
+ end
200
+
201
+ class String
202
+ def to_mssql_encode
203
+ NKF.nkf("-Wsx --cp932", self)
204
+ end
205
+
206
+ def to_display_encode
207
+ NKF.nkf("-Swx --cp932", self).strip
208
+ end
209
+ end
210
+
211
+ class NILTIME
212
+ NILTIME_STRING = "-"
213
+
214
+ def initialize(*args)
215
+ end
216
+
217
+ def year
218
+ return NILTIME_STRING * 4
219
+ end
220
+
221
+ def month
222
+ return NILTIME_STRING * 2
223
+ end
224
+
225
+ def day
226
+ return NILTIME_STRING * 2
227
+ end
228
+
229
+ def hour
230
+ return NILTIME_STRING * 2
231
+ end
232
+
233
+ def min
234
+ return NILTIME_STRING * 2
235
+ end
236
+
237
+ def sec
238
+ return NILTIME_STRING * 2
239
+ end
240
+
241
+ def strftime(*args)
242
+ nil
243
+ end
244
+ end
245
+
@@ -0,0 +1,4 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "CustomActiveRecord" do
4
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 'custom-active-record'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hiroeorz-custom-active-record
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - hiroeorz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-29 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: hiroe.orz@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION.yml
32
+ - custom-active-record.gemspec
33
+ - lib/custom-active-record.rb
34
+ - lib/custom-active-record/no-id-base.rb
35
+ - lib/custom-active-record/plurals-pkeys-base.rb
36
+ - lib/custom-active-record/sjis-base.rb
37
+ - spec/custom-active-record_spec.rb
38
+ - spec/spec_helper.rb
39
+ has_rdoc: true
40
+ homepage: http://github.com/hiroeorz/custom-active-record
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.2.0
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: TODO
65
+ test_files:
66
+ - spec/spec_helper.rb
67
+ - spec/custom-active-record_spec.rb