Class: 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
Methods inherited from Array
Constructor Details
#initialize(ary, parent = nil, delimiter = ' ') ⇒ Expression
Returns a new instance of Expression.
136 137 138 139 140 141 |
# File 'lib/less/engine/nodes/property.rb', line 136 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.
134 135 136 |
# File 'lib/less/engine/nodes/property.rb', line 134 def delimiter @delimiter end |
#parent ⇒ Object
Returns the value of attribute parent.
134 135 136 |
# File 'lib/less/engine/nodes/property.rb', line 134 def parent @parent end |
Instance Method Details
#entities ⇒ Object
146 |
# File 'lib/less/engine/nodes/property.rb', line 146 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
187 188 189 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 |
# File 'lib/less/engine/nodes/property.rb', line 187 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.tap do |ary| raise MixedUnitsError, evaled * ' ' if ary.size > 1 && !evaled.operators.empty? end.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.one?? 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) else first end else self end end |
#expressions ⇒ Object
143 |
# File 'lib/less/engine/nodes/property.rb', line 143 def expressions; select {|i| i.kind_of? Expression } end |
#flatten ⇒ Object
162 163 164 |
# File 'lib/less/engine/nodes/property.rb', line 162 def flatten self end |
#inspect ⇒ Object
154 155 156 |
# File 'lib/less/engine/nodes/property.rb', line 154 def inspect '[' + map {|i| i.inspect }.join(', ') + ']' end |
#literals ⇒ Object
147 |
# File 'lib/less/engine/nodes/property.rb', line 147 def literals; select {|i| i.kind_of? Literal } end |
#operators ⇒ Object
145 |
# File 'lib/less/engine/nodes/property.rb', line 145 def operators; select {|i| i.is_a? Operator } end |
#terminal? ⇒ Boolean
166 167 168 |
# File 'lib/less/engine/nodes/property.rb', line 166 def terminal? expressions.empty? #&& variables.empty? end |
#to_css(env = nil) ⇒ Object
170 171 172 173 174 175 |
# File 'lib/less/engine/nodes/property.rb', line 170 def to_css env = nil # puts "TOCSS, delim: |#{@delimiter}|" map do |i| i.respond_to?(:to_css) ? i.to_css() : i.to_s end * @delimiter end |
#to_ruby ⇒ Object
177 178 179 180 181 |
# File 'lib/less/engine/nodes/property.rb', line 177 def to_ruby map do |i| i.respond_to?(:to_ruby) ? i.to_ruby : i.to_s end end |