Module: SassC::Script::ValueConversion

Defined in:
lib/sassc/script/value_conversion.rb,
lib/sassc/script/value_conversion/map.rb,
lib/sassc/script/value_conversion/base.rb,
lib/sassc/script/value_conversion/bool.rb,
lib/sassc/script/value_conversion/list.rb,
lib/sassc/script/value_conversion/color.rb,
lib/sassc/script/value_conversion/number.rb,
lib/sassc/script/value_conversion/string.rb

Defined Under Namespace

Classes: Base, Bool, Color, List, Map, Number, String

Constant Summary collapse

SEPARATORS =
{
  space: :sass_space,
  comma: :sass_comma
}

Class Method Summary collapse

Class Method Details

.from_native(native_value, options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sassc/script/value_conversion.rb', line 5

def self.from_native(native_value, options)
  case value_tag = SassC::Native.value_get_tag(native_value)
  when :sass_null
    # no-op
  when :sass_string
    value = SassC::Native.string_get_value(native_value)
    type = SassC::Native.string_get_type(native_value)
    argument = SassC::Script::Value::String.new(value, type)
    argument
  when :sass_boolean
    value = SassC::Native.boolean_get_value(native_value)
    argument = SassC::Script::Value::Bool.new(value)
    argument
  when :sass_number
    value = SassC::Native.number_get_value(native_value)
    unit = SassC::Native.number_get_unit(native_value)
    argument = SassC::Script::Value::Number.new(value, unit)
    argument
  when :sass_color
    red, green, blue, alpha = SassC::Native.color_get_r(native_value), SassC::Native.color_get_g(native_value), SassC::Native.color_get_b(native_value), SassC::Native.color_get_a(native_value)
    argument = SassC::Script::Value::Color.new(red:red, green:green, blue:blue, alpha:alpha)
    argument.options = options
    argument
  when :sass_map
    values = {}
    length = SassC::Native::map_get_length native_value
    (0..length-1).each do |index|
      key = SassC::Native::map_get_key(native_value, index)
      value = SassC::Native::map_get_value(native_value, index)
      values[from_native(key, options)] = from_native(value, options)
    end
    argument = SassC::Script::Value::Map.new values
    argument
  when :sass_list
    length = SassC::Native::list_get_length(native_value)
    items = (0...length).map do |index|
      native_item = SassC::Native::list_get_value(native_value, index)
      from_native(native_item, options)
    end
    SassC::Script::Value::List.new(items, separator: :space)
  else
    raise UnsupportedValue.new("Sass argument of type #{value_tag} unsupported")
  end
end

.to_native(value) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sassc/script/value_conversion.rb', line 50

def self.to_native(value)
  case value_name = value.class.name.split("::").last
  when "String"
    SassC::Script::ValueConversion::String.new(value).to_native
  when "Color"
    SassC::Script::ValueConversion::Color.new(value).to_native
  when "Number"
    SassC::Script::ValueConversion::Number.new(value).to_native
  when "Map"
    SassC::Script::ValueConversion::Map.new(value).to_native
  when "List"
    SassC::Script::ValueConversion::List.new(value).to_native
  when "Bool"
    SassC::Script::ValueConversion::Bool.new(value).to_native
  else
    raise SassC::UnsupportedValue.new("Sass return type #{value_name} unsupported")
  end
end