composable 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +11 -0
- data/Rakefile +20 -0
- data/lib/composable.rb +63 -0
- data/lib/kinda-composable.rb +1 -0
- data/test/composable_test.rb +90 -0
- metadata +67 -0
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "rake/testtask"
|
2
|
+
|
3
|
+
begin
|
4
|
+
require "hanna/rdoctask"
|
5
|
+
rescue LoadError
|
6
|
+
require "rake/rdoctask"
|
7
|
+
end
|
8
|
+
|
9
|
+
task :default => [:test]
|
10
|
+
|
11
|
+
Rake::TestTask.new do |t|
|
12
|
+
t.test_files = Dir["test/*_test.rb"]
|
13
|
+
end
|
14
|
+
|
15
|
+
Rake::RDocTask.new do |t|
|
16
|
+
t.title = "Composable Documentation"
|
17
|
+
t.main = "README.rdoc"
|
18
|
+
t.rdoc_files.include("README.rdoc", "lib/**/*.rb")
|
19
|
+
t.rdoc_dir = "doc"
|
20
|
+
end
|
data/lib/composable.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'kinda-core'
|
3
|
+
|
4
|
+
module Kinda
|
5
|
+
module Composable
|
6
|
+
include Core
|
7
|
+
|
8
|
+
def add_child(child)
|
9
|
+
child.parent = self
|
10
|
+
child
|
11
|
+
end
|
12
|
+
|
13
|
+
alias_method :<<, :add_child
|
14
|
+
|
15
|
+
def remove_child(child)
|
16
|
+
child.parent = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
def parent(klass=nil)
|
20
|
+
result = @parent
|
21
|
+
if klass
|
22
|
+
until result.nil? || result.kind_of?(klass) do
|
23
|
+
result = result.parent
|
24
|
+
end
|
25
|
+
end
|
26
|
+
result
|
27
|
+
end
|
28
|
+
|
29
|
+
def parent=(new_parent)
|
30
|
+
if respond_to?(:fire)
|
31
|
+
fire(:parent) { __parent_equal__(new_parent) }
|
32
|
+
else
|
33
|
+
__parent_equal__(new_parent)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def __parent_equal__(new_parent)
|
38
|
+
if old_parent = @parent
|
39
|
+
old_parent.children.delete(self)
|
40
|
+
old_parent.try(:child_removed, self)
|
41
|
+
end
|
42
|
+
|
43
|
+
@parent = new_parent
|
44
|
+
|
45
|
+
if new_parent
|
46
|
+
new_parent.children << self
|
47
|
+
new_parent.try(:child_added, self)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def child(klass=nil)
|
52
|
+
children.find { |child| klass.nil? || child.kind_of?(klass) }
|
53
|
+
end
|
54
|
+
|
55
|
+
attr_reader :children do
|
56
|
+
@children ||= []
|
57
|
+
end
|
58
|
+
|
59
|
+
def children?
|
60
|
+
!children.empty?
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'composable')
|
@@ -0,0 +1,90 @@
|
|
1
|
+
#!/usr/bin/ruby19
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')).uniq!
|
7
|
+
require 'kinda-composable'
|
8
|
+
|
9
|
+
class TestComposable < Test::Unit::TestCase
|
10
|
+
def setup
|
11
|
+
@grand_parent_class = Class.new do
|
12
|
+
include Kinda::Composable
|
13
|
+
end
|
14
|
+
|
15
|
+
@parent_class = Class.new do
|
16
|
+
include Kinda::Composable
|
17
|
+
end
|
18
|
+
|
19
|
+
@child_class = Class.new do
|
20
|
+
include Kinda::Composable
|
21
|
+
end
|
22
|
+
|
23
|
+
@grand_parent = @grand_parent_class.new
|
24
|
+
@parent = @parent_class.new
|
25
|
+
@parent2 = @parent_class.new
|
26
|
+
@child = @child_class.new
|
27
|
+
@child2 = @child_class.new
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_add_child
|
31
|
+
@parent.add_child(@child)
|
32
|
+
assert @parent.children.size == 1
|
33
|
+
assert @parent.children.first == @child
|
34
|
+
assert @child.parent == @parent
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_add_children
|
38
|
+
@parent.add_child(@child)
|
39
|
+
assert @parent.children.size == 1
|
40
|
+
@parent.add_child(@child) # same child added twice
|
41
|
+
assert @parent.children.size == 1
|
42
|
+
@parent.add_child(@child2)
|
43
|
+
assert @parent.children.size == 2
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_children?
|
47
|
+
assert !@parent.children?
|
48
|
+
@parent.add_child(@child)
|
49
|
+
assert @parent.children?
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_remove_children
|
53
|
+
@parent.add_child(@child)
|
54
|
+
@parent.add_child(@child2)
|
55
|
+
@parent.remove_child(@child)
|
56
|
+
assert @child.parent == nil
|
57
|
+
assert @parent.children.first == @child2
|
58
|
+
assert @child2.parent == @parent
|
59
|
+
@parent.remove_child(@child2)
|
60
|
+
assert @child2.parent == nil
|
61
|
+
assert !@parent.children?
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_set_parent
|
65
|
+
@child.parent = @parent
|
66
|
+
assert @child.parent == @parent
|
67
|
+
assert @parent.children.size == 1
|
68
|
+
assert @parent.children.first == @child
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_reset_parent
|
72
|
+
@child.parent = @parent
|
73
|
+
assert @child.parent == @parent
|
74
|
+
@child.parent = @parent2
|
75
|
+
assert !@parent.children?
|
76
|
+
assert @child.parent == @parent2
|
77
|
+
assert @parent2.children.size == 1
|
78
|
+
assert @parent2.children.first == @child
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_parent_search
|
82
|
+
@grand_parent.add_child(@parent)
|
83
|
+
@parent.add_child(@child)
|
84
|
+
assert @child.parent == @parent
|
85
|
+
assert @child.parent(@parent_class) == @parent
|
86
|
+
assert @child.parent(@grand_parent_class) == @grand_parent
|
87
|
+
assert @parent.parent(@child_class) == nil
|
88
|
+
assert @grand_parent.parent(Object) == nil
|
89
|
+
end
|
90
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: composable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.9
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Manuel Vila
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-12 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: kinda-core
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.1
|
24
|
+
version:
|
25
|
+
description: Simple implementation of the composite design pattern
|
26
|
+
email: mvila@3base.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.rdoc
|
33
|
+
files:
|
34
|
+
- Rakefile
|
35
|
+
- lib/composable.rb
|
36
|
+
- lib/kinda-composable.rb
|
37
|
+
- README.rdoc
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/kinda/composable
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: 1.9.1
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project: kinda-composable
|
62
|
+
rubygems_version: 1.3.5
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Simple implementation of the composite design pattern
|
66
|
+
test_files:
|
67
|
+
- test/composable_test.rb
|