2008-10-15 20:00:28 -07:00
|
|
|
module Sass
|
2009-04-26 01:32:57 -07:00
|
|
|
# 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 {Sass::Functions}.
|
2008-10-15 20:00:28 -07:00
|
|
|
class Environment
|
2009-04-26 01:32:57 -07:00
|
|
|
# The enclosing environment,
|
|
|
|
# or nil if this is the global environment.
|
|
|
|
#
|
|
|
|
# @return [Environment]
|
2008-10-15 20:00:28 -07:00
|
|
|
attr_reader :parent
|
|
|
|
|
2009-04-26 01:32:57 -07:00
|
|
|
# @param parent [Environment] See \{#parent}
|
|
|
|
# @param options [Hash<Symbol, Object>] An options hash;
|
|
|
|
# see [the Sass options documentation](../Sass.html#sass_options)
|
2009-04-05 00:13:08 -07:00
|
|
|
def initialize(parent = nil, options = nil)
|
2008-10-15 20:00:28 -07:00
|
|
|
@vars = {}
|
|
|
|
@mixins = {}
|
|
|
|
@parent = parent
|
2009-04-05 00:13:08 -07:00
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
2009-04-26 01:32:57 -07:00
|
|
|
# The options hash.
|
|
|
|
# See [the Sass options documentation](../Sass.html#sass_options).
|
|
|
|
#
|
|
|
|
# @return [Hash<Symbol, Object>]
|
2009-04-05 00:13:08 -07:00
|
|
|
def options
|
|
|
|
@options || (parent && parent.options) || {}
|
2008-10-15 20:00:28 -07:00
|
|
|
end
|
|
|
|
|
2009-04-26 01:32:57 -07:00
|
|
|
class << self
|
|
|
|
private
|
2008-10-15 20:00:28 -07:00
|
|
|
|
2009-04-26 01:32:57 -07:00
|
|
|
# Note: when updating this,
|
|
|
|
# update haml/yard/inherited_hash.rb as well.
|
|
|
|
def inherited_hash(name)
|
|
|
|
class_eval <<RUBY, __FILE__, __LINE__ + 1
|
|
|
|
def #{name}(name)
|
|
|
|
@#{name}s[name] || @parent && @parent.#{name}(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_#{name}(name, value)
|
2009-04-27 13:33:52 -07:00
|
|
|
@#{name}s[name] = value unless try_set_#{name}(name, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def try_set_#{name}(name, value)
|
|
|
|
if @#{name}s.include?(name)
|
2009-04-26 01:32:57 -07:00
|
|
|
@#{name}s[name] = value
|
2009-04-27 13:33:52 -07:00
|
|
|
true
|
|
|
|
elsif @parent
|
|
|
|
@parent.try_set_#{name}(name, value)
|
2009-04-26 01:32:57 -07:00
|
|
|
else
|
2009-04-27 13:33:52 -07:00
|
|
|
false
|
2009-04-26 01:32:57 -07:00
|
|
|
end
|
2008-10-15 20:00:28 -07:00
|
|
|
end
|
2009-04-27 13:33:52 -07:00
|
|
|
protected :try_set_#{name}
|
2008-10-15 20:00:28 -07:00
|
|
|
|
2009-04-26 01:32:57 -07:00
|
|
|
def set_local_#{name}(name, value)
|
|
|
|
@#{name}s[name] = value
|
|
|
|
end
|
2008-10-15 20:00:28 -07:00
|
|
|
RUBY
|
2009-04-26 01:32:57 -07:00
|
|
|
end
|
2008-10-15 20:00:28 -07:00
|
|
|
end
|
2009-04-26 01:32:57 -07:00
|
|
|
|
|
|
|
# variable
|
|
|
|
# Script::Literal
|
2008-10-15 20:00:28 -07:00
|
|
|
inherited_hash :var
|
2009-04-26 01:32:57 -07:00
|
|
|
# mixin
|
|
|
|
# Engine::Mixin
|
2008-10-15 20:00:28 -07:00
|
|
|
inherited_hash :mixin
|
|
|
|
end
|
|
|
|
end
|