Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Update NodeList and NodeList test
- Loading branch information
Showing
2 changed files
with
50 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,47 @@ | ||
module Papyrus | ||
# A CommandBlock (not to be confused with a BlockCommand) provides a single | ||
# interface to multiple Command objects. As it is a Node, it has an output method. | ||
class CommandBlock < Node | ||
attr_reader :commands | ||
# A NodeList provides a single interface to multiple Node objects. As it is a | ||
# Node itself, it has an output method. | ||
class NodeList < Node | ||
attr_reader :nodes | ||
|
||
def initialize | ||
@commands = [] | ||
@nodes = [] | ||
end | ||
|
||
for meth in [ :length, :size, :first, :last, :empty? ] | ||
class_eval <<-EOT, __FILE__, __LINE__ | ||
def #{meth} | ||
@commands.send(:#{meth}) | ||
@nodes.send(:#{meth}) | ||
end | ||
EOT | ||
end | ||
def [](i) | ||
@commands[i] | ||
@nodes[i] | ||
end | ||
|
||
# Adds +command+ to the end of the Block's chain of Commands. | ||
# A TypeError is raised if the object being added is not a ((<Command>)). | ||
# Adds +node+ to the NodeList. | ||
# A TypeError is raised if the object being added is not a Node. | ||
# | ||
# This is also aliased as << | ||
def add(cmd) | ||
raise TypeError, 'Command::Block#add: Attempt to add non-Command object' unless cmd.kind_of?(Base) | ||
@commands << cmd | ||
def add(node) | ||
raise TypeError, 'NodeList#add: Attempt to add non-Node object' unless node.kind_of?(Node) | ||
@nodes << node | ||
self | ||
end | ||
def <<(cmd) | ||
add(cmd) | ||
def <<(node) | ||
add(node) | ||
end | ||
|
||
# Calls Command#output(context) on each Command contained in this | ||
# Calls Node#output(context) on each Node contained in this | ||
# object. The output is returned as a single string. If no output | ||
# is generated, returns an empty string. | ||
def output(context = nil) | ||
@commands.inject("") {|str, cmd| str << cmd.output(context) } | ||
@nodes.inject("") {|str, node| str << node.output(context) } | ||
end | ||
|
||
# Returns Commands held, as a string | ||
# Returns Nodes held, as a string | ||
def to_s | ||
'[ Blocks: ' + @commands.map {|cmd| "[#{cmd.to_s}]" }.join(' ') + ' ]' | ||
'[ NodeList: ' + @nodes.map {|node| "[#{node.to_s}]" }.join(' ') + ' ]' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters