Class: Less::Node::Expression

Inherits:
Array show all
Defined in:
lib/less/engine/nodes/property.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#dissolve, #one?

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

#parentObject

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

#copyObject



169
170
171
# File 'lib/less/engine/nodes/property.rb', line 169

def copy
  self.class.new(map {|i| i.dup }, parent)
end

#entitiesObject



149
# File 'lib/less/engine/nodes/property.rb', line 149

def entities;    select {|i| i.kind_of? Entity }     end

#evaluateObject

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

#expressionsObject



147
# File 'lib/less/engine/nodes/property.rb', line 147

def expressions; select {|i| i.kind_of? Expression } end

#flattenObject



161
162
163
# File 'lib/less/engine/nodes/property.rb', line 161

def flatten
  self
end

#inspectObject



157
158
159
# File 'lib/less/engine/nodes/property.rb', line 157

def inspect
  '[' + map {|i| i.inspect }.join(', ') + ']'
end

#literalsObject



150
# File 'lib/less/engine/nodes/property.rb', line 150

def literals;    select {|i| i.kind_of? Literal }    end

#operatorsObject



148
# File 'lib/less/engine/nodes/property.rb', line 148

def operators;   select {|i| i.is_a? Operator }      end

#terminal?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/less/engine/nodes/property.rb', line 165

def terminal?
  expressions.empty?
end

#to_cssObject



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_rubyObject



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