Sha256: effcded80cee16c635ac0edf42ffb09122e35c985cca39cdc51051198bd37d71

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

class NamespaceNode
  attr_reader :name, :children
  def initialize(name)
    @name = name
    @children = { }
    @objects = { }
  end

  def << (name)
    add_child(name)
  end

  def add_child(name)
    if @objects.has_key? name
      raise Exception.new("Node #{@name} already has object with name #{name}")
    end
    if not @children.has_key? name
      @children[name] = NamespaceNode.new(name)
      self.metaclass.send :define_method, name do
        @children[name]
      end
    end
    @children[name]
  end

  def add_object(name, object, *namespaces)
    raise Exception.new("Node #{@name} already has child node with name #{name}") if @children.has_key? name
    raise Exception.new("Node #{@name} already has object with name #{name}") if @objects.has_key? name

    if namespaces.length > 0
      next_namespace = namespaces.shift.to_s
      raise Exception.new("No namespace with name #{next_namespace} found") if not @children.has_key?(next_namespace)
      @children[next_namespace].add_object(name, object, *namespaces)
    else
      @objects[name] = object
      self.metaclass.send :define_method, name do
        @objects[name]
      end
    end
    
    self
  end

  def metaclass
    class << self
      self
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ironbuildrake-0.1.1-ironruby lib/namespace_node.rb
ironbuildrake-0.1.0 lib/namespace_node.rb