cat_tree 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca667a9a4b6bdef6808ca2a251b4e0d38e2279a5
4
- data.tar.gz: 990e3a9e31282d8a1dafc49dbfc9cd9bca9af7aa
3
+ metadata.gz: a66fae1f2ad5539ee6016ca8db48040797cb27a2
4
+ data.tar.gz: ee19da2b16fb8583b69198aa51595a1c10be9c68
5
5
  SHA512:
6
- metadata.gz: eef6bc7eb67daa6b4e1c0a8cebadd44e9024ba6e2be3157b7e7253404fa56c5f936112089682b10c7492abc7b09ad4a893e0ff957226d252c5ebeb2635c537eb
7
- data.tar.gz: 14e749d0d9771f6b43f2386f3909d5e2fa5f6ceddab2a8fa88fa15b46dfd07b725448ad753eed0b7b732e6eb26227471580942c748ce35ae76615540d12907b1
6
+ metadata.gz: c07c92b31b81a54752e6f0f3d4424cc78aee4d5012f78175a5aaf8ab222c4a6ab90cdedd8262b912d0430c4f04a7f0253b8d5edfb21e1f4dc055e0ea7717cb91
7
+ data.tar.gz: 10461c6ac759b33f4da7fb141865c191b50db6687e2e8d56c484cfb9be22611adcdd2b7e4c38b5fc96f76ce5420c7da92ef36c09837546b4ec79d947352a32bf
data/.gitignore CHANGED
@@ -7,10 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
- *.bundle
11
- *.so
12
- *.o
13
- *.a
14
- mkmf.log
15
- bundle
16
10
  gemfiles/*.lock
11
+ *.gem
data/.travis.yml CHANGED
@@ -1,11 +1,16 @@
1
1
  language: ruby
2
+ before_install:
3
+ - gem install bundler
2
4
  before_script:
3
5
  - mysql -e 'create database cat_tree_test;'
6
+ - bundle update
4
7
  - "bundle exec ruby spec/prepare.rb"
5
8
  rvm:
6
- - 1.9.3
7
9
  - 2.0.0
8
- - 2.1.2
10
+ - 2.1.8
11
+ - 2.2.4
12
+ - 2.3.0
13
+ - ruby-head
9
14
  gemfile:
10
15
  - gemfiles/rails3.gemfile
11
16
  - gemfiles/rails40.gemfile
data/Appraisals CHANGED
@@ -9,3 +9,7 @@ end
9
9
  appraise "rails41" do
10
10
  gem "activerecord", "~> 4.1.0"
11
11
  end
12
+
13
+ appraise "rails42" do
14
+ gem "activerecord", "~> 4.2.0"
15
+ end
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at tsukasa.oishi@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/LICENSE.txt CHANGED
@@ -1,22 +1,21 @@
1
- Copyright (c) 2014 Tsukasa OISHI
1
+ The MIT License (MIT)
2
2
 
3
- MIT License
3
+ Copyright (c) 2016 Tsukasa OISHI
4
4
 
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
12
11
 
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
15
14
 
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/cat_tree.gemspec CHANGED
@@ -10,20 +10,22 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["tsukasa.oishi@gmail.com"]
11
11
  spec.summary = %q{CatTree monitors ActiveRecord objects in development environment}
12
12
  spec.description = %q{CatTree monitors ActiveRecord objects in development environment}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/tsukasaoishi/cat_tree"
14
14
  spec.license = "MIT"
15
15
 
16
- spec.files = `git ls-files -z`.split("\x0")
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency 'activerecord', '>= 3.2.0', '< 4.2'
22
- spec.add_dependency 'activesupport', '>= 3.2.0', '< 4.2'
21
+ spec.required_ruby_version = '>= 2.0'
23
22
 
24
- spec.add_development_dependency "bundler", "< 1.8", ">= 1.6"
25
- spec.add_development_dependency "rake", "~> 10.0"
26
- spec.add_development_dependency "rspec", '~> 2.14'
27
- spec.add_development_dependency 'appraisal', '~> 1.0'
28
- spec.add_development_dependency 'mysql2'
23
+ spec.add_dependency 'activerecord', '>= 3.2.0', '< 5.0'
24
+ spec.add_dependency 'activesupport', '>= 3.2.0', '< 5.0'
25
+
26
+ spec.add_development_dependency "bundler", ">= 1.3.0", "< 2.0"
27
+ spec.add_development_dependency "rake", ">= 0.8.7"
28
+ spec.add_development_dependency 'mysql2', '~> 0.3.18'
29
+ spec.add_development_dependency "rspec"
30
+ spec.add_development_dependency 'appraisal'
29
31
  end
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.2.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,58 @@
1
+ module CatTree
2
+ class Observer
3
+ class ArBase
4
+ attr_reader :count, :callers
5
+
6
+ def initialize(object)
7
+ return unless object_valid?(object)
8
+ @valid = true
9
+
10
+ @model = object.class.name
11
+ @model_id = object.id
12
+ @count = 1
13
+ @callers = []
14
+
15
+ record_backtrace if CatTree::Config.backtrace
16
+ end
17
+
18
+ def merge(other)
19
+ @count += other.count
20
+ @callers.concat(other.callers)
21
+ self
22
+ end
23
+
24
+ def ==(other)
25
+ key == other.key
26
+ end
27
+
28
+ def key
29
+ [@model, @model_id]
30
+ end
31
+
32
+ def valid?
33
+ !!@valid
34
+ end
35
+
36
+ def title
37
+ "#{@model}(id:#{@model_id})"
38
+ end
39
+
40
+ private
41
+
42
+ def object_valid?(object)
43
+ object.is_a?(ActiveRecord::Base) && !object.new_record?
44
+ end
45
+
46
+ def record_backtrace
47
+ @callers << (defined?(Rails) ? backtrace_in_rails : caller)
48
+ end
49
+
50
+ def backtrace_in_rails
51
+ root_path = Rails.root.to_s
52
+ root_path += "/" unless root_path.last == "/"
53
+ caller.select{|c| c =~ %r!#{root_path}(app|lib)/!}
54
+ end
55
+ end
56
+ end
57
+ end
58
+
@@ -0,0 +1,43 @@
1
+ require 'cat_tree/observer/target_set/ar_base'
2
+
3
+ module CatTree
4
+ class Observer
5
+ class TargetSet
6
+ def initialize
7
+ @set = {}
8
+ end
9
+
10
+ def notice(object)
11
+ return unless target = get_target(object)
12
+ return unless target.valid?
13
+
14
+ if same_target = @set[target.key]
15
+ @set[target.key] = same_target.merge(target)
16
+ else
17
+ @set[target.key] = target
18
+ end
19
+ end
20
+
21
+ def object_count
22
+ @set.values.inject(0){|t,v| t + v.count}
23
+ end
24
+
25
+ def same_objects
26
+ @set.values.select{|v| v.count > 1}.sort_by{|v| v.count}.reverse
27
+ end
28
+
29
+ def empty?
30
+ @set.empty?
31
+ end
32
+
33
+ private
34
+
35
+ def get_target(object)
36
+ case object
37
+ when ActiveRecord::Base
38
+ ArBase.new(object)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,4 +1,5 @@
1
1
  require 'active_record'
2
+ require 'cat_tree/observer/target_set'
2
3
  require 'cat_tree/logger'
3
4
 
4
5
  module CatTree
@@ -8,23 +9,19 @@ module CatTree
8
9
  end
9
10
 
10
11
  def initialize
11
- @ar_base = {}
12
+ @target_set = TargetSet.new
12
13
  end
13
14
 
14
15
  def notice(object)
15
- return if object.new_record?
16
- key = "#{object.class.name}(id:#{object.id})"
17
- @ar_base[key] ||= {:count => 0, :callers => []}
18
- @ar_base[key][:count] += 1
19
- record_backtrace(@ar_base[key][:callers]) if Config.backtrace
16
+ @target_set.notice(object)
20
17
  end
21
18
 
22
19
  def ar_base_count
23
- @ar_base.values.inject(0){|t,v| t + v[:count]}
20
+ @target_set.object_count
24
21
  end
25
22
 
26
23
  def same_ar_base_objects
27
- Hash[*(@ar_base.select{|k,v| v[:count] > 1}.flatten)]
24
+ @target_set.same_objects
28
25
  end
29
26
 
30
27
  def check
@@ -37,30 +34,19 @@ module CatTree
37
34
 
38
35
  private
39
36
 
40
- def record_backtrace(callers)
41
- if defined?(Rails)
42
- root_path = Rails.root.to_s
43
- root_path += "/" unless root_path.last == "/"
44
- cal = caller.select{|c| c =~ %r!#{root_path}(app|lib)/!}
45
- callers << cal unless cal.empty?
46
- else
47
- callers << caller
48
- end
49
- end
50
-
51
37
  def output_message
52
- return if @ar_base.empty?
38
+ return if @target_set.empty?
53
39
 
54
40
  msg = ["", "[CatTree]"]
55
41
  msg << " ActiveRecord::Base:\t#{ar_base_count}"
56
42
 
57
43
  unless (same_objects = same_ar_base_objects).empty?
58
44
  msg << " Same objects:"
59
- same_objects.keys.sort_by{|k| same_objects[k][:count]}.reverse.each do |key|
60
- msg << " #{key}:\t#{same_objects[key][:count]}"
45
+ same_objects.each do |same_obj|
46
+ msg << " #{same_obj.title}:\t#{same_obj.count}"
61
47
 
62
48
  if Config.backtrace
63
- same_objects[key][:callers].each do |cal|
49
+ same_obj.callers.each do |cal|
64
50
  cal.each{|c| msg << " #{c}"}
65
51
  msg << ""
66
52
  end
@@ -1,3 +1,3 @@
1
1
  module CatTree
2
- VERSION = "0.0.2"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cat_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tsukasa OISHI
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2014-09-22 00:00:00.000000000 Z
11
+ date: 2016-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: 3.2.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '4.2'
22
+ version: '5.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: 3.2.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '4.2'
32
+ version: '5.0'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: activesupport
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -39,7 +39,7 @@ dependencies:
39
39
  version: 3.2.0
40
40
  - - "<"
41
41
  - !ruby/object:Gem::Version
42
- version: '4.2'
42
+ version: '5.0'
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
@@ -49,71 +49,71 @@ dependencies:
49
49
  version: 3.2.0
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
- version: '4.2'
52
+ version: '5.0'
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: bundler
55
55
  requirement: !ruby/object:Gem::Requirement
56
56
  requirements:
57
- - - "<"
58
- - !ruby/object:Gem::Version
59
- version: '1.8'
60
57
  - - ">="
61
58
  - !ruby/object:Gem::Version
62
- version: '1.6'
59
+ version: 1.3.0
60
+ - - "<"
61
+ - !ruby/object:Gem::Version
62
+ version: '2.0'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - "<"
68
- - !ruby/object:Gem::Version
69
- version: '1.8'
70
67
  - - ">="
71
68
  - !ruby/object:Gem::Version
72
- version: '1.6'
69
+ version: 1.3.0
70
+ - - "<"
71
+ - !ruby/object:Gem::Version
72
+ version: '2.0'
73
73
  - !ruby/object:Gem::Dependency
74
74
  name: rake
75
75
  requirement: !ruby/object:Gem::Requirement
76
76
  requirements:
77
- - - "~>"
77
+ - - ">="
78
78
  - !ruby/object:Gem::Version
79
- version: '10.0'
79
+ version: 0.8.7
80
80
  type: :development
81
81
  prerelease: false
82
82
  version_requirements: !ruby/object:Gem::Requirement
83
83
  requirements:
84
- - - "~>"
84
+ - - ">="
85
85
  - !ruby/object:Gem::Version
86
- version: '10.0'
86
+ version: 0.8.7
87
87
  - !ruby/object:Gem::Dependency
88
- name: rspec
88
+ name: mysql2
89
89
  requirement: !ruby/object:Gem::Requirement
90
90
  requirements:
91
91
  - - "~>"
92
92
  - !ruby/object:Gem::Version
93
- version: '2.14'
93
+ version: 0.3.18
94
94
  type: :development
95
95
  prerelease: false
96
96
  version_requirements: !ruby/object:Gem::Requirement
97
97
  requirements:
98
98
  - - "~>"
99
99
  - !ruby/object:Gem::Version
100
- version: '2.14'
100
+ version: 0.3.18
101
101
  - !ruby/object:Gem::Dependency
102
- name: appraisal
102
+ name: rspec
103
103
  requirement: !ruby/object:Gem::Requirement
104
104
  requirements:
105
- - - "~>"
105
+ - - ">="
106
106
  - !ruby/object:Gem::Version
107
- version: '1.0'
107
+ version: '0'
108
108
  type: :development
109
109
  prerelease: false
110
110
  version_requirements: !ruby/object:Gem::Requirement
111
111
  requirements:
112
- - - "~>"
112
+ - - ">="
113
113
  - !ruby/object:Gem::Version
114
- version: '1.0'
114
+ version: '0'
115
115
  - !ruby/object:Gem::Dependency
116
- name: mysql2
116
+ name: appraisal
117
117
  requirement: !ruby/object:Gem::Requirement
118
118
  requirements:
119
119
  - - ">="
@@ -136,6 +136,7 @@ files:
136
136
  - ".gitignore"
137
137
  - ".travis.yml"
138
138
  - Appraisals
139
+ - CODE_OF_CONDUCT.md
139
140
  - Gemfile
140
141
  - LICENSE.txt
141
142
  - README.md
@@ -144,20 +145,18 @@ files:
144
145
  - gemfiles/rails3.gemfile
145
146
  - gemfiles/rails40.gemfile
146
147
  - gemfiles/rails41.gemfile
148
+ - gemfiles/rails42.gemfile
147
149
  - lib/cat_tree.rb
148
150
  - lib/cat_tree/config.rb
149
151
  - lib/cat_tree/initializer.rb
150
152
  - lib/cat_tree/logger.rb
151
153
  - lib/cat_tree/observer.rb
154
+ - lib/cat_tree/observer/target_set.rb
155
+ - lib/cat_tree/observer/target_set/ar_base.rb
152
156
  - lib/cat_tree/rack.rb
153
157
  - lib/cat_tree/railtie.rb
154
158
  - lib/cat_tree/version.rb
155
- - spec/cat_tree_test.sqlite3
156
- - spec/db_dump.sql
157
- - spec/prepare.rb
158
- - spec/spec_helper.rb
159
- - spec/unit/observer_spec.rb
160
- homepage: ''
159
+ homepage: https://github.com/tsukasaoishi/cat_tree
161
160
  licenses:
162
161
  - MIT
163
162
  metadata: {}
@@ -169,7 +168,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
169
168
  requirements:
170
169
  - - ">="
171
170
  - !ruby/object:Gem::Version
172
- version: '0'
171
+ version: '2.0'
173
172
  required_rubygems_version: !ruby/object:Gem::Requirement
174
173
  requirements:
175
174
  - - ">="
@@ -177,13 +176,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
176
  version: '0'
178
177
  requirements: []
179
178
  rubyforge_project:
180
- rubygems_version: 2.2.2
179
+ rubygems_version: 2.5.1
181
180
  signing_key:
182
181
  specification_version: 4
183
182
  summary: CatTree monitors ActiveRecord objects in development environment
184
- test_files:
185
- - spec/cat_tree_test.sqlite3
186
- - spec/db_dump.sql
187
- - spec/prepare.rb
188
- - spec/spec_helper.rb
189
- - spec/unit/observer_spec.rb
183
+ test_files: []
Binary file
data/spec/db_dump.sql DELETED
@@ -1,102 +0,0 @@
1
- -- MySQL dump 10.13 Distrib 5.6.19, for osx10.9 (x86_64)
2
- --
3
- -- Host: localhost Database: cat_tree_test
4
- -- ------------------------------------------------------
5
- -- Server version 5.6.19
6
-
7
- /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
8
- /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
9
- /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
10
- /*!40101 SET NAMES utf8 */;
11
- /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
12
- /*!40103 SET TIME_ZONE='+00:00' */;
13
- /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
14
- /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
15
- /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
16
- /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
17
-
18
- --
19
- -- Table structure for table `articles`
20
- --
21
-
22
- DROP TABLE IF EXISTS `articles`;
23
- /*!40101 SET @saved_cs_client = @@character_set_client */;
24
- /*!40101 SET character_set_client = utf8 */;
25
- CREATE TABLE `articles` (
26
- `id` int(11) NOT NULL AUTO_INCREMENT,
27
- `user_id` int(11) DEFAULT NULL,
28
- `title` varchar(255) DEFAULT NULL,
29
- `body` text,
30
- `created_at` datetime NOT NULL,
31
- `updated_at` datetime NOT NULL,
32
- PRIMARY KEY (`id`)
33
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
34
- /*!40101 SET character_set_client = @saved_cs_client */;
35
-
36
- --
37
- -- Dumping data for table `articles`
38
- --
39
-
40
- LOCK TABLES `articles` WRITE;
41
- /*!40000 ALTER TABLE `articles` DISABLE KEYS */;
42
- /*!40000 ALTER TABLE `articles` ENABLE KEYS */;
43
- UNLOCK TABLES;
44
-
45
- --
46
- -- Table structure for table `schema_migrations`
47
- --
48
-
49
- DROP TABLE IF EXISTS `schema_migrations`;
50
- /*!40101 SET @saved_cs_client = @@character_set_client */;
51
- /*!40101 SET character_set_client = utf8 */;
52
- CREATE TABLE `schema_migrations` (
53
- `version` varchar(255) NOT NULL,
54
- UNIQUE KEY `unique_schema_migrations` (`version`)
55
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
56
- /*!40101 SET character_set_client = @saved_cs_client */;
57
-
58
- --
59
- -- Dumping data for table `schema_migrations`
60
- --
61
-
62
- LOCK TABLES `schema_migrations` WRITE;
63
- /*!40000 ALTER TABLE `schema_migrations` DISABLE KEYS */;
64
- INSERT INTO `schema_migrations` VALUES ('20140918010600'),('20140918010659');
65
- /*!40000 ALTER TABLE `schema_migrations` ENABLE KEYS */;
66
- UNLOCK TABLES;
67
-
68
- --
69
- -- Table structure for table `users`
70
- --
71
-
72
- DROP TABLE IF EXISTS `users`;
73
- /*!40101 SET @saved_cs_client = @@character_set_client */;
74
- /*!40101 SET character_set_client = utf8 */;
75
- CREATE TABLE `users` (
76
- `id` int(11) NOT NULL AUTO_INCREMENT,
77
- `name` varchar(255) DEFAULT NULL,
78
- `created_at` datetime NOT NULL,
79
- `updated_at` datetime NOT NULL,
80
- PRIMARY KEY (`id`)
81
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
82
- /*!40101 SET character_set_client = @saved_cs_client */;
83
-
84
- --
85
- -- Dumping data for table `users`
86
- --
87
-
88
- LOCK TABLES `users` WRITE;
89
- /*!40000 ALTER TABLE `users` DISABLE KEYS */;
90
- /*!40000 ALTER TABLE `users` ENABLE KEYS */;
91
- UNLOCK TABLES;
92
- /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
93
-
94
- /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
95
- /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
96
- /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
97
- /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
98
- /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
99
- /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
100
- /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
101
-
102
- -- Dump completed on 2014-09-19 12:11:03
data/spec/prepare.rb DELETED
@@ -1 +0,0 @@
1
- system("mysql -uroot cat_tree_test < spec/db_dump.sql")
data/spec/spec_helper.rb DELETED
@@ -1,33 +0,0 @@
1
- ENV["RAILS_ENV"]="test"
2
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
- require 'logger'
4
- require 'cat_tree'
5
- require File.join(File.dirname(__FILE__), "prepare.rb")
6
-
7
- ActiveRecord::Base.establish_connection(
8
- :adapter => 'mysql2',
9
- :encoding => 'utf8',
10
- :reconnect => false,
11
- :database => 'cat_tree_test',
12
- :username => 'root',
13
- :pool => 5,
14
- :timeout => 5000
15
- )
16
-
17
- module TruncateTable
18
- def truncate
19
- connection.execute("TRUNCATE TABLE #{table_name}")
20
- end
21
- end
22
-
23
- class User < ActiveRecord::Base
24
- extend TruncateTable
25
- has_many :articles
26
- end
27
-
28
- class Article < ActiveRecord::Base
29
- extend TruncateTable
30
- belongs_to :user
31
- end
32
-
33
- CatTree::Initializer.extend_active_record
@@ -1,54 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe CatTree::Observer do
4
- before do
5
- User.truncate
6
- Article.truncate
7
-
8
- u = User.create(:name => "cat_tree")
9
- u.articles.create(:title => "title1")
10
- u.articles.create(:title => "title2")
11
- u2 = User.create(:name => "cat_tree2")
12
- u2.articles.create(:title => "title3")
13
- @number_of_object = rand(10) + 1
14
- end
15
-
16
- context "#check" do
17
- it "count to find AR::Base object" do
18
- observer = CatTree::Observer.new
19
- observer.check do
20
- @number_of_object.times { User.where(:name => "cat_tree").first }
21
- end
22
-
23
- expect(observer.ar_base_count).to eq(@number_of_object)
24
- end
25
-
26
- it "count to find AR::Base object with association" do
27
- observer = CatTree::Observer.new
28
- observer.check do
29
- @number_of_object.times { User.includes(:articles).where(:name => "cat_tree").first }
30
- end
31
-
32
- expect(observer.ar_base_count).to eq(@number_of_object * 3)
33
- end
34
-
35
- it "count same AR:Base object" do
36
- @number_of_object += 1
37
- observer = CatTree::Observer.new
38
- observer.check do
39
- User.new
40
- User.create(:name => "dummy")
41
- @number_of_object.times do
42
- User.includes(:articles).where(:name => "cat_tree").first
43
- end
44
- end
45
-
46
- result = observer.same_ar_base_objects
47
- expect(result.size).to eq(3)
48
- result.each do |key, value|
49
- expect(["User(id:1)", "Article(id:1)", "Article(id:2)"]).to include(key)
50
- expect(value[:count]).to eq(@number_of_object)
51
- end
52
- end
53
- end
54
- end