Class: Stylish::Tree::SelectorScope
- Inherits:
-
Object
- Object
- Stylish::Tree::SelectorScope
- Includes:
- Formattable, Node
- Defined in:
- lib/stylish/tree.rb
Overview
Rules are namespaced by their place in a selector tree.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Instance Method Summary collapse
-
#<<(node) ⇒ Object
Append a child node.
-
#[](index) ⇒ Object
Return the child node at the given index.
-
#[]=(index, node) ⇒ Object
Replace an existing child node.
-
#comments ⇒ Object
Recursively return all the comments in the selector tree.
-
#delete(node) ⇒ Object
Remove a child node.
-
#initialize(selector) ⇒ SelectorScope
constructor
A new instance of SelectorScope.
-
#leaves(type = nil) ⇒ Object
Recursively return all the leaves of any, or a given type in a selector tree.
-
#rules ⇒ Object
Recursively return all the rules in the selector tree.
-
#to_a ⇒ Object
Return the node’s child nodes.
-
#to_s(symbols = {}, scope = "") ⇒ Object
Recursively serialise the selector tree.
Methods included from Formattable
Methods included from Node
Constructor Details
#initialize(selector) ⇒ SelectorScope
Returns a new instance of SelectorScope.
46 47 48 49 |
# File 'lib/stylish/tree.rb', line 46 def initialize(selector) @nodes = [] @scope = selector end |
Instance Attribute Details
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
43 44 45 |
# File 'lib/stylish/tree.rb', line 43 def nodes @nodes end |
Instance Method Details
#<<(node) ⇒ Object
Append a child node.
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/stylish/tree.rb', line 69 def <<(node) raise ArgumentError, "#{node.inspect} is not a node." unless node.is_a?(Tree::Node) unless node.root? @nodes << node else raise ArgumentError, "Root nodes cannot be added to trees." end end |
#[](index) ⇒ Object
Return the child node at the given index.
52 53 54 |
# File 'lib/stylish/tree.rb', line 52 def [](index) @nodes[index] end |
#[]=(index, node) ⇒ Object
Replace an existing child node.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/stylish/tree.rb', line 57 def []=(index, node) raise ArgumentError, "#{node.inspect} is not a node." unless node.is_a?(Tree::Node) unless node.root? @nodes[index] = node else raise ArgumentError, "Root nodes cannot be added to trees." end end |
#comments ⇒ Object
Recursively return all the comments in the selector tree.
105 106 107 |
# File 'lib/stylish/tree.rb', line 105 def comments leaves(Comment) end |
#delete(node) ⇒ Object
Remove a child node.
81 82 83 |
# File 'lib/stylish/tree.rb', line 81 def delete(node) @nodes.delete(node) end |
#leaves(type = nil) ⇒ Object
Recursively return all the leaves of any, or a given type in a selector tree.
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/stylish/tree.rb', line 111 def leaves(type = nil) @nodes.inject([]) do |leaves, node| if node.leaf? leaves << node if type.nil? || node.is_a?(type) else leaves.concat(node.leaves(type)) end leaves end end |
#rules ⇒ Object
Recursively return all the rules in the selector tree.
100 101 102 |
# File 'lib/stylish/tree.rb', line 100 def rules leaves(Rule) end |
#to_a ⇒ Object
Return the node’s child nodes.
95 96 97 |
# File 'lib/stylish/tree.rb', line 95 def to_a nodes end |
#to_s(symbols = {}, scope = "") ⇒ Object
Recursively serialise the selector tree.
86 87 88 89 90 91 92 |
# File 'lib/stylish/tree.rb', line 86 def to_s(symbols = {}, scope = "") return "" if @nodes.empty? @nodes.map {|node| node.to_s(symbols, @scope.to_s(symbols, scope)) }.join(self.class.format) end |