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

Constructor Details

#initialize(ary) ⇒ Expression

Returns a new instance of Expression.



79
80
81
# File 'lib/less/engine/nodes/property.rb', line 79

def initialize ary
  super [ary].flatten
end

Instance Method Details

#entitiesObject



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

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/less/engine/nodes/property.rb', line 100

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



87
88
89
# File 'lib/less/engine/nodes/property.rb', line 87

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

#literalsObject



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

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

#operatorsObject



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

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

#to_cssObject



91
92
93
# File 'lib/less/engine/nodes/property.rb', line 91

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