Sha256: 4419885d4c75222c33891b8b0c12598b5d6976758b85beac491bcfe971e7d623

Contents?: true

Size: 1.07 KB

Versions: 5

Compression:

Stored size: 1.07 KB

Contents

require 'yaml'

module TreeMacros

  def setup_tree(model_class,tree)
    create_tree(model_class,YAML.load(tree))
  end

  def nodes(name)
    @nodes[name].reload
  end

  def print_tree(node, inspect = false, depth = 0)
    print '  ' * depth
    print '- ' unless depth == 0
    print node.title
    print " (#{node.inspect})" if inspect
    print ':' if node.children.any?
    print "\n"
    node.children.each { |c| print_tree(c, inspect, depth + 1) }
  end

private
  def create_tree(model_class,object)
    case object
      when String then return create_node(model_class,object)
      when Array then object.each { |tree| create_tree(model_class,tree) }
      when Hash then
        name, children = object.first
        node = create_node(model_class,name)
        children.each { |c| node.children << create_tree(model_class,c) }
        return node
    end
  end

  def create_node(model_class,name)
    @nodes ||= HashWithIndifferentAccess.new
    @nodes[name] = model_class.create(:title => name , :slug => name)
  end
end

RSpec.configure do |config|
  config.include TreeMacros
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
amalgam-2.1.4 spec/support/tree_macro.rb
amalgam-2.1.3.1 spec/support/tree_macro.rb
amalgam-2.1.3 spec/support/tree_macro.rb
amalgam-2.1.2 spec/support/tree_macro.rb
amalgam-2.1.1 spec/support/tree_macro.rb