2015-09-18 15:12:37 -04:00
|
|
|
class LocaleFileWalker
|
|
|
|
protected
|
|
|
|
|
2017-02-24 05:35:33 -05:00
|
|
|
def handle_stream(stream)
|
|
|
|
stream.children.each { |document| handle_document(document) }
|
|
|
|
end
|
|
|
|
|
2015-09-18 15:12:37 -04:00
|
|
|
def handle_document(document)
|
2017-02-24 05:35:33 -05:00
|
|
|
# we want to ignore the locale (first key), so let's start at -1
|
2015-09-18 15:12:37 -04:00
|
|
|
handle_nodes(document.root.children, -1, [])
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_nodes(nodes, depth, parents)
|
2017-02-24 05:35:33 -05:00
|
|
|
return unless nodes
|
|
|
|
consecutive_scalars = 0
|
|
|
|
nodes.each do |node|
|
|
|
|
consecutive_scalars = handle_node(node, depth, parents, consecutive_scalars)
|
2015-09-18 15:12:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_node(node, depth, parents, consecutive_scalars)
|
2017-02-24 05:35:33 -05:00
|
|
|
if node_is_scalar = node.is_a?(Psych::Nodes::Scalar)
|
|
|
|
valid_scalar?(depth, consecutive_scalars) ? handle_scalar(node, depth, parents) : handle_value(node.value, parents)
|
2015-09-18 15:12:37 -04:00
|
|
|
elsif node.is_a?(Psych::Nodes::Alias)
|
|
|
|
handle_alias(node, depth, parents)
|
|
|
|
elsif node.is_a?(Psych::Nodes::Mapping)
|
|
|
|
handle_mapping(node, depth, parents)
|
|
|
|
handle_nodes(node.children, depth + 1, parents.dup)
|
|
|
|
end
|
|
|
|
|
|
|
|
node_is_scalar ? consecutive_scalars + 1 : 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_scalar?(depth, consecutive_scalars)
|
|
|
|
depth >= 0 && consecutive_scalars.even?
|
|
|
|
end
|
|
|
|
|
2017-02-24 05:35:33 -05:00
|
|
|
def handle_value(value, parents)
|
|
|
|
end
|
|
|
|
|
2015-09-18 15:12:37 -04:00
|
|
|
def handle_scalar(node, depth, parents)
|
|
|
|
parents[depth] = node.value
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_alias(node, depth, parents)
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_mapping(node, depth, parents)
|
|
|
|
end
|
|
|
|
end
|