Class: SoberSwag::Nodes::Base Abstract
- Inherits:
-
Object
- Object
- SoberSwag::Nodes::Base
- Includes:
- Comparable
- Defined in:
- lib/sober_swag/nodes/base.rb
Overview
Base Node that all other nodes inherit from. All nodes should define the following:
#deconstruct
, which returns an array of everything needed to identify the node. We base comparisons on the result of deconstruction.#deconstruct_keys
, which returns a hash of everything needed to identify the node. We use this later.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Value-level comparison.
-
#cata {|node| ... } ⇒ Object
Perform a catamorphism, or, a deep-first recursion.
-
#eql?(other) ⇒ Boolean
Is this object equal to the other object?.
-
#hash ⇒ Integer
Standard hash key.
-
#map ⇒ Object
Map over the inner, contained type of this node.
Instance Method Details
#<=>(other) ⇒ Integer
Value-level comparison.
Returns -1
, 0
, or +1
if this object is less than, equal to, or greater than other
.
25 26 27 28 29 |
# File 'lib/sober_swag/nodes/base.rb', line 25 def <=>(other) return other.class.name <=> self.class.name unless other.instance_of?(self.class) deconstruct <=> other.deconstruct end |
#cata {|node| ... } ⇒ Object
Perform a catamorphism, or, a deep-first recursion.
The basic way this works is deceptively simple: When you use 'cata' on a node, it will call the block you gave it with the deepest nodes in the tree first. It will then use the result of that block to reconstruct their parent node, and then call cata again on the parent, and so on until we reach the top.
When working with these definition nodes, we very often want to transform something recursively. This method allows us to do so by focusing on a single level at a time, keeping the actual recursion abstract.
60 61 62 |
# File 'lib/sober_swag/nodes/base.rb', line 60 def cata raise ArgumentError, 'Base is abstract' end |
#eql?(other) ⇒ Boolean
Is this object equal to the other object?
35 36 37 |
# File 'lib/sober_swag/nodes/base.rb', line 35 def eql?(other) deconstruct == other.deconstruct end |
#hash ⇒ Integer
Standard hash key.
42 43 44 |
# File 'lib/sober_swag/nodes/base.rb', line 42 def hash deconstruct.hash end |
#map ⇒ Object
Map over the inner, contained type of this node.
This will only map over values wrapped in a SoberSwag::Nodes::Primitive
object.
Unlike #cata, it does not transform the entire node tree.
68 69 70 |
# File 'lib/sober_swag/nodes/base.rb', line 68 def map raise ArgumentError, 'Base is abstract' end |