Class: LessNode
- Inherits:
-
Object
- Object
- LessNode
- Defined in:
- lib/lessify/less_node.rb
Instance Attribute Summary collapse
-
#child_nodes ⇒ Object
Returns the value of attribute child_nodes.
-
#dec_array ⇒ Object
Returns the value of attribute dec_array.
-
#declarations ⇒ Object
Returns the value of attribute declarations.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#selector ⇒ Object
Returns the value of attribute selector.
Instance Method Summary collapse
- #add_child(selector_string, declarations) ⇒ Object
- #child(selector) ⇒ Object
-
#find(selector) ⇒ Object
Search through the tree for selector.
- #has_child?(selector) ⇒ Boolean
-
#initialize(selector, parent, declarations = "") ⇒ LessNode
constructor
A new instance of LessNode.
- #move(sel_parent, sel_child) ⇒ Object
- #print_ancestry(selector) ⇒ Object
- #to_scss(prefix = "", options = {}) ⇒ Object
- #update_declarations_array ⇒ Object
Constructor Details
#initialize(selector, parent, declarations = "") ⇒ LessNode
Returns a new instance of LessNode.
5 6 7 8 9 10 11 12 |
# File 'lib/lessify/less_node.rb', line 5 def initialize(selector, parent, declarations = "") @selector = selector @parent = parent @declarations = declarations.strip @child_nodes = [] update_declarations_array parent.child_nodes.push(self) unless parent.nil? end |
Instance Attribute Details
#child_nodes ⇒ Object
Returns the value of attribute child_nodes.
3 4 5 |
# File 'lib/lessify/less_node.rb', line 3 def child_nodes @child_nodes end |
#dec_array ⇒ Object
Returns the value of attribute dec_array.
3 4 5 |
# File 'lib/lessify/less_node.rb', line 3 def dec_array @dec_array end |
#declarations ⇒ Object
Returns the value of attribute declarations.
3 4 5 |
# File 'lib/lessify/less_node.rb', line 3 def declarations @declarations end |
#parent ⇒ Object
Returns the value of attribute parent.
3 4 5 |
# File 'lib/lessify/less_node.rb', line 3 def parent @parent end |
#selector ⇒ Object
Returns the value of attribute selector.
3 4 5 |
# File 'lib/lessify/less_node.rb', line 3 def selector @selector end |
Instance Method Details
#add_child(selector_string, declarations) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/lessify/less_node.rb', line 26 def add_child(selector_string, declarations) selectors = selector_string.strip.split(" ") node = self selectors.each do |sel| if node.has_child?(sel) node = node.child(sel) else newNode = LessNode.new(sel, node) node = newNode end end node.declarations += (" " + declarations.strip) node.declarations.strip! node.update_declarations_array return node end |
#child(selector) ⇒ Object
22 23 24 |
# File 'lib/lessify/less_node.rb', line 22 def child(selector) @child_nodes.find { |cnode| cnode.selector == selector } end |
#find(selector) ⇒ Object
Search through the tree for selector
60 61 62 63 64 65 66 67 68 |
# File 'lib/lessify/less_node.rb', line 60 def find(selector) node = child(selector) return node unless node.nil? @child_nodes.each { |cnode| n = cnode.find(selector) return n unless n.nil? } return nil end |
#has_child?(selector) ⇒ Boolean
18 19 20 |
# File 'lib/lessify/less_node.rb', line 18 def has_child?(selector) @child_nodes.map { |cnode| cnode.selector == selector }.reduce(:|) end |
#move(sel_parent, sel_child) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/lessify/less_node.rb', line 70 def move(sel_parent, sel_child) parent_node = find(sel_parent) raise "#{sel_parent} not found" if parent_node.nil? child_node = find(sel_child) raise "#{sel_child} not found" if child_node.nil? node = parent_node until node.nil? raise "Move will create a cycle in the tree" if node == child_node node = node.parent end child_node.parent.child_nodes.delete(child_node) parent_node.child_nodes.push(child_node) child_node.parent = parent_node end |
#print_ancestry(selector) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/lessify/less_node.rb', line 87 def print_ancestry(selector) node = find(selector) puts "Printing ancestry for #{node}..." until node.nil? puts "\t#{node.to_s}" node = node.parent end end |
#to_scss(prefix = "", options = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/lessify/less_node.rb', line 43 def to_scss(prefix = "", = {}) tab = ['tab'] || "\t" print_declarations = [:print_declarations] print_declarations = true if print_declarations.nil? str = prefix + selector + " {" if (@child_nodes.size==0) && (!print_declarations || declarations.strip.size==0) str += "}\n" else str += "\n" str += (prefix + tab + declarations + "\n") if (print_declarations && declarations.strip.size>0) @child_nodes.each { |cnode| str += cnode.to_scss(prefix + tab, ) } str += prefix + "}\n" end end |
#update_declarations_array ⇒ Object
14 15 16 |
# File 'lib/lessify/less_node.rb', line 14 def update_declarations_array @dec_array = declarations.split(";").map { |d| d.strip } end |