Class: Sass::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/sass/environment.rb

Overview

The lexical environment for SassScript. This keeps track of variable and mixin definitions.

A new environment is created for each level of Sass nesting. This allows variables to be lexically scoped. The new environment refers to the environment in the upper scope, so it has access to variables defined in enclosing scopes, but new variables are defined locally.

Environment also keeps track of the Engine options so that they can be made available to Script::Functions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Environment

Returns a new instance of Environment.

Parameters:



22
23
24
25
26
27
28
# File 'lib/sass/environment.rb', line 22

def initialize(parent = nil)
  @vars = {}
  @mixins = {}
  @parent = parent
  @stack = [] unless parent
  set_var("important", Script::String.new("!important")) unless @parent
end

Instance Attribute Details

#options{Symbol => Object}

The options hash. See the Sass options documentation.

Returns:

  • ({Symbol => Object})


34
35
36
# File 'lib/sass/environment.rb', line 34

def options
  @options || (parent && parent.options) || {}
end

#parentEnvironment (readonly)

The enclosing environment, or nil if this is the global environment.

Returns:



18
19
20
# File 'lib/sass/environment.rb', line 18

def parent
  @parent
end

Instance Method Details

#pop_frame

Pop a stack frame from the mixin/include stack.



70
71
72
73
# File 'lib/sass/environment.rb', line 70

def pop_frame
  stack.pop if stack.last[:prepared]
  stack.pop
end

#prepare_frame(frame_info)

Like #push_frame, but next time a stack frame is pushed, it will be merged with this frame.

Parameters:

  • frame_info ({Symbol => Object})

    Same as for #push_frame.



65
66
67
# File 'lib/sass/environment.rb', line 65

def prepare_frame(frame_info)
  push_frame(frame_info.merge(:prepared => true))
end

#push_frame(frame_info)

Push a new stack frame onto the mixin/include stack.

Parameters:

  • frame_info ({Symbol => Object})

    Frame information has the following keys:

    :filename : The name of the file in which the lexical scope changed.

    :mixin : The name of the mixin in which the lexical scope changed, or nil if it wasn't within in a mixin.

    :line : The line of the file on which the lexical scope changed. Never nil.



52
53
54
55
56
57
58
59
# File 'lib/sass/environment.rb', line 52

def push_frame(frame_info)
  if stack.last && stack.last[:prepared]
    stack.last.delete(:prepared)
    stack.last.merge!(frame_info)
  else
    stack.push(frame_info)
  end
end

#stackArray<{Symbol => Object}>

A list of stack frames in the mixin/include stack. The last element in the list is the most deeply-nested frame.

Returns:

  • (Array<{Symbol => Object}>)

    The stack frames, of the form passed to #push_frame.



80
81
82
# File 'lib/sass/environment.rb', line 80

def stack
  @stack ||= @parent.stack
end