PageRenderTime 20ms CodeModel.GetById 10ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-jruby/src/main/resources/haml-3.0.25/lib/sass/tree/for_node.rb

http://github.com/scalate/scalate
Ruby | 55 lines | 33 code | 6 blank | 16 comment | 0 complexity | 7d8c4f8b2b294c4e4ae8e4dc06372f76 MD5 | raw file
 1require 'sass/tree/node'
 2
 3module Sass::Tree
 4  # A dynamic node representing a Sass `@for` loop.
 5  #
 6  # @see Sass::Tree
 7  class ForNode < Node
 8    # @param var [String] The name of the loop variable
 9    # @param from [Script::Node] The parse tree for the initial expression
10    # @param to [Script::Node] The parse tree for the final expression
11    # @param exclusive [Boolean] Whether to include `to` in the loop
12    #   or stop just before
13    def initialize(var, from, to, exclusive)
14      @var = var
15      @from = from
16      @to = to
17      @exclusive = exclusive
18      super()
19    end
20
21    protected
22
23    # @see Node#to_src
24    def to_src(tabs, opts, fmt)
25      to = @exclusive ? "to" : "through"
26      "#{'  ' * tabs}@for $#{dasherize(@var, opts)} from #{@from.to_sass(opts)} #{to} #{@to.to_sass(opts)}" +
27        children_to_src(tabs, opts, fmt)
28    end
29
30    # Runs the child nodes once for each time through the loop,
31    # varying the variable each time.
32    #
33    # @param environment [Sass::Environment] The lexical environment containing
34    #   variable and mixin values
35    # @return [Array<Tree::Node>] The resulting static nodes
36    # @see Sass::Tree
37    def _perform(environment)
38      from = @from.perform(environment)
39      to = @to.perform(environment)
40      from.assert_int!
41      to.assert_int!
42
43      to = to.coerce(from.numerator_units, from.denominator_units)
44      range = Range.new(from.to_i, to.to_i, @exclusive)
45
46      children = []
47      environment = Sass::Environment.new(environment)
48      range.each do |i|
49        environment.set_local_var(@var, Sass::Script::Number.new(i, from.numerator_units, from.denominator_units))
50        children += perform_children(environment)
51      end
52      children
53    end
54  end
55end