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, delimiter = ' ') ⇒ Expression

Returns a new instance of Expression.



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

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.



134
135
136
# File 'lib/less/engine/nodes/property.rb', line 134

def delimiter
  @delimiter
end

#parentObject

Returns the value of attribute parent.



134
135
136
# File 'lib/less/engine/nodes/property.rb', line 134

def parent
  @parent
end

Instance Method Details

#entitiesObject



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

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



188
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/less/engine/nodes/property.rb', line 188

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.tap do |ary|
      raise MixedUnitsError, evaled * ' ' if ary.size > 1 && !evaled.operators.empty?
    end.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.one?? 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



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

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

#flattenObject



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

def flatten
  self
end

#inspectObject



154
155
156
# File 'lib/less/engine/nodes/property.rb', line 154

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

#literalsObject



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

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

#operatorsObject



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

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

#terminal?Boolean

Returns:

  • (Boolean)


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

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

#to_css(env = nil) ⇒ Object



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

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



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

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

#variablesObject



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

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