Class: Less::Node::Expression

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

Instance Method Summary collapse

Methods inherited from Array

#mean, #sum

Instance Method Details

#entitiesObject



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

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

TODO: refactor the conditionals



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/less/engine/nodes/property.rb', line 80

def evaluate
  if size > 2 && (entities.size == operators.size + 1)
    
    # Create a sub-expression with all the variables/properties evaluated
    evaluated = Expression.new map {|e| e.respond_to?(:evaluate) ? e.evaluate : e }
  
    unit = evaluated.literals.map do |node|
      node.unit
    end.compact.uniq.tap do |ary|
      raise MixedUnitsError, self * ' ' if ary.size > 1
    end.join
  
    entity = evaluated.literals.find {|e| e.unit == unit } || evaluated.first        
    ruby = map {|e| e.to_ruby if e.respond_to? :to_ruby }
  
    unless ruby.include? nil
      if entity
        result = eval(ruby.join)
        if result.is_a? Entity
          result
        else
          entity.class.new(*result, *(unit if entity.class == Node::Number))
        end
      else
        first
      end
    else
      self
    end
  elsif size == 1
    first
  else
    self
  end
end

#inspectObject



67
68
69
# File 'lib/less/engine/nodes/property.rb', line 67

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

#literalsObject



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

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

#operatorsObject



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

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

#to_cssObject



71
72
73
# File 'lib/less/engine/nodes/property.rb', line 71

def to_css
  map {|i| i.to_css } * ' '
end