Class: Less::Node::Expression
- Defined in:
- lib/less/engine/nodes/property.rb
Instance Attribute Summary collapse
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #copy ⇒ Object
- #entities ⇒ Object
-
#evaluate ⇒ 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) ⇒ Expression
constructor
A new instance of Expression.
- #inspect ⇒ Object
- #literals ⇒ Object
- #operators ⇒ Object
- #terminal? ⇒ Boolean
- #to_css ⇒ Object
- #to_ruby ⇒ Object
Methods inherited from Array
Constructor Details
#initialize(ary, parent = nil) ⇒ Expression
Returns a new instance of Expression.
142 143 144 145 |
# File 'lib/less/engine/nodes/property.rb', line 142 def initialize ary, parent = nil self.parent = parent super ary.dup end |
Instance Attribute Details
#parent ⇒ Object
Returns the value of attribute parent.
140 141 142 |
# File 'lib/less/engine/nodes/property.rb', line 140 def parent @parent end |
Instance Method Details
#copy ⇒ Object
169 170 171 |
# File 'lib/less/engine/nodes/property.rb', line 169 def copy self.class.new(map {|i| i.dup }, parent) end |
#entities ⇒ Object
149 |
# File 'lib/less/engine/nodes/property.rb', line 149 def entities; select {|i| i.kind_of? Entity } end |
#evaluate ⇒ Object
Evaluates the expression and instantiates a new Literal with the result ex: [#111, +, #111] will evaluate to a Color node, with value #222
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 |
# File 'lib/less/engine/nodes/property.rb', line 189 def evaluate if size > 2 or !terminal? # Replace self with an evaluated sub-expression replace map {|e| e.respond_to?(:evaluate) ? e.evaluate : e } unit = literals.map do |node| node.unit end.compact.uniq.tap do |ary| raise MixedUnitsError, self * ' ' if ary.size > 1 && !operators.empty? end.join entity = literals.find {|e| e.unit == unit } || literals.first || entities.first result = operators.empty?? self : eval(to_ruby.join) case result when Entity then result when Expression then result.one?? result.first : self.class.new(result) else entity.class.new(result, *(unit if entity.class == Node::Number)) end elsif size == 1 first else self end end |
#expressions ⇒ Object
147 |
# File 'lib/less/engine/nodes/property.rb', line 147 def expressions; select {|i| i.kind_of? Expression } end |
#flatten ⇒ Object
161 162 163 |
# File 'lib/less/engine/nodes/property.rb', line 161 def flatten self end |
#inspect ⇒ Object
157 158 159 |
# File 'lib/less/engine/nodes/property.rb', line 157 def inspect '[' + map {|i| i.inspect }.join(', ') + ']' end |
#literals ⇒ Object
150 |
# File 'lib/less/engine/nodes/property.rb', line 150 def literals; select {|i| i.kind_of? Literal } end |
#operators ⇒ Object
148 |
# File 'lib/less/engine/nodes/property.rb', line 148 def operators; select {|i| i.is_a? Operator } end |
#terminal? ⇒ Boolean
165 166 167 |
# File 'lib/less/engine/nodes/property.rb', line 165 def terminal? expressions.empty? end |
#to_css ⇒ Object
173 174 175 176 177 |
# File 'lib/less/engine/nodes/property.rb', line 173 def to_css map do |i| i.respond_to?(:to_css) ? i.to_css : i.to_s end * ' ' end |
#to_ruby ⇒ Object
179 180 181 182 183 |
# File 'lib/less/engine/nodes/property.rb', line 179 def to_ruby map do |i| i.respond_to?(:to_ruby) ? i.to_ruby : i.to_s end end |