Class: Less::Tree
Instance Method Summary collapse
-
#find(what = :var, path = []) ⇒ Object
Find a variable or mixin from a specific path.
-
#initialize(init = {}) ⇒ Tree
constructor
A new instance of Tree.
-
#nearest(var, path) ⇒ Object
Find the nearest variable in the hierarchy.
-
#to_css(css = []) ⇒ Object
Convert the tree to css, using full paths.
- #traverse(by = :leaf, path = [], &blk) ⇒ Object
- #var(k) ⇒ Object
- #vars ⇒ Object
- #vars? ⇒ Boolean
Methods inherited from Hash
Constructor Details
#initialize(init = {}) ⇒ Tree
Returns a new instance of Tree.
3 4 5 |
# File 'lib/less/tree.rb', line 3 def initialize init = {} self.replace init end |
Instance Method Details
#find(what = :var, path = []) ⇒ Object
Find a variable or mixin from a specific path
22 23 24 25 26 27 28 29 30 |
# File 'lib/less/tree.rb', line 22 def find what = :var, path = [] path.inject(self) do |branch, k| if what == :var && k == path.last branch[:variables][ k ] else branch = branch[ k ] end end end |
#nearest(var, path) ⇒ Object
Find the nearest variable in the hierarchy
35 36 37 38 39 40 41 42 |
# File 'lib/less/tree.rb', line 35 def nearest var, path values = [] path.inject(self) do |branch, k| values << branch.var( var ) branch[ k ] end values.compact.last end |
#to_css(css = []) ⇒ Object
Convert the tree to css, using full paths
72 73 74 75 76 77 78 79 80 |
# File 'lib/less/tree.rb', line 72 def to_css css = [] self.traverse :branch do |path, node| properties = node.inject("") do |s, (k, v)| v.is_a?(String) ? (s + "#{k}: #{CGI.unescape(v)}; ") : s # Add the property to the list end css << path * ' > ' + " { " + properties + "}" # Add the rule-set to the CSS end css.join "\n" end |
#traverse(by = :leaf, path = [], &blk) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/less/tree.rb', line 44 def traverse by = :leaf, path = [], &blk # # Traverse the whole tree, returning each leaf or branch (recursive) # ### # Aside from the key & value, we yield the full path of the leaf, # aswell as the branch which contains it (self). # Note that in :branch mode, we only return 'twigs', branches which contain leaves. # self.each do |key, value| # `self` is the current node, starting with the trunk if value.is_a? Hash and key != :variables # If the node is a branch, we can go deeper path << key # Add the current branch to the path self[ key ] = value.to_tree. # Make sure any change is saved to the main tree traverse by, path, &blk # Recurse, with the current node becoming `self` yield path, self[ key ] if by == :branch # The node is a branch, yield it to the block path.pop # We're returning from a branch, pop the last element elsif by == :leaf and key.is_a? String yield key, value, path, self # The node is a leaf, yield it to the block else next end end self end |
#var(k) ⇒ Object
7 8 9 |
# File 'lib/less/tree.rb', line 7 def var k self[:variables] ? self[:variables][ k ] : nil end |
#vars ⇒ Object
15 16 17 |
# File 'lib/less/tree.rb', line 15 def vars self[:variables] ? self[:variables] : nil end |
#vars? ⇒ Boolean
11 12 13 |
# File 'lib/less/tree.rb', line 11 def vars? self.include? :variables end |