hiroeorz-custom-active-record 0.1.3 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 1
4
- :patch: 3
4
+ :patch: 7
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{custom-active-record}
5
- s.version = "0.1.3"
5
+ s.version = "0.1.6"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["hiroeorz"]
9
- s.date = %q{2009-06-29}
9
+ s.date = %q{2009-07-03}
10
10
  s.email = %q{hiroe.orz@gmail.com}
11
11
  s.extra_rdoc_files = [
12
12
  "LICENSE",
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
23
23
  "lib/custom_active_record.rb",
24
24
  "lib/custom_active_record/no_id_base.rb",
25
25
  "lib/custom_active_record/plurals_pkeys_base.rb",
26
+ "lib/custom_active_record/plurals_pkeys_sjis_base.rb",
26
27
  "lib/custom_active_record/sjis_base.rb",
27
28
  "spec/custom-active-record_spec.rb",
28
29
  "spec/spec_helper.rb"
@@ -1,40 +1,60 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- require "custom_active_record/sjis_base"
4
-
5
1
  module ActiveRecord
6
- class PluralsPKeysBase < ActiveRecord::SJISBase
2
+ class PluralsPKeysBase < ActiveRecord::Base
7
3
 
8
4
  @@primary_key_list = {}
9
5
 
6
+
10
7
  def self.set_primary_key_list(*keys)
11
- @@primary_key_list[self] = keys.to_mssql_encode
8
+ @@primary_key_list[self] = keys
12
9
  end
13
10
 
14
- def destroy
15
- unless new_record?
16
- where = ""
17
- first = true
11
+ def self.find_from_ids(ids, options)
12
+ if ids.kind_of?(Array) and ids.length == 1
13
+ ids = ids[0]
14
+ end
15
+
16
+ expects_array = ids.first.kind_of?(Array)
17
+
18
+ unless expects_array
19
+ ids = [ids]
20
+ end
21
+
22
+ where = ""
23
+
24
+ ids.each_with_index do |id_element, id_i|
25
+ if id_i.zero?
26
+ where << "("
27
+ else
28
+ where << ") or ("
29
+ end
18
30
 
19
- primary_key_list.each do |key|
20
- value = read_attribute(key)
31
+ primary_key_list.each_with_index do |key, i|
32
+ value = id_element[i]
33
+
34
+ where << " and " unless i.zero?
35
+ where << "#{key} = #{quote_value(value)}"
21
36
 
22
- where << " and " unless first
23
- where << " [#{key}] = #{quote_value(value)}"
24
- first = false
25
37
  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
38
  end
32
39
 
33
- freeze
40
+ where << ")"
41
+
42
+ find_mode = expects_array ? :all : :first
43
+
44
+ self.find(find_mode, :conditions => where,
45
+ :order => primary_key_list.join(","))
46
+ end
47
+
48
+ def destroy
49
+ raise "not supported method"
34
50
  end
35
51
 
36
52
  private
37
53
 
54
+ def self.primary_key_list
55
+ return @@primary_key_list[self]
56
+ end
57
+
38
58
  def primary_key_list
39
59
  return @@primary_key_list[self.class]
40
60
  end
@@ -47,7 +67,7 @@ module ActiveRecord
47
67
  value = read_attribute(key)
48
68
 
49
69
  where << " and " unless first
50
- where << "[#{key}] = #{quote_value(value)}"
70
+ where << "#{key} = #{quote_value(value)}"
51
71
  first = false
52
72
  end
53
73
 
@@ -0,0 +1,62 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "custom_active_record/sjis_base"
4
+
5
+ module ActiveRecord
6
+ class PluralsPKeysSJISBase < 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
@@ -3,3 +3,4 @@ require "active_record"
3
3
  require "custom_active_record/sjis_base.rb"
4
4
  require "custom_active_record/no_id_base.rb"
5
5
  require "custom_active_record/plurals_pkeys_base.rb"
6
+ require "custom_active_record/plurals_pkeys_sjis_base.rb"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiroeorz-custom-active-record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - hiroeorz
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-29 00:00:00 -07:00
12
+ date: 2009-07-03 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -33,6 +33,7 @@ files:
33
33
  - lib/custom_active_record.rb
34
34
  - lib/custom_active_record/no_id_base.rb
35
35
  - lib/custom_active_record/plurals_pkeys_base.rb
36
+ - lib/custom_active_record/plurals_pkeys_sjis_base.rb
36
37
  - lib/custom_active_record/sjis_base.rb
37
38
  - spec/custom-active-record_spec.rb
38
39
  - spec/spec_helper.rb