is_a_collection 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ class Class
2
+
3
+ def is_a_collection(id_method=:id)
4
+ id_method = id_method.to_sym
5
+
6
+ include InstanceMethods
7
+ extend ClassMethods
8
+
9
+ define_method :add_to_collection do
10
+ self.class.__collection[send id_method] = self
11
+ end
12
+ define_method :remove_from_collection do
13
+ self.class.__collection.delete(send id_method)
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+ def initialize(*args, &block)
19
+ super
20
+ add_to_collection
21
+ end
22
+ def destroy(*args, &block)
23
+ remove_from_collection
24
+ super if self.class.superclass.instance_methods.include?(:destroy)
25
+ end
26
+ end
27
+
28
+ module ClassMethods
29
+ def __collection
30
+ @__collection ||= {}
31
+ end
32
+ def all
33
+ __collection.values
34
+ end
35
+ def find(identifier)
36
+ @__collection[identifier]
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,61 @@
1
+ require File.join( File.expand_path(File.dirname(__FILE__)), '../lib/is_a_collection' )
2
+
3
+ class A
4
+ is_a_collection
5
+
6
+ def initialize(*args)
7
+ super
8
+ end
9
+
10
+ def id
11
+ object_id
12
+ end
13
+
14
+ end
15
+
16
+ class B
17
+ attr_reader :name
18
+ is_a_collection :name
19
+
20
+ def initialize(name)
21
+ @name = name
22
+ add_to_collection
23
+ end
24
+
25
+ end
26
+
27
+ describe "is_a_collection" do
28
+ describe "#all" do
29
+ describe "without any instances" do
30
+ it "should be empty" do
31
+ A.all.should be_empty
32
+ end
33
+ end
34
+ describe "with some instances" do
35
+ it "should return these instances" do
36
+ a1 = A.new
37
+ a2 = A.new
38
+ A.all.should == [a1,a2]
39
+ end
40
+ end
41
+ end
42
+ describe "#find" do
43
+ it "should return the instance by default identifier (#object_id)" do
44
+ a = A.new
45
+ A.find(a.object_id).should == a
46
+ end
47
+ it "should return the instance by custom identifier" do
48
+ b = B.new('foobar')
49
+ B.find('foobar').should == b
50
+ end
51
+ end
52
+ describe "#destroy" do
53
+ before(:each) do
54
+ @b = B.new('doomed')
55
+ end
56
+ it "should remove the instances" do
57
+ @b.destroy
58
+ B.find('doomed').should be_nil
59
+ end
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: is_a_collection
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Niko Dittmann
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-09 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ description: "A small gem that adds #find, #all and #destroy to a class to keep track of its instances."
34
+ email: mail@niko-dittmann.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - lib/is_a_collection.rb
43
+ - spec/is_a_collection_spec.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/niko/is_a_collection
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project: nowarning
72
+ rubygems_version: 1.3.7
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: "A small gem that adds #find, #all and #destroy to a class to keep track of its instances."
76
+ test_files:
77
+ - spec/is_a_collection_spec.rb