Composite version 0.3.0


Mixin module to create classes where the 'whole' object is seen as being composed of 'parts'. Allowing access to some object both as composed as well as in parts. The typical use case is a subclass of String that is used to deal with a string that has a certain format and supports accessor methods to its parts, for example an URI/IRI, a BCP 47 language tag, a name or address in a certain format, and so on.


class myComposite < BaseClass
  def initialize (n)
  part :part1, :part2, ...
  def compose
    # calculate a value for the BaseClass object
    # from @part1, @part2,...
  def decompose
    # calculate @part1, @part2,...
    # from the value of BaseClass

The above provides methods part1, part1=, part2, part2=,… for access to parts.


decompose is called on each access (both read and write) to any of the parts, because it's impossible to predict when the BaseClass object changes (all methods of the BaseClass would need to be patched). If decompose takes time, it may be possible to speed things up by saving a copy of the BaseClass object, or its hash, internally and do a comparison to see if anything has changed.


Copyright © 2007 Martin J. Du“rst ( Licensed under the same terms as Ruby. Absolutely no warranty. (see