Class: Preval::Node
Defined Under Namespace
Classes: TypeMatch
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#literal ⇒ Object
readonly
Returns the value of attribute literal.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #[](index, *args) ⇒ Object
-
#initialize(type, body, literal = false) ⇒ Node
constructor
A new instance of Node.
- #is?(other) ⇒ Boolean
- #join(delim = '') ⇒ Object
- #replace(node) ⇒ Object
- #source(index) ⇒ Object
- #starts_with?(type) ⇒ Boolean
- #to_source ⇒ Object
- #type_match?(*types) ⇒ Boolean
- #update(type, body) ⇒ Object
- #visit(pass) ⇒ Object
Methods included from Format
Constructor Details
#initialize(type, body, literal = false) ⇒ Node
Returns a new instance of Node.
28 29 30 31 32 |
# File 'lib/preval/node.rb', line 28 def initialize(type, body, literal = false) @type = type @body = body @literal = literal end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
26 27 28 |
# File 'lib/preval/node.rb', line 26 def body @body end |
#literal ⇒ Object (readonly)
Returns the value of attribute literal.
26 27 28 |
# File 'lib/preval/node.rb', line 26 def literal @literal end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
26 27 28 |
# File 'lib/preval/node.rb', line 26 def type @type end |
Instance Method Details
#[](index, *args) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/preval/node.rb', line 34 def [](index, *args) node = body[index] return nil unless node args.any? ? node[*args] : node end |
#is?(other) ⇒ Boolean
45 46 47 |
# File 'lib/preval/node.rb', line 45 def is?(other) type == other end |
#join(delim = '') ⇒ Object
41 42 43 |
# File 'lib/preval/node.rb', line 41 def join(delim = '') body.map(&:to_source).join(delim) end |
#replace(node) ⇒ Object
49 50 51 52 53 |
# File 'lib/preval/node.rb', line 49 def replace(node) @type = node.type @body = node.body @literal = node.literal end |
#source(index) ⇒ Object
55 56 57 |
# File 'lib/preval/node.rb', line 55 def source(index) body[index].to_source end |
#starts_with?(type) ⇒ Boolean
59 60 61 |
# File 'lib/preval/node.rb', line 59 def starts_with?(type) body[0].is?(type) end |
#to_source ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/preval/node.rb', line 63 def to_source return body if literal begin public_send(:"to_#{type}") rescue NoMethodError raise NotImplementedError, "#{type} has not yet been implemented" end end |
#type_match?(*types) ⇒ Boolean
73 74 75 |
# File 'lib/preval/node.rb', line 73 def type_match?(*types) TypeMatch.new(types).match?(self) end |
#update(type, body) ⇒ Object
77 78 79 80 81 |
# File 'lib/preval/node.rb', line 77 def update(type, body) @type = type @body = body @literal = type.to_s.start_with?('@') end |
#visit(pass) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/preval/node.rb', line 83 def visit(pass) return if literal handler = :"on_#{type}" pass.public_send(handler, self) if pass.respond_to?(handler) return unless body.is_a?(Array) body.each do |child| child.visit(pass) if child.is_a?(Node) end end |