lib/abstract_mapper/branch.rb in abstract_mapper-0.0.1 vs lib/abstract_mapper/branch.rb in abstract_mapper-0.0.2
- old
+ new
@@ -4,11 +4,11 @@
# A special type of the composed node, that describes transformation,
# applied to some level of nested input.
#
# Unlike the simple node, describing a transformation of data, the
- # branch carries a collection of subnodes along with methods to [#rebuild]
+ # branch carries a collection of subnodes along with methods to [#update]
# itself with the same attributes and different subnodes.
#
# Tne branch only stores subnodes and composes transformations.
# Its has no access to DSL and knows neither how to build a tree
# (see [AbstractMapper::Builder]), nor how to optimize it later
@@ -30,33 +30,33 @@
# The block that returns an array of subnodes for the branch
#
# @return [Branch::Node]
# @private
- def initialize(*attributes)
+ def initialize(attributes = {})
@subnodes = block_given? ? yield : []
- super(*attributes, &nil)
+ super(attributes, &nil)
end
# Returns a new branch of the same type, with the same attributes,
# but with a different collection of subnodes, transmitted by the block.
#
# @example
# branch = Branch.new(:foo)
# # => <Branch(:foo) []>
- # branch.rebuild { Node.new(:bar) }
+ # branch.update { Node.new(:bar) }
# # => <Branch(:foo) [<Node(:bar)>]>
#
# @param [Proc] block
# The block that should return an array of subnodes for the branch
#
# @return [AbstractMapper::Branch]
#
# @yield block
#
- def rebuild(&block)
- self.class.new(*attributes, &block)
+ def update
+ self.class.new(attributes) { yield }
end
# @!method each
# Returns the enumerator for subnodes
#
@@ -71,11 +71,11 @@
# @param [AbstractMapper::Node] other
#
# @return [AbstractMapper::Branch]
#
def <<(other)
- rebuild { entries << other }
+ update { entries << other }
end
# The composition of transformations from all subnodes of the branch
#
# To be reloaded by the subclasses to apply the composition to
@@ -93,9 +93,19 @@
#
# @return [String]
#
def to_s
"#{super} [#{map(&:inspect).join(", ")}]"
+ end
+
+ # Checks equality of branches by type, attributes and subnodes
+ #
+ # @param [Other] other
+ #
+ # @return [Boolean]
+ #
+ def eql?(other)
+ super && entries.eql?(other.entries)
end
private
# Substitutes the name of the class by the special name "Root"