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

- old
+ new

@@ -1,11 +1,20 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'fathom')) +# TODO: Move this into a proper configuration module +# require 'spira' +# @repository = RDF::Repository.new +# Spira.add_repository(:default, @repository) + class Fathom::Node + # See notes in the spec about this. + # include Spira::Resource + attr_reader :name, :distribution, :description, :values def initialize(opts={}) + symbolize_keys!(opts) @name = opts[:name] assert_distribution(opts) @description = opts[:description] @values = opts[:values] assert_links(opts) @@ -54,12 +63,35 @@ parents << parent end true end + def simple_inspect + self.name ? "#{self.name} (#{self.class.to_s})" : self.class.to_s + end + + def inspect + "#{self.class.to_s}: " + [ + self.name, + self.description, + "children:", + self.children.map {|e| e.simple_inspect }.inspect, + "parents: ", + self.parents.map {|e| e.simple_inspect }.inspect, + ].compact.join(", ") + end + protected + # Quick and dirty extract from ActiveSupport's same method + def symbolize_keys!(h) + h.keys.each do |key| + h[(key.to_sym rescue key) || key] = h.delete(key) + end + h + end + 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 @@ -74,10 +106,9 @@ found ||= [] found = [found] unless found.is_a?(Array) found.each do |parent| add_parent(parent) end - found = opts[:children] found ||= opts[:child] found ||= [] found = [found] unless found.is_a?(Array)