From 7406a627f8fcfbebea49521f9dbd2e12d54c3a29 Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Sat, 13 Nov 2010 00:46:41 -0800 Subject: [PATCH] A new function: if() --- doc-src/content/CHANGELOG.markdown | 2 ++ lib/compass/sass_extensions/functions.rb | 3 ++- lib/compass/sass_extensions/functions/if.rb | 9 +++++++++ test/sass_extensions_test.rb | 5 +++++ 4 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 lib/compass/sass_extensions/functions/if.rb diff --git a/doc-src/content/CHANGELOG.markdown b/doc-src/content/CHANGELOG.markdown index c39cea10..a4aa151f 100644 --- a/doc-src/content/CHANGELOG.markdown +++ b/doc-src/content/CHANGELOG.markdown @@ -47,6 +47,8 @@ COMPASS CHANGELOG If you provide a number with units of `deg` then it will return a unitless number after converting to radians. Otherwise, it assumes the number is a radian length measure and passes the units along to the result. +* A new function `if()` that allows you to switch on a value without using `@if`. + Usage: `if($truth-value, $value-if-true, $value-if-false)`. ### Rails diff --git a/lib/compass/sass_extensions/functions.rb b/lib/compass/sass_extensions/functions.rb index 922268e4..b8e566e6 100644 --- a/lib/compass/sass_extensions/functions.rb +++ b/lib/compass/sass_extensions/functions.rb @@ -2,7 +2,7 @@ module Compass::SassExtensions::Functions end %w( - selectors enumerate urls display + selectors enumerate urls display if inline_image image_size gradient_support font_files constants lists colors trig ).each do |func| @@ -22,6 +22,7 @@ module Sass::Script::Functions include Compass::SassExtensions::Functions::Lists include Compass::SassExtensions::Functions::Colors include Compass::SassExtensions::Functions::Trig + include Compass::SassExtensions::Functions::If end # Wierd that this has to be re-included to pick up sub-modules. Ruby bug? diff --git a/lib/compass/sass_extensions/functions/if.rb b/lib/compass/sass_extensions/functions/if.rb new file mode 100644 index 00000000..98c1591d --- /dev/null +++ b/lib/compass/sass_extensions/functions/if.rb @@ -0,0 +1,9 @@ +module Compass::SassExtensions::Functions::If + def if(truth, if_true, if_false) + if truth.to_bool + if_true + else + if_false + end + end +end diff --git a/test/sass_extensions_test.rb b/test/sass_extensions_test.rb index 601ff0c4..bee9a8be 100644 --- a/test/sass_extensions_test.rb +++ b/test/sass_extensions_test.rb @@ -59,6 +59,11 @@ class SassExtensionsTest < Test::Unit::TestCase assert_equal "25%", evaluate("saturation(adjust-saturation(hsl(50deg, 50%, 50%), -25%))") end + def test_if_function + assert_equal "no", evaluate("if(false, yes, no)") + assert_equal "yes", evaluate("if(true, yes, no)") + end + def test_trig_functions assert_equal "0.841px", evaluate("sin(1px)") assert_equal "0.0", evaluate("sin(pi())")