acts_as_aggregate_root 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ gem "rails", "= 2.3.5"
9
+ group :development do
10
+ gem "shoulda", ">= 0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.5.2"
13
+ gem "rcov", ">= 0"
14
+ gem "rspec", ">= 0"
15
+ gem 'sqlite3-ruby', :require => 'sqlite3'
16
+ gem 'mysql'
17
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actionmailer (2.3.5)
5
+ actionpack (= 2.3.5)
6
+ actionpack (2.3.5)
7
+ activesupport (= 2.3.5)
8
+ rack (~> 1.0.0)
9
+ activerecord (2.3.5)
10
+ activesupport (= 2.3.5)
11
+ activeresource (2.3.5)
12
+ activesupport (= 2.3.5)
13
+ activesupport (2.3.5)
14
+ diff-lcs (1.1.2)
15
+ git (1.2.5)
16
+ jeweler (1.5.2)
17
+ bundler (~> 1.0.0)
18
+ git (>= 1.2.5)
19
+ rake
20
+ mysql (2.8.1)
21
+ rack (1.0.1)
22
+ rails (2.3.5)
23
+ actionmailer (= 2.3.5)
24
+ actionpack (= 2.3.5)
25
+ activerecord (= 2.3.5)
26
+ activeresource (= 2.3.5)
27
+ activesupport (= 2.3.5)
28
+ rake (>= 0.8.3)
29
+ rake (0.8.7)
30
+ rcov (0.9.9)
31
+ rspec (2.4.0)
32
+ rspec-core (~> 2.4.0)
33
+ rspec-expectations (~> 2.4.0)
34
+ rspec-mocks (~> 2.4.0)
35
+ rspec-core (2.4.0)
36
+ rspec-expectations (2.4.0)
37
+ diff-lcs (~> 1.1.2)
38
+ rspec-mocks (2.4.0)
39
+ shoulda (2.11.3)
40
+ sqlite3 (1.3.3)
41
+ sqlite3-ruby (1.3.3)
42
+ sqlite3 (>= 1.3.3)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ bundler (~> 1.0.0)
49
+ jeweler (~> 1.5.2)
50
+ mysql
51
+ rails (= 2.3.5)
52
+ rcov
53
+ rspec
54
+ shoulda
55
+ sqlite3-ruby
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Surya Gaddipati
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,19 @@
1
+ = acts-as-aggregate-root
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to acts-as-aggregate-root
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Surya Gaddipati. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "acts_as_aggregate_root"
16
+ gem.homepage = "http://github.com/suryagaddipati/acts-as-aggregate-root"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Makes an active record object act as an aggregate root}
19
+ gem.description = %Q{Automatically manages saving, deletion and validating the object graph}
20
+ gem.email = "surya.gaddipati@gmail.com"
21
+ gem.authors = ["Surya Gaddipati"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "acts_as_aggregate_root #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,90 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{acts_as_aggregate_root}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Surya Gaddipati"]
12
+ s.date = %q{2011-01-28}
13
+ s.description = %q{Automatically manages saving, deletion and validating the object graph}
14
+ s.email = %q{surya.gaddipati@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "acts-as-aggregate-root.gemspec",
28
+ "acts_as_taggable_on.sqlite3",
29
+ "lib/acts_as_aggregate_root.rb",
30
+ "lib/acts_as_aggregate_root/acts_as_aggregate_root.rb",
31
+ "rails/init.rb",
32
+ "spec/acts_as_aggregate_root/acts_as_aggregate_root_spec.rb",
33
+ "spec/bm.rb",
34
+ "spec/database.yml",
35
+ "spec/debug.log",
36
+ "spec/order.rb",
37
+ "spec/order_item.rb",
38
+ "spec/schema.rb",
39
+ "spec/spec_helper.rb",
40
+ "spec/sub_order.rb"
41
+ ]
42
+ s.homepage = %q{http://github.com/suryagaddipati/acts-as-aggregate-root}
43
+ s.licenses = ["MIT"]
44
+ s.require_paths = ["lib"]
45
+ s.rubygems_version = %q{1.4.2}
46
+ s.summary = %q{Makes an active record object act as an aggregate root}
47
+ s.test_files = [
48
+ "spec/acts_as_aggregate_root/acts_as_aggregate_root_spec.rb",
49
+ "spec/bm.rb",
50
+ "spec/order.rb",
51
+ "spec/order_item.rb",
52
+ "spec/schema.rb",
53
+ "spec/spec_helper.rb",
54
+ "spec/sub_order.rb"
55
+ ]
56
+
57
+ if s.respond_to? :specification_version then
58
+ s.specification_version = 3
59
+
60
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
61
+ s.add_runtime_dependency(%q<rails>, ["= 2.3.5"])
62
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
63
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
64
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
65
+ s.add_development_dependency(%q<rcov>, [">= 0"])
66
+ s.add_development_dependency(%q<rspec>, [">= 0"])
67
+ s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
68
+ s.add_development_dependency(%q<mysql>, [">= 0"])
69
+ else
70
+ s.add_dependency(%q<rails>, ["= 2.3.5"])
71
+ s.add_dependency(%q<shoulda>, [">= 0"])
72
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
73
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
74
+ s.add_dependency(%q<rcov>, [">= 0"])
75
+ s.add_dependency(%q<rspec>, [">= 0"])
76
+ s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
77
+ s.add_dependency(%q<mysql>, [">= 0"])
78
+ end
79
+ else
80
+ s.add_dependency(%q<rails>, ["= 2.3.5"])
81
+ s.add_dependency(%q<shoulda>, [">= 0"])
82
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
83
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
84
+ s.add_dependency(%q<rcov>, [">= 0"])
85
+ s.add_dependency(%q<rspec>, [">= 0"])
86
+ s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
87
+ s.add_dependency(%q<mysql>, [">= 0"])
88
+ end
89
+ end
90
+
Binary file
@@ -0,0 +1,6 @@
1
+ require "active_record"
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require "acts_as_aggregate_root/acts_as_aggregate_root"
4
+ $LOAD_PATH.shift
5
+
6
+ ActiveRecord::Base.class_eval { include ActsAsAggregateRoot }
@@ -0,0 +1,40 @@
1
+
2
+ module ActsAsAggregateRoot #:nodoc:
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+ module ClassMethods
7
+ def acts_as_aggregate_root
8
+
9
+ def add_destroy_methods_to_class(klass)
10
+ klass.reflections.select {|(k,v)| v.macro == :has_many }.each {|(name,reflection)|
11
+ add_destroy_method(klass,name.to_s)
12
+ reflection.options[:autosave] = true
13
+ add_destroy_methods_to_class(reflection.klass)
14
+ }
15
+ end
16
+
17
+
18
+ def add_destroy_method(klass,reflection_name)
19
+ klass.instance_eval do
20
+ method_name = "has_many_dependent_destroy_for_#{reflection_name}".to_sym
21
+ define_method(method_name) do
22
+ send(reflection_name).each { |o| o.destroy }
23
+ end
24
+ before_destroy method_name
25
+ end
26
+ end
27
+
28
+ self.instance_eval do
29
+ def has_many(association_id, options = {}, &extension)
30
+ # options .merge! :autosave => true , :dependent => :destroy
31
+ super
32
+ reflection = create_has_many_reflection(association_id, options, &extension)
33
+ reflection.options[:autosave] = true
34
+ add_destroy_method(self,reflection.name)
35
+ add_destroy_methods_to_class(reflection.klass)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "acts_as_aggregate_root"
@@ -0,0 +1,50 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Acts as Aggregate Root" do
4
+ before(:each) do
5
+ clean_database!
6
+ end
7
+
8
+ it "should delete sub-order if order is deleted" do
9
+ o = Order.new(:sub_orders => [SubOrder.new])
10
+ o.save(false)
11
+ o.destroy
12
+ Order.all.count.should == 0
13
+ SubOrder.all.count.should == 0
14
+ end
15
+
16
+ it "should delete sub-order if order is deleted" do
17
+ o = Order.new(:sub_orders => [SubOrder.new(:order_items => [OrderItem.new] )])
18
+ o.save(false)
19
+ o.destroy
20
+ Order.all.count.should == 0
21
+ SubOrder.all.count.should == 0
22
+ OrderItem.all.count.should == 0
23
+ end
24
+
25
+ it "should save sub-order if order is saved" do
26
+ o = Order.new(:sub_orders => [SubOrder.new])
27
+ o.save(false)
28
+
29
+ o.name = "OrderName"
30
+ o.sub_orders.first.name = "SubOrderName"
31
+ o.save
32
+
33
+ Order.first.name.should == "OrderName"
34
+ SubOrder.first.name.should == "SubOrderName"
35
+
36
+ end
37
+
38
+ it "should save sub-order if order is saved" do
39
+ o = Order.new(:sub_orders => [SubOrder.new(:order_items => [OrderItem.new])])
40
+ o.save(false)
41
+
42
+ o.name = "OrderName"
43
+ o.sub_orders.first.order_items.first.name = "OrderItemName"
44
+ o.save
45
+
46
+ Order.first.name.should == "OrderName"
47
+ OrderItem.first.name.should == "OrderItemName"
48
+
49
+ end
50
+ end
data/spec/bm.rb ADDED
@@ -0,0 +1,52 @@
1
+ require 'active_record'
2
+ require 'action_view'
3
+ require File.expand_path('../../lib/acts-as-taggable-on', __FILE__)
4
+
5
+ if defined?(ActiveRecord::Acts::TaggableOn)
6
+ ActiveRecord::Base.send :include, ActiveRecord::Acts::TaggableOn
7
+ ActiveRecord::Base.send :include, ActiveRecord::Acts::Tagger
8
+ ActionView::Base.send :include, TagsHelper if defined?(ActionView::Base)
9
+ end
10
+
11
+ TEST_DATABASE_FILE = File.join(File.dirname(__FILE__), '..', 'test.sqlite3')
12
+ File.unlink(TEST_DATABASE_FILE) if File.exist?(TEST_DATABASE_FILE)
13
+ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => TEST_DATABASE_FILE
14
+
15
+ ActiveRecord::Base.silence do
16
+ ActiveRecord::Migration.verbose = false
17
+ ActiveRecord::Schema.define :version => 0 do
18
+ create_table "taggings", :force => true do |t|
19
+ t.integer "tag_id", :limit => 11
20
+ t.integer "taggable_id", :limit => 11
21
+ t.string "taggable_type"
22
+ t.string "context"
23
+ t.datetime "created_at"
24
+ t.integer "tagger_id", :limit => 11
25
+ t.string "tagger_type"
26
+ end
27
+
28
+ add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
29
+ add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context"
30
+
31
+ create_table "tags", :force => true do |t|
32
+ t.string "name"
33
+ end
34
+
35
+ create_table :taggable_models, :force => true do |t|
36
+ t.column :name, :string
37
+ t.column :type, :string
38
+ t.column :cached_tag_list, :string
39
+ end
40
+ end
41
+
42
+ class TaggableModel < ActiveRecord::Base
43
+ acts_as_taggable
44
+ acts_as_taggable_on :languages
45
+ acts_as_taggable_on :skills
46
+ acts_as_taggable_on :needs, :offerings
47
+ end
48
+ end
49
+
50
+ puts Benchmark.measure {
51
+ 1000.times { TaggableModel.create :tag_list => "awesome, epic, neat" }
52
+ }
data/spec/database.yml ADDED
@@ -0,0 +1,10 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ database: acts_as_taggable_on.sqlite3
4
+
5
+ mysql:
6
+ adapter: mysql
7
+ hostname: localhost
8
+ username: root
9
+ database: acts_as_aggregate_root
10
+ socket: /tmp/mysql.sock