Class: RedQuilt::NodeRef
- Inherits:
-
Object
- Object
- RedQuilt::NodeRef
- Includes:
- Enumerable
- Defined in:
- lib/red_quilt/node_ref.rb
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#node_id ⇒ Object
readonly
Returns the value of attribute node_id.
Instance Method Summary collapse
- #children ⇒ Object
- #each ⇒ Object
- #find_all(type) ⇒ Object
-
#initialize(document, node_id) ⇒ NodeRef
constructor
A new instance of NodeRef.
- #source_location ⇒ Object
- #source_span ⇒ Object
- #text ⇒ Object
- #to_h ⇒ Object
- #type ⇒ Object
- #walk {|_self| ... } ⇒ Object
Constructor Details
#initialize(document, node_id) ⇒ NodeRef
Returns a new instance of NodeRef.
9 10 11 12 13 |
# File 'lib/red_quilt/node_ref.rb', line 9 def initialize(document, node_id) @document = document @arena = document.arena @node_id = node_id end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
7 8 9 |
# File 'lib/red_quilt/node_ref.rb', line 7 def document @document end |
#node_id ⇒ Object (readonly)
Returns the value of attribute node_id.
7 8 9 |
# File 'lib/red_quilt/node_ref.rb', line 7 def node_id @node_id end |
Instance Method Details
#children ⇒ Object
23 24 25 |
# File 'lib/red_quilt/node_ref.rb', line 23 def children @arena.child_ids(@node_id).map { |child_id| NodeRef.new(@document, child_id) } end |
#each ⇒ Object
15 16 17 |
# File 'lib/red_quilt/node_ref.rb', line 15 def each(&) walk(&) end |
#find_all(type) ⇒ Object
53 54 55 |
# File 'lib/red_quilt/node_ref.rb', line 53 def find_all(type) walk.select { |node| node.type == type } end |
#source_location ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/red_quilt/node_ref.rb', line 57 def source_location span = source_span return nil unless span start_loc = @document.source_map.line_column(span.start_byte) end_loc = @document.source_map.line_column(span.end_byte) { start_line: start_loc[:line], start_column: start_loc[:column], end_line: end_loc[:line], end_column: end_loc[:column], } end |
#source_span ⇒ Object
49 50 51 |
# File 'lib/red_quilt/node_ref.rb', line 49 def source_span @arena.source_span(@node_id) end |
#text ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/red_quilt/node_ref.rb', line 36 def text first_child_id = @arena.raw_first_child_id(@node_id) return @arena.text(@node_id) if first_child_id == -1 text = +"" @arena.child_ids(@node_id).each do |child_id| child = NodeRef.new(@document, child_id) fragment = child.text text << fragment.to_s unless fragment.nil? end text end |
#to_h ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/red_quilt/node_ref.rb', line 72 def to_h ast = { type: type, source_span: source_span, children: children.map(&:to_h), } attributes = ast_attributes ast[:attributes] = attributes unless attributes.empty? ast end |
#type ⇒ Object
19 20 21 |
# File 'lib/red_quilt/node_ref.rb', line 19 def type @arena.type_name(@node_id) end |