Skip to content

Commit bea8df0

Browse files
committed
Merge pull request #38 from TeaSeaLancs/add-boolean
Add boolean script support
2 parents 9518b88 + 7453bdb commit bea8df0

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

lib/sassc/native/native_functions_api.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ module Native
2626

2727
# ADDAPI union Sass_Value* ADDCALL sass_make_map (size_t len);
2828
attach_function :sass_make_map, [:size_t], :sass_value_ptr
29+
30+
# ADDAPI union Sass_Value* ADDCALL sass_make_boolean (boolean val);
31+
attach_function :sass_make_boolean, [:bool], :sass_value_ptr
2932

3033
# ADDAPI void ADDCALL sass_map_set_key (union Sass_Value* v, size_t i, union Sass_Value*);
3134
attach_function :sass_map_set_key, [:sass_value_ptr, :size_t, :sass_value_ptr], :void
@@ -60,6 +63,9 @@ module Native
6063

6164
# ADDAPI const char* ADDCALL sass_number_get_unit (const union Sass_Value* v);
6265
attach_function :sass_number_get_unit, [:sass_value_ptr], :string
66+
67+
# ADDAPI const char* ADDCALL sass_boolean_get_value (const union Sass_Value* v);
68+
attach_function :sass_boolean_get_value, [:sass_value_ptr], :bool
6369

6470
def self.string_get_type(native_value)
6571
string_is_quoted(native_value) ? :string : :identifier

lib/sassc/script.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ module Script
3131
require 'sass/script/value/base'
3232
require 'sass/script/value/string'
3333
require 'sass/script/value/color'
34+
require 'sass/script/value/bool'
3435

3536
SassC::Script::String = Sass::Script::Value::String
3637
SassC::Script::Value::String = Sass::Script::Value::String
3738

3839
SassC::Script::Color = Sass::Script::Value::Color
3940
SassC::Script::Value::Color = Sass::Script::Value::Color
41+
42+
SassC::Script::Bool = Sass::Script::Value::Bool
43+
SassC::Script::Value::Bool = Sass::Script::Value::Bool

lib/sassc/script/value_conversion.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ def self.from_native(native_value, options)
1010
type = Native.string_get_type(native_value)
1111
argument = Script::String.new(value, type)
1212

13+
argument
14+
when :sass_boolean
15+
value = Native.boolean_get_value(native_value)
16+
argument = Script::Bool.new(value)
17+
1318
argument
1419
when :sass_number
1520
value = Native.number_get_value(native_value)
@@ -51,6 +56,8 @@ def self.to_native(value)
5156
Number.new(value).to_native
5257
when "Map"
5358
Map.new(value).to_native
59+
when "Bool"
60+
Bool.new(value).to_native
5461
else
5562
raise UnsupportedValue.new("Sass return type #{value_name} unsupported")
5663
end
@@ -64,3 +71,4 @@ def self.to_native(value)
6471
require_relative "value_conversion/number"
6572
require_relative "value_conversion/color"
6673
require_relative "value_conversion/map"
74+
require_relative "value_conversion/bool"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module SassC
2+
module Script
3+
module ValueConversion
4+
class Bool < Base
5+
def to_native
6+
Native::make_boolean(@value.value)
7+
end
8+
end
9+
end
10+
end
11+
end

test/functions_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ def test_function_that_takes_a_number
8080
CSS
8181
end
8282

83+
def test_function_that_returns_a_bool
84+
assert_sass <<-SCSS, <<-CSS
85+
div { width: returns-a-bool(); }
86+
SCSS
87+
div { width: true; }
88+
CSS
89+
end
90+
91+
def test_function_that_takes_a_bool
92+
assert_sass <<-SCSS, <<-CSS
93+
div { display: inspect-bool(true)}
94+
SCSS
95+
div { display: true; }
96+
CSS
97+
end
98+
8399
def test_function_with_optional_arguments
84100
assert_sass <<-SCSS, <<-EXPECTED_CSS
85101
div {
@@ -199,6 +215,15 @@ def returns_a_number
199215
return Sass::Script::Value::Number.new(-312,'rem')
200216
end
201217

218+
def returns_a_bool
219+
return Sass::Script::Value::Bool.new(true)
220+
end
221+
222+
def inspect_bool ( argument )
223+
raise StandardError.new "passed value is not a Sass::Script::Value::Bool" unless argument.is_a? Sass::Script::Value::Bool
224+
return argument
225+
end
226+
202227
def inspect_number ( argument )
203228
raise StandardError.new "passed value is not a Sass::Script::Value::Number" unless argument.is_a? Sass::Script::Value::Number
204229
return argument

0 commit comments

Comments
 (0)