Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Mix-in to support Composite and Visitor patterns
Ruby
branch: master
Failed to load latest commit information.
lib
script
spec
.gitignore
History.txt
Manifest.txt
PostInstall.txt
README.rdoc
Rakefile
composite-ruby.gemspec

README.rdoc

composite-ruby

github.com/fredgeorge/composite-ruby

DESCRIPTION:

These class(es) ease the ability to implement a Composite pattern [GoF] and a supporting Visitor pattern [GoF]. Pattern::Composite extends Enumerable, allowing rich iteration across the children of the Composite. Composite patterns are useful anywhere there is a hierarchy or tree. Examples include organization structures, parse trees for expressions, and anytime a collection of a collection (of a collection, etc.) occurs.

require 'composite-ruby'
class MyClass
  include Pattern::Composite
  ...
end

Mixed-in methods include:

<<(child)             - add a sub-component
add(child)            - alias for <<
each                  - iterating through children
total(:accessor)      - sum the values of the specified accessor of the leaves
aggregate(:accessor)  - return an array of all elements of the specified accessor of the leaves
collecting(:accessor) - return a merged collection of all the collections from the leaves

In addition, recursive accessors can be created. For example for totaling the values in leaves:

class MyClass
  include Pattern::Composite
  recursive_total :var1, :var2, ...
  ...
end

This creates recursive methods that return the total all the values of var1 in the leaves. See the RSpec test, spec/unbalanced_composite_spec.rb, for a comprehensive example of usage.

FEATURES/PROBLEMS:

Support for Composite pattern, including generating methods for leaves for:

totals:                     recursive_total :var1, :var2, :method1, :method2
aggregating value answers:  recursive_values :var3, :var4, :method3, :method4
collecting array answers:   recursive_collecting :var5, :var6, :method5, :method6

No support of Visitor pattern (yet)

SYNOPSIS:

require 'composite-ruby'
class MyClass
  include Pattern::Composite
  recursive_total :to_i, :to_f
  ...
end

x = MyClass.new 3, 4
x << 5
x.to_i    # => 12

REQUIREMENTS:

RSpec (if you want to run the tests)

INSTALL:

sudo gem install composite-ruby

ACKNOWLEDGEMENTS

The path here was hardly direct. Key contributors to the journey were:

  • Erich Gamma and the rest of the Gang of Four [GoF] for Design Patterns

  • Eric Gold for beating me about the head with Smalltalk and patterns

  • Matz (Yukihiro Matsumoto) for inventing a cool language Ruby

  • Dave (Pragmatic) Thomas for talking about it

  • Obie Fernandez for bullying Ruby into ThoughtWorks

  • Roy Singham for listening to Obie

  • Neil Hutchinson (TrafficBroker) for creating an environment championing innovation

LICENSE:

(The MIT License)

Copyright © 2009 Fred George

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.