Class: Moxml::XPath::Ruby::Node
- Inherits:
- BasicObject
- Defined in:
- lib/moxml/xpath/ruby/node.rb
Overview
Class representing a single node in a Ruby AST.
This class provides a fluent DSL for building Ruby code dynamically. It’s modeled after the “ast” gem but simplified to avoid method conflicts.
Instance Attribute Summary collapse
- #type ⇒ Symbol readonly
Instance Method Summary collapse
-
#add_block(*args) ⇒ Moxml::XPath::Ruby::Node
Wraps the current node in a block.
-
#and(other) ⇒ Moxml::XPath::Ruby::Node
Returns a boolean “and” node.
-
#assign(other) ⇒ Moxml::XPath::Ruby::Node
Returns an assignment node.
-
#else ⇒ Moxml::XPath::Ruby::Node
Adds an “else” statement to the current node.
-
#eq(other) ⇒ Moxml::XPath::Ruby::Node
Returns an equality expression node.
-
#followed_by(other = nil) ⇒ Moxml::XPath::Ruby::Node
Chains two nodes together.
-
#if_false(&block) ⇒ Object
Wraps the current node in an ‘if !…` statement.
-
#if_true ⇒ Moxml::XPath::Ruby::Node
Wraps the current node in an if statement node.
-
#initialize(type, children = []) ⇒ Node
constructor
A new instance of Node.
- #inspect ⇒ String
-
#is_a?(klass) ⇒ Moxml::XPath::Ruby::Node
Returns a node for Ruby’s “is_a?” method.
-
#method_missing(name, *args) ⇒ Moxml::XPath::Ruby::Node
Returns a node for a method call.
-
#not ⇒ Moxml::XPath::Ruby::Node
Returns a node that evaluates to its inverse.
-
#or(other) ⇒ Moxml::XPath::Ruby::Node
Returns a boolean “or” node.
- #to_a ⇒ Array (also: #to_ary)
-
#to_array ⇒ Moxml::XPath::Ruby::Node
Returns a “to_a” call node.
-
#to_str ⇒ Object
Prevent implicit string conversion - Nodes must be explicitly processed.
-
#while_true ⇒ Moxml::XPath::Ruby::Node
Wraps the current node in a ‘while` statement.
-
#wrap ⇒ Moxml::XPath::Ruby::Node
Wraps the current node in a ‘begin` node.
Constructor Details
#initialize(type, children = []) ⇒ Node
Returns a new instance of Node.
28 29 30 31 |
# File 'lib/moxml/xpath/ruby/node.rb', line 28 def initialize(type, children = []) @type = type.to_sym @children = children end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Moxml::XPath::Ruby::Node
Returns a node for a method call.
167 168 169 |
# File 'lib/moxml/xpath/ruby/node.rb', line 167 def method_missing(name, *args) Node.new(:send, [self, name.to_s, *args]) end |
Instance Attribute Details
#type ⇒ Symbol (readonly)
24 25 26 |
# File 'lib/moxml/xpath/ruby/node.rb', line 24 def type @type end |
Instance Method Details
#add_block(*args) ⇒ Moxml::XPath::Ruby::Node
Wraps the current node in a block.
105 106 107 |
# File 'lib/moxml/xpath/ruby/node.rb', line 105 def add_block(*args) Node.new(:block, [self, args, yield]) end |
#and(other) ⇒ Moxml::XPath::Ruby::Node
Returns a boolean “and” node.
71 72 73 |
# File 'lib/moxml/xpath/ruby/node.rb', line 71 def and(other) Node.new(:and, [self, other]) end |
#assign(other) ⇒ Moxml::XPath::Ruby::Node
Returns an assignment node.
Wraps assigned values in a begin/end block to ensure that multiple lines of code result in the proper value being assigned.
53 54 55 56 57 |
# File 'lib/moxml/xpath/ruby/node.rb', line 53 def assign(other) other = other.wrap if other.type == :followed_by Node.new(:assign, [self, other]) end |
#else ⇒ Moxml::XPath::Ruby::Node
Adds an “else” statement to the current node.
This method assumes it’s being called only on “if” nodes.
148 149 150 |
# File 'lib/moxml/xpath/ruby/node.rb', line 148 def else Node.new(:if, @children + [yield]) end |
#eq(other) ⇒ Moxml::XPath::Ruby::Node
Returns an equality expression node.
63 64 65 |
# File 'lib/moxml/xpath/ruby/node.rb', line 63 def eq(other) Node.new(:eq, [self, other]) end |
#followed_by(other = nil) ⇒ Moxml::XPath::Ruby::Node
Chains two nodes together.
156 157 158 159 160 |
# File 'lib/moxml/xpath/ruby/node.rb', line 156 def followed_by(other = nil) other = yield if ::Kernel.block_given? Node.new(:followed_by, [self, other]) end |
#if_false(&block) ⇒ Object
Wraps the current node in an ‘if !…` statement.
129 130 131 |
# File 'lib/moxml/xpath/ruby/node.rb', line 129 def if_false(&block) self.not.if_true(&block) end |
#if_true ⇒ Moxml::XPath::Ruby::Node
Wraps the current node in an if statement node.
The body of this statement is set to the return value of the supplied block.
122 123 124 |
# File 'lib/moxml/xpath/ruby/node.rb', line 122 def if_true Node.new(:if, [self, yield]) end |
#inspect ⇒ String
179 180 181 |
# File 'lib/moxml/xpath/ruby/node.rb', line 179 def inspect "(#{type} #{@children.map(&:inspect).join(' ')})" end |
#is_a?(klass) ⇒ Moxml::XPath::Ruby::Node
Returns a node for Ruby’s “is_a?” method.
97 98 99 |
# File 'lib/moxml/xpath/ruby/node.rb', line 97 def is_a?(klass) Node.new(:send, [self, "is_a?", klass]) end |
#not ⇒ Moxml::XPath::Ruby::Node
Returns a node that evaluates to its inverse.
89 90 91 |
# File 'lib/moxml/xpath/ruby/node.rb', line 89 def not !self end |
#or(other) ⇒ Moxml::XPath::Ruby::Node
Returns a boolean “or” node.
79 80 81 |
# File 'lib/moxml/xpath/ruby/node.rb', line 79 def or(other) Node.new(:or, [self, other]) end |
#to_a ⇒ Array Also known as: to_ary
34 35 36 |
# File 'lib/moxml/xpath/ruby/node.rb', line 34 def to_a @children end |
#to_array ⇒ Moxml::XPath::Ruby::Node
Returns a “to_a” call node.
42 43 44 |
# File 'lib/moxml/xpath/ruby/node.rb', line 42 def to_array Node.new(:send, [self, :to_a]) end |
#to_str ⇒ Object
Prevent implicit string conversion - Nodes must be explicitly processed
172 173 174 175 176 |
# File 'lib/moxml/xpath/ruby/node.rb', line 172 def to_str ::Kernel.raise ::TypeError, "Cannot implicitly convert #{self.class} to String. Use Generator#process instead." end |
#while_true ⇒ Moxml::XPath::Ruby::Node
Wraps the current node in a ‘while` statement.
The body of this statement is set to the return value of the supplied block.
139 140 141 |
# File 'lib/moxml/xpath/ruby/node.rb', line 139 def while_true Node.new(:while, [self, yield]) end |
#wrap ⇒ Moxml::XPath::Ruby::Node
Wraps the current node in a ‘begin` node.
112 113 114 |
# File 'lib/moxml/xpath/ruby/node.rb', line 112 def wrap Node.new(:begin, [self]) end |