Class: Less::Node::Expression

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ary, parent = nil, delimiter = ' ') ⇒ Expression

Returns a new instance of Expression.



138
139
140
141
142
143
# File 'lib/less/engine/nodes/property.rb', line 138

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

#delimiterObject

Returns the value of attribute delimiter.



136
137
138
# File 'lib/less/engine/nodes/property.rb', line 136

def delimiter
  @delimiter
end

#parentObject

Returns the value of attribute parent.



136
137
138
# File 'lib/less/engine/nodes/property.rb', line 136

def parent
  @parent
end

Instance Method Details

#entitiesObject



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

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



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
227
228
229
230
231
# File 'lib/less/engine/nodes/property.rb', line 190

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
    raise MixedUnitsError, evaled * ' ' if unit.size > 1 && !evaled.operators.empty?
    unit = unit.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.size == 1 ? 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)
    elsif first.is_a? Function
      first.evaluate(env)
    else
      first
    end
  else
    self
  end
end

#expressionsObject



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

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

#flattenObject



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

def flatten
  self
end

#inspectObject



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

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

#literalsObject



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

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

#operatorsObject



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

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

#terminal?Boolean

Returns:

  • (Boolean)


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

def terminal?
  expressions.empty? #&& variables.empty?
end

#to_css(env = nil) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/less/engine/nodes/property.rb', line 172

def to_css env = nil
#        puts "TOCSS, delim: |#{@delimiter}|"
  map do |i|
    i.parent = self.parent
    i.respond_to?(:to_css) ? i.to_css() : i.to_s
  end * @delimiter
end

#to_rubyObject



180
181
182
183
184
# File 'lib/less/engine/nodes/property.rb', line 180

def to_ruby
  map do |i|
    i.respond_to?(:to_ruby) ? i.to_ruby : i.to_s
  end
end

#variablesObject



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

def variables;   select {|i| i.kind_of? Variable   } end