Module: SassC::Script

Defined in:
lib/sassc/script.rb,
lib/sassc/script/string.rb,
lib/sassc/script/functions.rb

Defined Under Namespace

Modules: Functions Classes: String

Class Method Summary collapse

Class Method Details

.custom_functionsObject



3
4
5
6
7
# File 'lib/sassc/script.rb', line 3

def self.custom_functions
  Functions.instance_methods.select do |function|
    Functions.public_method_defined?(function)
  end
end

.formatted_function_name(function_name) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/sassc/script.rb', line 60

def self.formatted_function_name(function_name)
  params = Functions.instance_method(function_name).parameters
  params = params.select { |param| param[0] == :req }
                 .map(&:first)
                 .map { |p| "$#{p}" }
                 .join(", ")
  "#{function_name}(#{params})"
end

.setup_custom_functions(options, sass_options) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
# File 'lib/sassc/script.rb', line 9

def self.setup_custom_functions(options, sass_options)
  callbacks = {}

  list = Native.make_function_list(custom_functions.count)

  functs = Class.new.extend(Functions)
  def functs.options=(opts)
    @sass_options = opts
  end
  def functs.options
    @sass_options
  end
  functs.options = sass_options

  custom_functions.each_with_index do |custom_function, i|
    callbacks[custom_function] = FFI::Function.new(:pointer, [:pointer, :pointer]) do |s_args, cookie|
      length = Native.list_get_length(s_args)

      v = Native.list_get_value(s_args, 0)
      v = Native.string_get_value(v).dup

      s = String.new(String.unquote(v), String.type(v))

      value = functs.send(custom_function, s)

      if value
        value = String.new(String.unquote(value.to_s), value.type)
        value.to_native
      else
        String.new("").to_native
      end
    end

    callback = Native.make_function(
      formatted_function_name(custom_function),
      callbacks[custom_function],
      nil
    )

    Native::function_set_list_entry(list, i, callback)
  end

  Native::option_set_c_functions(options, list)

  status = yield

  callbacks

  status
end