Module: Flint

Defined in:
lib/flint.rb,
lib/flint/version.rb,
lib/flint/functions.rb

Constant Summary collapse

VERSION =
"2.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.declare(*args) ⇒ Object



3
4
5
# File 'lib/flint/functions.rb', line 3

def self.declare(*args)
  Sass::Script::Functions.declare(*args)
end

Instance Method Details

#flint_ruby_exists(map, key) ⇒ Bool

Check if key exists in map

Parameters:

  • map (Map)
    • map to search

  • key (String)
    • key to search for

Returns:

  • (Bool)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/flint/functions.rb', line 107

def flint_ruby_exists(map, key)
  assert_type map, :Map, :map
  assert_type key, :String, :key

  hash = map.to_h

  if hash.fetch(key, false)
    return Sass::Script::Bool.new(true)
  else
    hash.each do |_, value|
      if value.is_a?(Sass::Script::Value::Map)
        return Sass::Script::Bool.new(true) if flint_ruby_exists(value, key).value
      end
    end
  end

  Sass::Script::Bool.new(false)
end

#flint_ruby_list_to_string(list, glue) ⇒ String

Joins all elements of list with passed glue

Parameters:

  • list (List)
  • glue (String)

Returns:

  • (String)


46
47
48
49
50
51
52
53
# File 'lib/flint/functions.rb', line 46

def flint_ruby_list_to_string(list, glue)
  assert_type list, :List, :list
  assert_type glue, :String, :glue

  arr = list.to_a.flatten.map { |item| item.value }

  Sass::Script::String.new(arr.join(glue.value))
end

#flint_ruby_map_fetch(map, *keys) ⇒ *

Fetch value from map

Parameters:

  • map (Map)
    • map to fetch value from

  • keys (ArgList)
    • list of keys to traverse

Returns:

  • (*)


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/flint/functions.rb', line 24

def flint_ruby_map_fetch(map, *keys)
  assert_type map, :Map, :map

  result = map
  keys.each { |key| result.nil? ? break : result = result.to_h.fetch(key, nil) }

  unless result.nil?
    result
  else
    Sass::Script::Bool.new(false)
  end
end

#flint_ruby_replace_substring(string, find, replace) ⇒ String

Replace substring with string

Parameters:

  • string (String)
    • string that contains substring

  • find (String)
    • substring to replace

  • replace (String)
    • new string to replace sub with

Returns:

  • (String)


90
91
92
93
94
95
96
# File 'lib/flint/functions.rb', line 90

def flint_ruby_replace_substring(string, find, replace)
  assert_type string, :String, :string
  assert_type find, :String, :find
  assert_type replace, :String, :replace

  Sass::Script::String.new(string.value.gsub(find.value, replace.value))
end

#flint_ruby_string_to_list(string, separator, ignore) ⇒ List

Turn string into a flat list

Parameters:

  • string (String)
    • string to operate on

  • separator (String)
    • item to find which separates substrings

  • ignore (String)
    • removes remaining string beyond item

Returns:

  • (List)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/flint/functions.rb', line 65

def flint_ruby_string_to_list(string, separator, ignore)
  assert_type string, :String, :string
  assert_type separator, :String, :separator
  assert_type ignore, :String, :ignore

  # Remove everything after ignore, split with separator
  items = string.value[/[^#{ignore}]+/].split(separator.value)

  if items.count == 1
    Sass::Script::String.new(items[0], :comma)
  else
    Sass::Script::List.new(items.map { |i| Sass::Script::String.new(i) }, :comma)
  end
end

#flint_use_rubyBool

Use ruby functions

Returns:

  • (Bool)


12
13
14
# File 'lib/flint/functions.rb', line 12

def flint_use_ruby()
  Sass::Script::Bool.new(true)
end