split node classes into separate files
This commit is contained in:
parent
b00f49ade6
commit
9492f29e66
@ -1,5 +1,7 @@
|
|||||||
require_relative "sof/util"
|
require_relative "sof/util"
|
||||||
require_relative "sof/node"
|
require_relative "sof/node"
|
||||||
|
require_relative "sof/simple_node"
|
||||||
|
require_relative "sof/object_node"
|
||||||
require_relative "sof/members"
|
require_relative "sof/members"
|
||||||
require_relative "sof/volotile"
|
require_relative "sof/volotile"
|
||||||
require_relative "sof/writer"
|
require_relative "sof/writer"
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# We transform objects into a tree of nodes
|
# We transform objects into a tree of nodes
|
||||||
|
|
||||||
module Sof
|
module Sof
|
||||||
#abstract base class for nodes in the tree
|
# abstract base class for nodes in the tree
|
||||||
# may be referenced (should be a simple name or number)
|
# may be referenced (should be a simple name or number)
|
||||||
class Node
|
class Node
|
||||||
include Util
|
include Util
|
||||||
@ -19,40 +19,4 @@ module Sof
|
|||||||
end
|
end
|
||||||
attr_reader :referenced
|
attr_reader :referenced
|
||||||
end
|
end
|
||||||
|
|
||||||
class SimpleNode < Node
|
|
||||||
def initialize data , ref = nil
|
|
||||||
super(ref)
|
|
||||||
@data = data
|
|
||||||
end
|
|
||||||
attr_reader :data
|
|
||||||
def out io , level
|
|
||||||
super(io,level)
|
|
||||||
io.write(data)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class ObjectNode < Node
|
|
||||||
def initialize data , ref
|
|
||||||
super(ref)
|
|
||||||
@data = data
|
|
||||||
@children = []
|
|
||||||
end
|
|
||||||
attr_reader :children , :data
|
|
||||||
def add k , v
|
|
||||||
@children << [k,v]
|
|
||||||
end
|
|
||||||
def out io , level = 0
|
|
||||||
super
|
|
||||||
io.write(@data)
|
|
||||||
indent = " " * (level + 1)
|
|
||||||
@children.each_with_index do |child , i|
|
|
||||||
k , v = child
|
|
||||||
io.write "\n#{indent}"
|
|
||||||
k.out(io , level + 2)
|
|
||||||
io.write " "
|
|
||||||
v.out(io , level + 2)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
30
lib/sof/object_node.rb
Normal file
30
lib/sof/object_node.rb
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
|
||||||
|
module Sof
|
||||||
|
|
||||||
|
# ObjectNode means node with structure
|
||||||
|
# ie arrays and hashes get transformed into these too
|
||||||
|
|
||||||
|
class ObjectNode < Node
|
||||||
|
def initialize data , ref
|
||||||
|
super(ref)
|
||||||
|
@data = data
|
||||||
|
@children = []
|
||||||
|
end
|
||||||
|
attr_reader :children , :data
|
||||||
|
def add k , v
|
||||||
|
@children << [k,v]
|
||||||
|
end
|
||||||
|
def out io , level = 0
|
||||||
|
super
|
||||||
|
io.write(@data)
|
||||||
|
indent = " " * (level + 1)
|
||||||
|
@children.each_with_index do |child , i|
|
||||||
|
k , v = child
|
||||||
|
io.write "\n#{indent}"
|
||||||
|
k.out(io , level + 2)
|
||||||
|
io.write " "
|
||||||
|
v.out(io , level + 2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
19
lib/sof/simple_node.rb
Normal file
19
lib/sof/simple_node.rb
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# We transform objects into a tree of nodes
|
||||||
|
|
||||||
|
module Sof
|
||||||
|
|
||||||
|
# What makes a node simple is that it has no further structure
|
||||||
|
#
|
||||||
|
class SimpleNode < Node
|
||||||
|
def initialize data , ref = nil
|
||||||
|
super(ref)
|
||||||
|
@data = data
|
||||||
|
end
|
||||||
|
attr_reader :data
|
||||||
|
def out io , level
|
||||||
|
super(io,level)
|
||||||
|
io.write(data)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user