Module: Sass::Script::Value::Helpers

Included in:
Functions::EvaluationContext
Defined in:
lib/sass/script/value/helpers.rb

Overview

Provides helper functions for creating sass values from within ruby methods.

Since:

  • 3.3.0

Instance Method Summary collapse

Instance Method Details

#bool(value) ⇒ Sass::Script::Value::Bool

Construct a Sass Boolean.

Parameters:

  • value (Object)

    A ruby object that will be tested for truthiness.

Returns:

Since:

  • 3.3.0



9
10
11
# File 'lib/sass/script/value/helpers.rb', line 9

def bool(value)
  Bool.new(value)
end

#hex_color(value, alpha = nil) ⇒ Sass::Script::Value::Color

Construct a Sass Color from a hex color string.

Parameters:

  • value (::String)

    A string representing a hex color. The leading hash ("#") is optional.

  • alpha (::Number) (defaults to: nil)

    The alpha channel. A number between 0 and 1.

Returns:

Since:

  • 3.3.0



19
20
21
# File 'lib/sass/script/value/helpers.rb', line 19

def hex_color(value, alpha = nil)
  Color.from_hex(value, alpha)
end

#hsl_color(hue, saturation, lightness, alpha = nil) ⇒ Sass::Script::Value::Color

Construct a Sass Color from hsl values.

Parameters:

  • hue (::Number)

    The hue of the color in degrees. A non-negative number, usually less than 360.

  • saturation (::Number)

    The saturation of the color. Must be between 0 and 100 inclusive.

  • lightness (::Number)

    The lightness of the color. Must be between 0 and 100 inclusive.

  • alpha (::Number) (defaults to: nil)

    The alpha channel. A number between 0 and 1.

Returns:

Since:

  • 3.3.0



34
35
36
37
38
# File 'lib/sass/script/value/helpers.rb', line 34

def hsl_color(hue, saturation, lightness, alpha = nil)
  attrs = {:hue => hue, :saturation => saturation, :lightness => lightness}
  attrs[:alpha] = alpha if alpha
  Color.new(attrs)
end

#list(*elements, separator) ⇒ Sass::Script::Value::List #list(array, separator) ⇒ Sass::Script::Value::List

Overloads:

Since:

  • 3.3.0



80
81
82
83
84
85
86
87
88
89
# File 'lib/sass/script/value/helpers.rb', line 80

def list(*elements)
  unless elements.last.is_a?(Symbol)
    raise ArgumentError.new("A list type of :space or :comma must be specified.")
  end
  separator = elements.pop
  if elements.size == 1 && elements.first.is_a?(Array)
    elements = elements.first
  end
  Sass::Script::Value::List.new(elements, separator)
end

#map(hash) ⇒ Sass::Script::Value::Map

Construct a Sass map.

Parameters:

Returns:

Since:

  • 3.3.0



96
97
98
# File 'lib/sass/script/value/helpers.rb', line 96

def map(hash)
  Map.new(hash)
end

#nullSass::Script::Value::Null

Create a sass null value.

Returns:

Since:

  • 3.3.0



103
104
105
# File 'lib/sass/script/value/helpers.rb', line 103

def null
  Sass::Script::Value::Null.new
end

#number(number, unit_string = nil) ⇒ Sass::Script::Value::Number

Construct a Sass Number from a ruby number.

Parameters:

  • number (::Number)

    A numeric value.

  • unit_string (::String) (defaults to: nil)

    A unit string of the form numeral_unit1 * numeral_unit2 ... / denominator_unit1 * denominator_unit2 ... this is the same format that is returned by the unit_str method

Returns:

See Also:

Since:

  • 3.3.0



65
66
67
# File 'lib/sass/script/value/helpers.rb', line 65

def number(number, unit_string = nil)
  Number.new(number, *parse_unit_string(unit_string))
end

#quoted_string(str) ⇒ Sass::Script::Value::String

Create a quoted string.

Parameters:

  • str (::String)

    A ruby string.

Returns:

Since:

  • 3.3.0



111
112
113
# File 'lib/sass/script/value/helpers.rb', line 111

def quoted_string(str)
  Sass::Script::String.new(str, :string)
end

#rgb_color(red, green, blue, alpha = nil) ⇒ Sass::Script::Value::Color

Construct a Sass Color from rgb values.

Parameters:

  • red (::Number)

    The red component. Must be between 0 and 255 inclusive.

  • green (::Number)

    The green component. Must be between 0 and 255 inclusive.

  • blue (::Number)

    The blue component. Must be between 0 and 255 inclusive.

  • alpha (::Number) (defaults to: nil)

    The alpha channel. A number between 0 and 1.

Returns:

Since:

  • 3.3.0



48
49
50
51
52
# File 'lib/sass/script/value/helpers.rb', line 48

def rgb_color(red, green, blue, alpha = nil)
  attrs = {:red => red, :green => green, :blue => blue}
  attrs[:alpha] = alpha if alpha
  Color.new(attrs)
end

#unquoted_string(str) ⇒ Sass::Script::Value::String Also known as: identifier

Create an unquoted string.

Parameters:

  • str (::String)

    A ruby string.

Returns:

Since:

  • 3.3.0



119
120
121
# File 'lib/sass/script/value/helpers.rb', line 119

def unquoted_string(str)
  Sass::Script::String.new(str, :identifier)
end