activegroonga 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. data/AUTHORS +1 -0
  2. data/NEWS.ja.rdoc +5 -0
  3. data/NEWS.rdoc +5 -0
  4. data/README.ja.rdoc +49 -0
  5. data/README.rdoc +49 -0
  6. data/Rakefile +175 -0
  7. data/lib/active_groonga.rb +75 -0
  8. data/lib/active_groonga/aggregations.rb +30 -0
  9. data/lib/active_groonga/associations.rb +93 -0
  10. data/lib/active_groonga/associations/belongs_to_association.rb +25 -0
  11. data/lib/active_groonga/attribute_methods.rb +36 -0
  12. data/lib/active_groonga/base.rb +1579 -0
  13. data/lib/active_groonga/column.rb +107 -0
  14. data/lib/active_groonga/dirty.rb +30 -0
  15. data/lib/active_groonga/fixtures.rb +92 -0
  16. data/lib/active_groonga/migration.rb +150 -0
  17. data/lib/active_groonga/rails_support.rb +31 -0
  18. data/lib/active_groonga/reflection.rb +30 -0
  19. data/lib/active_groonga/schema.rb +314 -0
  20. data/lib/active_groonga/schema_dumper.rb +147 -0
  21. data/lib/active_groonga/tasks.rb +16 -0
  22. data/lib/active_groonga/tasks/groonga.rake +162 -0
  23. data/lib/active_groonga/test_case.rb +21 -0
  24. data/lib/active_groonga/test_help.rb +21 -0
  25. data/lib/active_groonga/timestamp.rb +30 -0
  26. data/lib/active_groonga/validations.rb +26 -0
  27. data/lib/active_groonga/version.rb +24 -0
  28. data/license/LGPL +504 -0
  29. data/rails/README +28 -0
  30. data/rails/init.rb +70 -0
  31. data/rails_generators/model_groonga/USAGE +28 -0
  32. data/rails_generators/model_groonga/model_groonga_generator.rb +45 -0
  33. data/rails_generators/model_groonga/templates/fixtures.yml +17 -0
  34. data/rails_generators/model_groonga/templates/migration.rb +16 -0
  35. data/rails_generators/model_groonga/templates/model.rb +2 -0
  36. data/rails_generators/model_groonga/templates/unit_test.rb +8 -0
  37. data/test-unit/Rakefile +35 -0
  38. data/test-unit/TODO +5 -0
  39. data/test-unit/bin/testrb +5 -0
  40. data/test-unit/html/classic.html +15 -0
  41. data/test-unit/html/index.html +25 -0
  42. data/test-unit/html/index.html.ja +27 -0
  43. data/test-unit/lib/test/unit.rb +342 -0
  44. data/test-unit/lib/test/unit/assertionfailederror.rb +14 -0
  45. data/test-unit/lib/test/unit/assertions.rb +1149 -0
  46. data/test-unit/lib/test/unit/attribute.rb +125 -0
  47. data/test-unit/lib/test/unit/autorunner.rb +306 -0
  48. data/test-unit/lib/test/unit/collector.rb +43 -0
  49. data/test-unit/lib/test/unit/collector/descendant.rb +23 -0
  50. data/test-unit/lib/test/unit/collector/dir.rb +108 -0
  51. data/test-unit/lib/test/unit/collector/load.rb +135 -0
  52. data/test-unit/lib/test/unit/collector/objectspace.rb +34 -0
  53. data/test-unit/lib/test/unit/color-scheme.rb +86 -0
  54. data/test-unit/lib/test/unit/color.rb +96 -0
  55. data/test-unit/lib/test/unit/diff.rb +538 -0
  56. data/test-unit/lib/test/unit/error.rb +124 -0
  57. data/test-unit/lib/test/unit/exceptionhandler.rb +39 -0
  58. data/test-unit/lib/test/unit/failure.rb +110 -0
  59. data/test-unit/lib/test/unit/fixture.rb +176 -0
  60. data/test-unit/lib/test/unit/notification.rb +125 -0
  61. data/test-unit/lib/test/unit/omission.rb +143 -0
  62. data/test-unit/lib/test/unit/pending.rb +146 -0
  63. data/test-unit/lib/test/unit/priority.rb +161 -0
  64. data/test-unit/lib/test/unit/runner/console.rb +52 -0
  65. data/test-unit/lib/test/unit/runner/emacs.rb +8 -0
  66. data/test-unit/lib/test/unit/testcase.rb +360 -0
  67. data/test-unit/lib/test/unit/testresult.rb +89 -0
  68. data/test-unit/lib/test/unit/testsuite.rb +110 -0
  69. data/test-unit/lib/test/unit/ui/console/outputlevel.rb +14 -0
  70. data/test-unit/lib/test/unit/ui/console/testrunner.rb +220 -0
  71. data/test-unit/lib/test/unit/ui/emacs/testrunner.rb +49 -0
  72. data/test-unit/lib/test/unit/ui/testrunner.rb +20 -0
  73. data/test-unit/lib/test/unit/ui/testrunnermediator.rb +77 -0
  74. data/test-unit/lib/test/unit/ui/testrunnerutilities.rb +41 -0
  75. data/test-unit/lib/test/unit/util/backtracefilter.rb +41 -0
  76. data/test-unit/lib/test/unit/util/method-owner-finder.rb +28 -0
  77. data/test-unit/lib/test/unit/util/observable.rb +90 -0
  78. data/test-unit/lib/test/unit/util/procwrapper.rb +48 -0
  79. data/test-unit/lib/test/unit/version.rb +7 -0
  80. data/test-unit/sample/adder.rb +13 -0
  81. data/test-unit/sample/subtracter.rb +12 -0
  82. data/test-unit/sample/tc_adder.rb +18 -0
  83. data/test-unit/sample/tc_subtracter.rb +18 -0
  84. data/test-unit/sample/test_user.rb +22 -0
  85. data/test-unit/sample/ts_examples.rb +7 -0
  86. data/test-unit/test/collector/test-descendant.rb +135 -0
  87. data/test-unit/test/collector/test-load.rb +333 -0
  88. data/test-unit/test/collector/test_dir.rb +406 -0
  89. data/test-unit/test/collector/test_objectspace.rb +98 -0
  90. data/test-unit/test/run-test.rb +13 -0
  91. data/test-unit/test/test-attribute.rb +86 -0
  92. data/test-unit/test/test-color-scheme.rb +56 -0
  93. data/test-unit/test/test-color.rb +47 -0
  94. data/test-unit/test/test-diff.rb +477 -0
  95. data/test-unit/test/test-emacs-runner.rb +60 -0
  96. data/test-unit/test/test-fixture.rb +287 -0
  97. data/test-unit/test/test-notification.rb +33 -0
  98. data/test-unit/test/test-omission.rb +81 -0
  99. data/test-unit/test/test-pending.rb +70 -0
  100. data/test-unit/test/test-priority.rb +119 -0
  101. data/test-unit/test/test_assertions.rb +1082 -0
  102. data/test-unit/test/test_error.rb +26 -0
  103. data/test-unit/test/test_failure.rb +33 -0
  104. data/test-unit/test/test_testcase.rb +478 -0
  105. data/test-unit/test/test_testresult.rb +113 -0
  106. data/test-unit/test/test_testsuite.rb +129 -0
  107. data/test-unit/test/testunit-test-util.rb +14 -0
  108. data/test-unit/test/ui/test_testrunmediator.rb +20 -0
  109. data/test-unit/test/util/test-method-owner-finder.rb +38 -0
  110. data/test-unit/test/util/test_backtracefilter.rb +41 -0
  111. data/test-unit/test/util/test_observable.rb +102 -0
  112. data/test-unit/test/util/test_procwrapper.rb +36 -0
  113. data/test/active-groonga-test-utils.rb +234 -0
  114. data/test/fixtures/bookmark.rb +2 -0
  115. data/test/fixtures/task.rb +2 -0
  116. data/test/fixtures/user.rb +2 -0
  117. data/test/run-test.rb +51 -0
  118. data/test/test-associations.rb +24 -0
  119. data/test/test-base.rb +194 -0
  120. data/test/test-schema.rb +49 -0
  121. metadata +192 -0
data/AUTHORS ADDED
@@ -0,0 +1 @@
1
+ Kouhei Sutou <kou@clear-code.com>
data/NEWS.ja.rdoc ADDED
@@ -0,0 +1,5 @@
1
+ = お知らせ
2
+
3
+ == 0.0.1: 2009-04-30
4
+
5
+ * 最初のリリース!
data/NEWS.rdoc ADDED
@@ -0,0 +1,5 @@
1
+ = NEWS
2
+
3
+ == 0.0.1: 2009-04-30
4
+
5
+ * Initial release!
data/README.ja.rdoc ADDED
@@ -0,0 +1,49 @@
1
+ = はじめに
2
+
3
+ == 名前
4
+
5
+ ActiveGroonga
6
+
7
+ == 説明
8
+
9
+ ActiveRecord風のAPIを使ってgroongaを使うためのライブラリです。
10
+
11
+ groongaは全文検索機能とカラムストアの機能を提供しています。
12
+ ActiveRecordとRDBの組み合わせの代わりに、ActiveGroongaと
13
+ groongaの組み合わせを使うことができます。
14
+
15
+ Ruby/groongaをベースとしています。Ruby/groongaとgroongaに関
16
+ する情報は以下を参照して下さい。
17
+
18
+ * Ruby/groonga: http://groonga.rubyforge.org/
19
+ * groonga: http://groonga.org/
20
+
21
+ == 作者
22
+
23
+ Kouhei Sutou <kou@clear-code.com>
24
+
25
+ == ライセンス
26
+
27
+ LGPL 2.1です。詳しくはlicense/LGPLを見てください。
28
+
29
+ == 依存ソフトウェア
30
+
31
+ * Ruby/groonga
32
+
33
+ == インストール
34
+
35
+ % sudo gem install activegroonga
36
+
37
+ == ドキュメント
38
+
39
+ http://groonga.rubyforge.org/active_groonga/
40
+
41
+ == メーリングリスト
42
+
43
+ 質問、要望、バグ報告などはgroongaのMLにお願いします。
44
+
45
+ http://lists.sourceforge.jp/mailman/listinfo/groonga-dev
46
+
47
+ == 感謝
48
+
49
+ * ...
data/README.rdoc ADDED
@@ -0,0 +1,49 @@
1
+ = README
2
+
3
+ == Name
4
+
5
+ ActiveGroonga
6
+
7
+ == Description
8
+
9
+ A library to use groonga with ActiveRecord like API.
10
+
11
+ groonga provides both of full text search and column store
12
+ features. ActiveGroonga + groonga can be used as an
13
+ alternative of ActiveRecord + RDB.
14
+
15
+ ActiveGroonga is based on Ruby/groonga. See the following
16
+ URLs about Ruby/groonga and groonga.
17
+
18
+ * Ruby/groonga: http://groonga.rubyforge.org/
19
+ * groonga: http://groonga.org/
20
+
21
+ == Author
22
+
23
+ Kouhei Sutou <kou@clear-code.com>
24
+
25
+ == License
26
+
27
+ LGPL 2.1. See license/LGPL for details.
28
+
29
+ == Dependencies
30
+
31
+ * Ruby/groonga
32
+
33
+ == Install
34
+
35
+ % sudo gem install activegroonga
36
+
37
+ == Documents
38
+
39
+ Japanese only. Sorry.
40
+
41
+ http://groonga.rubyforge.org/active_groonga/
42
+
43
+ == Mailing list
44
+
45
+ http://rubyforge.org/mailman/listinfo/groonga-users-en
46
+
47
+ == Thanks
48
+
49
+ * ...
data/Rakefile ADDED
@@ -0,0 +1,175 @@
1
+ # -*- coding: utf-8; mode: ruby -*-
2
+ #
3
+ # Copyright (C) 2009 Kouhei Sutou <kou@clear-code.com>
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License version 2.1 as published by the Free Software Foundation.
8
+ #
9
+ # This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this library; if not, write to the Free Software
16
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
+
18
+ require 'English'
19
+
20
+ require 'find'
21
+ require 'fileutils'
22
+ require 'pathname'
23
+ require 'erb'
24
+ require 'rubygems'
25
+ gem 'rdoc'
26
+ require 'hoe'
27
+
28
+ ENV["NODOT"] = "yes"
29
+
30
+ Hoe::SUPPORTED_TEST_FRAMEWORKS[:testunit2] = "test/run-test.rb"
31
+
32
+ base_dir = File.join(File.dirname(__FILE__))
33
+ truncate_base_dir = Proc.new do |x|
34
+ x.gsub(/^#{Regexp.escape(base_dir + File::SEPARATOR)}/, '')
35
+ end
36
+
37
+ @groonga_base_dir = File.expand_path(File.join(base_dir, '..', 'groonga'))
38
+ groonga_ext_dir = File.join(@groonga_base_dir, 'src')
39
+ groonga_lib_dir = File.join(groonga_ext_dir, 'lib')
40
+ $LOAD_PATH.unshift(groonga_ext_dir)
41
+ $LOAD_PATH.unshift(groonga_lib_dir)
42
+ ENV["RUBYLIB"] = "#{groonga_lib_dir}:#{groonga_ext_dir}:#{ENV['RUBYLIB']}"
43
+
44
+ def guess_version
45
+ require 'active_groonga/version'
46
+ ActiveGroonga::VERSION::STRING
47
+ end
48
+
49
+ manifest = File.join(base_dir, "Manifest.txt")
50
+ manifest_contents = []
51
+ base_dir_included_components = %w(AUTHORS
52
+ NEWS.rdoc NEWS.ja.rdoc
53
+ README.rdoc README.ja.rdoc
54
+ Rakefile extconf.rb)
55
+ excluded_components = %w(.cvsignore .gdb_history CVS depend Makefile pkg
56
+ .git .svn doc vendor .test-result)
57
+ excluded_suffixes = %w(.png .ps .pdf .o .so .a .txt .~)
58
+ Find.find(base_dir) do |target|
59
+ target = truncate_base_dir[target]
60
+ components = target.split(File::SEPARATOR)
61
+ if components.size == 1 and !File.directory?(target)
62
+ next unless base_dir_included_components.include?(components[0])
63
+ end
64
+ Find.prune if (excluded_components - components) != excluded_components
65
+ next if excluded_suffixes.include?(File.extname(target))
66
+ manifest_contents << target if File.file?(target)
67
+ end
68
+
69
+ File.open(manifest, "w") do |f|
70
+ f.puts manifest_contents.sort.join("\n")
71
+ end
72
+
73
+ # For Hoe's no user friendly default behavior. :<
74
+ File.open("README.txt", "w") {|file| file << "= Dummy README\n== XXX\n"}
75
+ FileUtils.cp("NEWS.rdoc", "History.txt")
76
+ at_exit do
77
+ FileUtils.rm_f("README.txt")
78
+ FileUtils.rm_f("History.txt")
79
+ FileUtils.rm_f(manifest)
80
+ end
81
+
82
+ def cleanup_white_space(entry)
83
+ entry.gsub(/(\A\n+|\n+\z)/, '') + "\n"
84
+ end
85
+
86
+ ENV["VERSION"] ||= guess_version
87
+ version = ENV["VERSION"]
88
+ project = Hoe.new('activegroonga', version) do |project|
89
+ project.rubyforge_name = 'groonga'
90
+ authors = File.join(base_dir, "AUTHORS")
91
+ project.author = File.readlines(authors).collect do |line|
92
+ if /\s*<[^<>]*>$/ =~ line
93
+ $PREMATCH
94
+ else
95
+ nil
96
+ end
97
+ end.compact
98
+ project.email = ['groonga-users-en@rubyforge.org',
99
+ 'groonga-dev@lists.sourceforge.jp']
100
+ project.url = 'http://groonga.rubyforge.org/'
101
+ project.testlib = :testunit2
102
+ project.test_globs = ["test/run-test.rb"]
103
+ project.spec_extras = {
104
+ :extra_rdoc_files => Dir.glob("*.rdoc"),
105
+ }
106
+ project.readme_file = "README.ja.rdoc"
107
+ project.extra_deps = [["activerecord", "=2.3.2"]]
108
+
109
+ news_of_current_release = File.read("NEWS.rdoc").split(/^==\s.*$/)[1]
110
+ project.changes = cleanup_white_space(news_of_current_release)
111
+
112
+ entries = File.read("README.rdoc").split(/^==\s(.*)$/)
113
+ description = cleanup_white_space(entries[entries.index("Description") + 1])
114
+ project.summary, project.description, = description.split(/\n\n+/, 3)
115
+
116
+ project.need_tar = false
117
+ project.remote_rdoc_dir = "active_groonga"
118
+ end
119
+
120
+ project.spec.dependencies.delete_if {|dependency| dependency.name == "hoe"}
121
+
122
+ ObjectSpace.each_object(Rake::RDocTask) do |rdoc_task|
123
+ t_option_index = rdoc_task.options.index("-t")
124
+ rdoc_task.options[t_option_index, 2] = nil
125
+ rdoc_task.title = "ActiveGroonga - #{version}"
126
+ rdoc_task.rdoc_files = Dir.glob("lib/**/*.rb")
127
+ rdoc_task.rdoc_files += Dir.glob("*.rdoc")
128
+ end
129
+
130
+ task :publish_docs => [:prepare_docs_for_publishing]
131
+
132
+
133
+ include ERB::Util
134
+
135
+ def apply_template(file, head, header, footer)
136
+ content = File.read(file)
137
+ content = content.sub(/lang="en"/, 'lang="ja"')
138
+
139
+ title = nil
140
+ content = content.sub(/<title>(.+?)<\/title>/) do
141
+ title = $1
142
+ head.result(binding)
143
+ end
144
+
145
+ content = content.sub(/<body(?:.*?)>/) do |body_start|
146
+ "#{body_start}\n#{header.result(binding)}\n"
147
+ end
148
+
149
+ content = content.sub(/<\/body/) do |body_end|
150
+ "\n#{footer.result(binding)}\n#{body_end}"
151
+ end
152
+
153
+ File.open(file, "w") do |file|
154
+ file.print(content)
155
+ end
156
+ end
157
+
158
+ def erb_template(name)
159
+ file = File.join(@groonga_base_dir, "html", "#{name}.html.erb")
160
+ template = File.read(file)
161
+ erb = ERB.new(template, nil, "-")
162
+ erb.filename = file
163
+ erb
164
+ end
165
+
166
+ task :prepare_docs_for_publishing do
167
+ head = erb_template("head")
168
+ header = erb_template("header")
169
+ footer = erb_template("footer")
170
+ Find.find("doc") do |file|
171
+ if /\.html\z/ =~ file and /_(?:c|rb)\.html\z/ !~ file
172
+ apply_template(file, head, header, footer)
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,75 @@
1
+ # Copyright (C) 2009 Kouhei Sutou <kou@clear-code.com>
2
+ #
3
+ # This library is free software; you can redistribute it and/or
4
+ # modify it under the terms of the GNU Lesser General Public
5
+ # License version 2.1 as published by the Free Software Foundation.
6
+ #
7
+ # This library is distributed in the hope that it will be useful,
8
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10
+ # Lesser General Public License for more details.
11
+ #
12
+ # You should have received a copy of the GNU Lesser General Public
13
+ # License along with this library; if not, write to the Free Software
14
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
+
16
+ require 'rubygems'
17
+ require 'active_support'
18
+ require 'active_record'
19
+
20
+ base_dir = File.dirname(__FILE__)
21
+ ruby_groonga_dir = File.join(base_dir, "..", "..", "groonga")
22
+ ruby_groonga_dir = File.expand_path(ruby_groonga_dir)
23
+ if File.exist?(ruby_groonga_dir)
24
+ $LOAD_PATH.unshift(File.join(ruby_groonga_dir, "src"))
25
+ $LOAD_PATH.unshift(File.join(ruby_groonga_dir, "src", "lib"))
26
+ end
27
+ require 'groonga'
28
+
29
+ module ActiveGroonga
30
+ def self.load_all!
31
+ [Base]
32
+ end
33
+
34
+ autoload :VERSION, 'active_groonga/version'
35
+
36
+ autoload :ActiveGroongaError, 'active_groonga/base'
37
+
38
+ autoload :Aggregations, 'active_groonga/aggregations'
39
+ autoload :AssociationPreload, 'active_groonga/association_preload'
40
+ autoload :Associations, 'active_groonga/associations'
41
+ autoload :AttributeMethods, 'active_groonga/attribute_methods'
42
+ autoload :AutosaveAssociation, 'active_groonga/autosave_association'
43
+ autoload :Base, 'active_groonga/base'
44
+ autoload :Batches, 'active_groonga/batches'
45
+ autoload :Calculations, 'active_groonga/calculations'
46
+ autoload :Callbacks, 'active_groonga/callbacks'
47
+ autoload :Column, 'active_groonga/column'
48
+ autoload :Dirty, 'active_groonga/dirty'
49
+ autoload :Migration, 'active_groonga/migration'
50
+ autoload :Migrator, 'active_groonga/migration'
51
+ autoload :NamedScope, 'active_groonga/named_scope'
52
+ autoload :NestedAttributes, 'active_groonga/nested_attributes'
53
+ autoload :Observing, 'active_groonga/observer'
54
+ autoload :QueryCache, 'active_groonga/query_cache'
55
+ autoload :Reflection, 'active_groonga/reflection'
56
+ autoload :Schema, 'active_groonga/schema'
57
+ autoload :SchemaDumper, 'active_groonga/schema_dumper'
58
+ autoload :Serialization, 'active_groonga/serialization'
59
+ autoload :SessionStore, 'active_groonga/session_store'
60
+ autoload :TestCase, 'active_groonga/test_case'
61
+ autoload :Timestamp, 'active_groonga/timestamp'
62
+ autoload :Transactions, 'active_groonga/transactions'
63
+ autoload :Validations, 'active_groonga/validations'
64
+
65
+ module Locking
66
+ autoload :Optimistic, 'active_groonga/locking/optimistic'
67
+ autoload :Pessimistic, 'active_groonga/locking/pessimistic'
68
+ end
69
+ end
70
+
71
+ # I18n.load_path << File.dirname(__FILE__) + '/active_groonga/locale/en.yml'
72
+
73
+ if defined?(Rails::Configuration)
74
+ require 'active_groonga/rails_support'
75
+ end
@@ -0,0 +1,30 @@
1
+ # Copyright (C) 2009 Kouhei Sutou <kou@clear-code.com>
2
+ #
3
+ # This library is free software; you can redistribute it and/or
4
+ # modify it under the terms of the GNU Lesser General Public
5
+ # License version 2.1 as published by the Free Software Foundation.
6
+ #
7
+ # This library is distributed in the hope that it will be useful,
8
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10
+ # Lesser General Public License for more details.
11
+ #
12
+ # You should have received a copy of the GNU Lesser General Public
13
+ # License along with this library; if not, write to the Free Software
14
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
+
16
+ module ActiveGroonga
17
+ module Aggregations
18
+ class << self
19
+ def included(base)
20
+ base.class_eval do
21
+ include(ActiveRecord::Aggregations)
22
+ extend(ClassMethods)
23
+ end
24
+ end
25
+ end
26
+
27
+ module ClassMethods
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,93 @@
1
+ # Copyright (C) 2009 Kouhei Sutou <kou@clear-code.com>
2
+ #
3
+ # This library is free software; you can redistribute it and/or
4
+ # modify it under the terms of the GNU Lesser General Public
5
+ # License version 2.1 as published by the Free Software Foundation.
6
+ #
7
+ # This library is distributed in the hope that it will be useful,
8
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10
+ # Lesser General Public License for more details.
11
+ #
12
+ # You should have received a copy of the GNU Lesser General Public
13
+ # License along with this library; if not, write to the Free Software
14
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
+
16
+ # This library includes ActiveRecord based codes temporary.
17
+ # Here is their copyright and license:
18
+ #
19
+ # Copyright (c) 2004-2009 David Heinemeier Hansson
20
+ #
21
+ # Permission is hereby granted, free of charge, to any person obtaining
22
+ # a copy of this software and associated documentation files (the
23
+ # "Software"), to deal in the Software without restriction, including
24
+ # without limitation the rights to use, copy, modify, merge, publish,
25
+ # distribute, sublicense, and/or sell copies of the Software, and to
26
+ # permit persons to whom the Software is furnished to do so, subject to
27
+ # the following conditions:
28
+ #
29
+ # The above copyright notice and this permission notice shall be
30
+ # included in all copies or substantial portions of the Software.
31
+ #
32
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39
+
40
+ module ActiveGroonga
41
+ module Associations
42
+ class << self
43
+ def included(base)
44
+ base.class_eval do
45
+ include(ActiveRecord::Associations)
46
+ extend(ClassMethods)
47
+ end
48
+ end
49
+ end
50
+
51
+ autoload :BelongsToAssociation, 'active_groonga/associations/belongs_to_association'
52
+
53
+ module ClassMethods
54
+ # from ActiveRecord
55
+ def belongs_to(association_id, options = {})
56
+ reflection = create_belongs_to_reflection(association_id, options)
57
+
58
+ if reflection.options[:polymorphic]
59
+ association_accessor_methods(reflection, BelongsToPolymorphicAssociation)
60
+ else
61
+ association_accessor_methods(reflection, BelongsToAssociation)
62
+ association_constructor_method(:build, reflection, BelongsToAssociation)
63
+ association_constructor_method(:create, reflection, BelongsToAssociation)
64
+ end
65
+
66
+ # Create the callbacks to update counter cache
67
+ if options[:counter_cache]
68
+ cache_column = reflection.counter_cache_column
69
+
70
+ method_name = "belongs_to_counter_cache_after_create_for_#{reflection.name}".to_sym
71
+ define_method(method_name) do
72
+ association = send(reflection.name)
73
+ association.class.increment_counter(cache_column, send(reflection.primary_key_name)) unless association.nil?
74
+ end
75
+ after_create method_name
76
+
77
+ method_name = "belongs_to_counter_cache_before_destroy_for_#{reflection.name}".to_sym
78
+ define_method(method_name) do
79
+ association = send(reflection.name)
80
+ association.class.decrement_counter(cache_column, send(reflection.primary_key_name)) unless association.nil?
81
+ end
82
+ before_destroy method_name
83
+
84
+ module_eval(
85
+ "#{reflection.class_name}.send(:attr_readonly,\"#{cache_column}\".intern) if defined?(#{reflection.class_name}) && #{reflection.class_name}.respond_to?(:attr_readonly)"
86
+ )
87
+ end
88
+
89
+ configure_dependency_for_belongs_to(reflection)
90
+ end
91
+ end
92
+ end
93
+ end