class Sass::Script::Functions::EvaluationContext
The context in which methods in {Script::Functions} are evaluated. That means that all instance methods of {EvaluationContext} are available to use in functions.
Constants
- TYPE_NAMES
The human-readable names for [Sass::Script::Value::Base]. The default is just the downcased name of the type.
Attributes
The environment for this function. This environment’s {Environment#parent} is the global environment, and its {Environment#caller} is a read-only view of the local environment of the caller of this function.
@return [Environment]
The options hash for the {Sass::Engine} that is processing the function call
@return [{Symbol => Object}]
Public Class Methods
@param environment [Environment] See {#environment}
# File lib/sass/script/functions.rb, line 514 def initialize(environment) @environment = environment @options = environment.options end
Public Instance Methods
Asserts that the value is an integer.
@example
assert_integer 2px assert_integer 2.5px => SyntaxError: "Expected 2.5px to be an integer" assert_integer 2.5px, "width" => SyntaxError: "Expected width to be an integer but got 2.5px"
@param number [Sass::Script::Value::Base] The value to be validated. @param name [::String] The name of the parameter being validated. @raise [ArgumentError] if number is not an integer or is not a number.
# File lib/sass/script/functions.rb, line 588 def assert_integer(number, name = nil) assert_type number, :Number, name return if number.int? if name raise ArgumentError.new("Expected $#{name} to be an integer but got #{number}") else raise ArgumentError.new("Expected #{number} to be an integer") end end
Asserts that the type of a given SassScript value is the expected type (designated by a symbol).
Valid types are ‘:Bool`, `:Color`, `:Number`, and `:String`. Note that `:String` will match both double-quoted strings and unquoted identifiers.
@example
assert_type value, :String assert_type value, :Number
@param value [Sass::Script::Value::Base] A SassScript value @param type [Symbol, Array<Symbol>] The name(s) of the type the value is expected to be @param name [String, Symbol, nil] The name of the argument. @raise [ArgumentError] if value is not of the correct type.
# File lib/sass/script/functions.rb, line 533 def assert_type(value, type, name = nil) valid_types = Array(type) found_type = valid_types.find do |t| value.is_a?(Sass::Script::Value.const_get(t)) || t == :Map && value.is_a?(Sass::Script::Value::List) && value.value.empty? end if found_type value.check_deprecated_interp if found_type == :String return end err = if valid_types.size == 1 "#{value.inspect} is not a #{TYPE_NAMES[type] || type.to_s.downcase}" else type_names = valid_types.map {|t| TYPE_NAMES[t] || t.to_s.downcase} "#{value.inspect} is not any of #{type_names.join(', ')}" end err = "$#{name.to_s.tr('_', '-')}: " + err if name raise ArgumentError.new(err) end
Asserts that the unit of the number is as expected.
@example
assert_unit number, "px" assert_unit number, nil
@param number [Sass::Script::Value::Number] The number to be validated. @param unit [::String]
The unit that the number must have. If nil, the number must be unitless.
@param name [::String] The name of the parameter being validated. @raise [ArgumentError] if number is not of the correct unit or is not a number.
# File lib/sass/script/functions.rb, line 566 def assert_unit(number, unit, name = nil) assert_type number, :Number, name return if number.is_unit?(unit) expectation = unit ? "have a unit of #{unit}" : "be unitless" if name raise ArgumentError.new("Expected $#{name} to #{expectation} but got #{number}") else raise ArgumentError.new("Expected #{number} to #{expectation}") end end
Performs a node that has been delayed for execution.
@private @param node [Sass::Script::Tree::Node,
Sass::Script::Value::Base] When this is a tree node, it's performed in the caller's environment. When it's a value (which can happen when the value had to be performed already -- like for a splat), it's returned as-is.
@param env [Sass::Environment] The environment within which to perform the node.
Defaults to the (read-only) environment of the caller.
# File lib/sass/script/functions.rb, line 608 def perform(node, env = environment.caller) if node.is_a?(Sass::Script::Value::Base) node else node.perform(env) end end