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
@@ -0,0 +1,107 @@
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
+ class Column < ActiveRecord::ConnectionAdapters::Column
18
+ # Instantiates a new column in the table.
19
+ #
20
+ # +column+ is the Groonga::Column.
21
+ def initialize(column)
22
+ @column = column
23
+ @name = column.name.split(/\./, 2)[1]
24
+ @type = detect_type
25
+ end
26
+
27
+ def type_cast(value)
28
+ return nil if value.nil?
29
+ case type
30
+ when :references
31
+ if value.is_a?(ActiveGroonga::Base)
32
+ value
33
+ else
34
+ reference_object_class.find(value)
35
+ end
36
+ else
37
+ super
38
+ end
39
+ end
40
+
41
+ def type_cast_code(var_name)
42
+ case type
43
+ when :references
44
+ "#{reference_object_class.name}.find(#{var_name})"
45
+ else
46
+ super
47
+ end
48
+ end
49
+
50
+ def number?
51
+ super or type == :unsigned_integer
52
+ end
53
+
54
+ def quote(value)
55
+ case value
56
+ when ActiveGroonga::Base
57
+ Groonga::Record.new(value.class.table, value.id)
58
+ else
59
+ value
60
+ end
61
+ end
62
+
63
+ def reference_type?
64
+ @type == :references
65
+ end
66
+
67
+ def reference_object_name
68
+ return nil unless reference_type?
69
+ @column.range.name.gsub(/(?:\A<table:|>\z)/, '')
70
+ end
71
+
72
+ def reference_object_class
73
+ table_name = reference_object_name
74
+ return nil if table_name.nil?
75
+ table_name.camelcase.singularize.constantize
76
+ end
77
+
78
+ private
79
+ def detect_type
80
+ case @column.range
81
+ when Groonga::Type
82
+ case @column.range.id
83
+ when Groonga::Type::INT
84
+ :integer
85
+ when Groonga::Type::UINT
86
+ :unsigned_integer
87
+ when Groonga::Type::INT64
88
+ :decimal
89
+ when Groonga::Type::FLOAT
90
+ :float
91
+ when Groonga::Type::TIME
92
+ :time
93
+ when Groonga::Type::SHORT_TEXT
94
+ :string
95
+ when Groonga::Type::TEXT, Groonga::Type::LONG_TEXT
96
+ :text
97
+ else
98
+ :string
99
+ end
100
+ when Groonga::Table
101
+ :references
102
+ else
103
+ :string
104
+ end
105
+ end
106
+ end
107
+ 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 Dirty
18
+ class << self
19
+ def included(base)
20
+ base.class_eval do
21
+ include(ActiveRecord::Dirty)
22
+ extend(ClassMethods)
23
+ end
24
+ end
25
+ end
26
+
27
+ module ClassMethods
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,92 @@
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 TestFixtures
18
+ class << self
19
+ def included(base)
20
+ base.class_eval do
21
+ alias_method_chain :setup_fixtures, :active_groonga
22
+ alias_method_chain :teardown_fixtures, :active_groonga
23
+ end
24
+ end
25
+ end
26
+
27
+ def setup_fixtures_with_active_groonga
28
+ setup_fixtures_without_active_groonga
29
+
30
+ @fixture_cache ||= {}
31
+ @@already_loaded_fixtures ||= {}
32
+
33
+ load_active_groonga_fixtures
34
+ end
35
+
36
+ def teardown_fixtures_with_active_groonga
37
+ teardown_fixtures_without_active_groonga
38
+ end
39
+
40
+ def load_active_groonga_fixtures
41
+ @loaded_fixtures ||= {}
42
+ fixtures = Fixtures.create_fixtures(fixture_path, fixture_table_names, fixture_class_names) do
43
+ ConnectionMock.new
44
+ end
45
+ unless fixtures.nil?
46
+ if fixtures.instance_of?(Fixtures)
47
+ @loaded_fixtures[fixtures.name] = fixtures
48
+ else
49
+ fixtures.each { |f| @loaded_fixtures[f.name] = f }
50
+ end
51
+ end
52
+ end
53
+
54
+ class ConnectionMock
55
+ def initialize
56
+ @last_quoted_table_name = nil
57
+ end
58
+
59
+ def disable_referential_integrity
60
+ yield
61
+ end
62
+
63
+ def transaction(options=nil)
64
+ yield
65
+ end
66
+
67
+ def quote_table_name(table_name)
68
+ @last_quoted_table_name = Base.groonga_table_name(table_name)
69
+ table_name
70
+ end
71
+
72
+ def delete(sql, name=nil)
73
+ if @last_quoted_table_name
74
+ Base.context[@last_quoted_table_name].truncate
75
+ end
76
+ end
77
+
78
+ def insert_fixture(fixture, table_name)
79
+ table = Base.context[Base.groonga_table_name(table_name)]
80
+ record = table.add
81
+
82
+ row = fixture.to_hash
83
+
84
+ fixture.each do |key, value|
85
+ record[key] = value
86
+ end
87
+
88
+ row[fixture.model_class.primary_key] = record.id
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,150 @@
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
+ class Migration < ActiveRecord::Migration
42
+ class << self
43
+ undef_method :connection
44
+
45
+ def method_missing(method, *arguments, &block)
46
+ arg_list = arguments.map(&:inspect) * ', '
47
+
48
+ say_with_time "#{method}(#{arg_list})" do
49
+ unless arguments.empty? || method == :execute
50
+ arguments[0] = Migrator.proper_table_name(arguments.first)
51
+ end
52
+ Schema.send(method, *arguments, &block)
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ class Migrator < ActiveRecord::Migrator
59
+ class << self
60
+ def schema_migrations_table_name
61
+ Base.table_name_prefix + 'schema_migrations' + Base.table_name_suffix
62
+ end
63
+
64
+ def groonga_schema_migrations_table_name
65
+ Base.groonga_metadata_table_name(schema_migrations_table_name)
66
+ end
67
+
68
+ def get_all_versions
69
+ table = Base.context[groonga_schema_migrations_table_name]
70
+ table.records.collect {|record| record.key.to_i}.sort
71
+ end
72
+
73
+ def current_version
74
+ table = Base.context[groonga_schema_migrations_table_name]
75
+ if table.nil?
76
+ 0
77
+ else
78
+ get_all_versions.max || 0
79
+ end
80
+ end
81
+
82
+ def proper_table_name(name)
83
+ begin
84
+ name.table_name
85
+ rescue
86
+ "#{Base.table_name_prefix}#{name}#{Base.table_name_suffix}"
87
+ end
88
+ end
89
+ end
90
+
91
+ def initialize(direction, migrations_path, target_version = nil)
92
+ Schema.initialize_schema_management_tables
93
+ @direction, @migrations_path, @target_version = direction, migrations_path, target_version
94
+ FileUtils.mkdir_p(@migrations_path) unless File.exist?(@migrations_path)
95
+ end
96
+
97
+ def migrate
98
+ current = migrations.detect { |m| m.version == current_version }
99
+ target = migrations.detect { |m| m.version == @target_version }
100
+
101
+ if target.nil? && !@target_version.nil? && @target_version > 0
102
+ raise UnknownMigrationVersionError.new(@target_version)
103
+ end
104
+
105
+ start = up? ? 0 : (migrations.index(current) || 0)
106
+ finish = migrations.index(target) || migrations.size - 1
107
+ runnable = migrations[start..finish]
108
+
109
+ # skip the last migration if we're headed down, but not ALL the way down
110
+ runnable.pop if down? && !target.nil?
111
+
112
+ runnable.each do |migration|
113
+ Base.logger.info "Migrating to #{migration.name} (#{migration.version})"
114
+
115
+ # On our way up, we skip migrating the ones we've already migrated
116
+ next if up? && migrated.include?(migration.version.to_i)
117
+
118
+ # On our way down, we skip reverting the ones we've never migrated
119
+ if down? && !migrated.include?(migration.version.to_i)
120
+ migration.announce 'never migrated, skipping'; migration.write
121
+ next
122
+ end
123
+
124
+ begin
125
+ migration.migrate(@direction)
126
+ record_version_state_after_migrating(migration.version)
127
+ rescue => e
128
+ raise StandardError, "An error has occurred, all later migrations canceled:\n\n#{e}", e.backtrace
129
+ end
130
+ end
131
+ end
132
+
133
+ private
134
+ def record_version_state_after_migrating(version)
135
+ table_name = self.class.groonga_schema_migrations_table_name
136
+ table = Base.context[table_name]
137
+
138
+ @migrated_versions ||= []
139
+ if down?
140
+ @migrated_versions.delete(version.to_i)
141
+ table.records.each do |record|
142
+ record.delete if record.key == version.to_s
143
+ end
144
+ else
145
+ @migrated_versions.push(version.to_i).sort!
146
+ table.add(version.to_s)
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,31 @@
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
+ class Rails::Configuration
17
+ attr_writer :active_groonga, :groonga_configuration_file
18
+
19
+ def active_groonga
20
+ @active_groonga ||= Rails::OrderedOptions.new
21
+ end
22
+
23
+ def groonga_configuration_file
24
+ File.join(root_path, 'config', 'groonga.yml')
25
+ end
26
+
27
+ def groonga_configuration
28
+ require 'erb'
29
+ YAML::load(ERB.new(IO.read(groonga_configuration_file)).result)
30
+ end
31
+ 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 Reflection
18
+ class << self
19
+ def included(base)
20
+ base.class_eval do
21
+ include(ActiveRecord::Reflection)
22
+ extend(ClassMethods)
23
+ end
24
+ end
25
+ end
26
+
27
+ module ClassMethods
28
+ end
29
+ end
30
+ end