Class: Less::Node::Expression
- Inherits:
-
Array
- Object
- Array
- Less::Node::Expression
- Defined in:
- lib/less/engine/nodes/property.rb
Instance Attribute Summary collapse
-
#delimiter ⇒ Object
Returns the value of attribute delimiter.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #entities ⇒ Object
-
#evaluate(env = nil) ⇒ Object
Evaluates the expression and instantiates a new Literal with the result ex: [#111, +, #111] will evaluate to a Color node, with value #222.
- #expressions ⇒ Object
- #flatten ⇒ Object
-
#initialize(ary, parent = nil, delimiter = ' ') ⇒ Expression
constructor
A new instance of Expression.
- #inspect ⇒ Object
- #literals ⇒ Object
- #operators ⇒ Object
- #terminal? ⇒ Boolean
- #to_css(env = nil) ⇒ Object
- #to_ruby ⇒ Object
- #variables ⇒ Object
Constructor Details
#initialize(ary, parent = nil, delimiter = ' ') ⇒ Expression
Returns a new instance of Expression.
138 139 140 141 142 143 |
# File 'lib/less/engine/nodes/property.rb', line 138 def initialize ary, parent = nil, delimiter = ' ' self.parent = parent self.delimiter = delimiter # puts "new expression #{ary} |#{delimiter}|" super(ary.is_a?(Array) ? ary : [ary].flatten) end |
Instance Attribute Details
#delimiter ⇒ Object
Returns the value of attribute delimiter.
136 137 138 |
# File 'lib/less/engine/nodes/property.rb', line 136 def delimiter @delimiter end |
#parent ⇒ Object
Returns the value of attribute parent.
136 137 138 |
# File 'lib/less/engine/nodes/property.rb', line 136 def parent @parent end |
Instance Method Details
#entities ⇒ Object
148 |
# File 'lib/less/engine/nodes/property.rb', line 148 def entities; select {|i| i.kind_of? Entity } end |
#evaluate(env = nil) ⇒ Object
Evaluates the expression and instantiates a new Literal with the result ex: [#111, +, #111] will evaluate to a Color node, with value #222
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/less/engine/nodes/property.rb', line 190 def evaluate env = nil # puts "expression #{self.inspect} env: #{env ? env.variables : "nil"}" if size > 2 or !terminal? # puts " SIZE > 2 or !terminal" # puts "--- sub evaluation ---" # Replace self with an evaluated sub-expression evaled = self.class.new(map {|e| e.respond_to?(:evaluate) ? e.evaluate(env) : e }, parent, delimiter) #5 # puts "======================" # puts "evaled => #{evaled.inspect}" unit = evaled.literals.map do |node| node.unit end.compact.uniq raise MixedUnitsError, evaled * ' ' if unit.size > 1 && !evaled.operators.empty? unit = unit.join entity = evaled.literals.find {|e| e.unit == unit } || evaled.literals.first || evaled.entities.first result = evaled.operators.empty?? evaled : eval(evaled.to_ruby.join) # puts "entity is a #{entity.class}" # puts "delimiter is |#{@delimiter}|" case result when Entity then result when Expression then result.size == 1 ? result.first : self.class.new(result, parent, delimiter) else entity.class.new(result, *(unit if entity.class == Node::Number)) end elsif size == 1 if first.is_a? Variable first.evaluate(env) elsif first.is_a? Function first.evaluate(env) else first end else self end end |
#expressions ⇒ Object
145 |
# File 'lib/less/engine/nodes/property.rb', line 145 def expressions; select {|i| i.kind_of? Expression } end |
#flatten ⇒ Object
164 165 166 |
# File 'lib/less/engine/nodes/property.rb', line 164 def flatten self end |
#inspect ⇒ Object
156 157 158 |
# File 'lib/less/engine/nodes/property.rb', line 156 def inspect '[' + map {|i| i.inspect }.join(', ') + ']' end |
#literals ⇒ Object
149 |
# File 'lib/less/engine/nodes/property.rb', line 149 def literals; select {|i| i.kind_of? Literal } end |
#operators ⇒ Object
147 |
# File 'lib/less/engine/nodes/property.rb', line 147 def operators; select {|i| i.is_a? Operator } end |
#terminal? ⇒ Boolean
168 169 170 |
# File 'lib/less/engine/nodes/property.rb', line 168 def terminal? expressions.empty? #&& variables.empty? end |
#to_css(env = nil) ⇒ Object
172 173 174 175 176 177 178 |
# File 'lib/less/engine/nodes/property.rb', line 172 def to_css env = nil # puts "TOCSS, delim: |#{@delimiter}|" map do |i| i.parent = self.parent i.respond_to?(:to_css) ? i.to_css() : i.to_s end * @delimiter end |
#to_ruby ⇒ Object
180 181 182 183 184 |
# File 'lib/less/engine/nodes/property.rb', line 180 def to_ruby map do |i| i.respond_to?(:to_ruby) ? i.to_ruby : i.to_s end end |
#variables ⇒ Object
146 |
# File 'lib/less/engine/nodes/property.rb', line 146 def variables; select {|i| i.kind_of? Variable } end |