Extending Sass


Advertisements

You can extend the functionality of SASS to provide different types of features and customizations for users. To make use of these features, user should have knowledge of Ruby.

Defining Custom SASS Functions

You can define your own SASS functions while using Ruby API. You can add your custom functions by adding them to Ruby methods as shown in the following code −

module Sass::Script::Functions
   def reverse(string)
      assert_type string, :String
      Sass::Script::Value::String.new(string.value.reverse)
   end
   declare :reverse, [:string]
end

In the code you could see, the Function, declare, specifies the argument names for the function. If it fails then it will not accept any arguments even if the function is working and it also takes arbitrary keyword arguments. You can get Ruby values by using value accessor and access the color objects by using rgb, red, green, or blue.

Cache Stores

SASS stores cache of parsed documents, which can be reused without parsing again. SASS uses :cache_location to write cache files on the file system. It makes compilation of SASS files faster and if you delete cached files, they will be generated again when you compile next time. You can define your own cache store by setting the :cache_store option. This will write cache files on the file system or share cache files to ruby processes or machines. SASS uses instance of subclass of Sass::CacheStores::Base to store and retrieve cache results.

Custom Importers

SASS uses @import to import SCSS and SASS files and passes paths to @import rule to find an appropriate path code for specified paths. SASS importers use file system for loading the code and added to the load using database or different file naming scheme.

Single importer can take single file loading and can be placed in :load_paths array along with the paths of file system. While using @import, SASS looks for loaded paths, which import the path for the importer. When the path is found, the imported file is used. A user can inherit the importers from Sass::Importers::Base.

Advertisements