kinda-composable 0.0.2

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 ADDED
@@ -0,0 +1,14 @@
1
+ Author:: Manuel Vila (mailto:mvila@3base.com)
2
+ License:: Don't know yet
3
+
4
+ = Composable
5
+
6
+ Simple implementation of the composite design pattern.
7
+
8
+ == Installation
9
+
10
+ $ sudo gem install kinda-composable --source http://gems.github.com
11
+
12
+ == Usage
13
+
14
+ Still very alpha so documentation will come later.
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')
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kinda-composable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Manuel Vila
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Simple implementation of the composite design pattern
17
+ email: mvila@3base.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - lib/composable.rb
26
+ - lib/kinda-composable.rb
27
+ - README.rdoc
28
+ has_rdoc: true
29
+ homepage: http://github.com/kinda/composable
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project: kinda-composable
50
+ rubygems_version: 1.2.0
51
+ signing_key:
52
+ specification_version: 2
53
+ summary: Simple implementation of the composite design pattern
54
+ test_files: []
55
+