lib/fathom/node.rb in fathom-0.2.3 vs lib/fathom/node.rb in fathom-0.3.0

- old
+ new

@@ -1,8 +1,8 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'fathom')) class Fathom::Node - + attr_reader :name, :distribution, :description, :values def initialize(opts={}) @name = opts[:name] assert_distribution(opts) @@ -20,36 +20,54 @@ @parents ||= [] end def add_parent(parent) self.parents << parent + self.add_accessor_for_node(parent) parent.register_child(self) end def register_child(child) raise "Cannot register a child if this node is not a parent already. Use add_parent to the other node or add_child to this node." unless child.parents.include?(self) - children << child unless children.include?(child) + unless children.include?(child) + self.add_accessor_for_node(child) + children << child + end true end def children @children ||= [] end def add_child(child) self.children << child + self.add_accessor_for_node(child) child.register_parent(self) end def register_parent(parent) raise "Cannot register a parent if this node is not a child already. Use add_child to the other node or add_parent to this node." unless parent.children.include?(self) - parents << parent unless parents.include?(parent) + unless parents.include?(parent) + self.add_accessor_for_node(parent) + parents << parent + end true end protected + + def add_accessor_for_node(node) + return false unless node.is_a?(Node) and node.name_sym + return false if self.respond_to?(node.name_sym) + (class << self; self; end).module_eval do + define_method node.name_sym do + node + end + end + end def assert_links(opts) found = opts[:parents] found ||= opts[:parent] found ||= []