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.
Attributes included from Formattable
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 Node
Constructor Details
#initialize(selector) ⇒ SelectorScope
Returns a new instance of SelectorScope.
45 46 47 48 49 50 |
# File 'lib/stylish/tree.rb', line 45 def initialize(selector) accept_format(/\s*/m, "\n") @scope = Selector.new(selector) @nodes = [] 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.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/stylish/tree.rb', line 70 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.
53 54 55 |
# File 'lib/stylish/tree.rb', line 53 def [](index) @nodes[index] end |
#[]=(index, node) ⇒ Object
Replace an existing child node.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/stylish/tree.rb', line 58 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.
104 105 106 |
# File 'lib/stylish/tree.rb', line 104 def comments leaves(Comment) end |
#delete(node) ⇒ Object
Remove a child node.
82 83 84 |
# File 'lib/stylish/tree.rb', line 82 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.
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/stylish/tree.rb', line 110 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.
99 100 101 |
# File 'lib/stylish/tree.rb', line 99 def rules leaves(Rule) end |
#to_a ⇒ Object
Return the node’s child nodes.
94 95 96 |
# File 'lib/stylish/tree.rb', line 94 def to_a nodes end |
#to_s(symbols = {}, scope = "") ⇒ Object
Recursively serialise the selector tree.
87 88 89 90 91 |
# File 'lib/stylish/tree.rb', line 87 def to_s(symbols = {}, scope = "") return "" if @nodes.empty? scope = scope.empty? ? @scope.to_s : scope + " " + @scope.to_s @nodes.map {|node| node.to_s(symbols, scope) }.join(@format) end |