Module: Apropos::SassFunctions

Included in:
Sass::Script::Functions
Defined in:
lib/apropos/sass_functions.rb

Overview

A collection of methods mixed into Sass::Script::Functions to allow Apropos to be used from Sass files. The primary method is ‘image-variants`, which generates the actual CSS rules. Configuration directly from the Sass file is possible with the `add-dpi-image-variant` and `add-breakpoint-image-variant` methods, although the limitations of Sass syntax require that the output of these functions be assigned to a dummy variable.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/apropos/sass_functions.rb', line 14

def self.included(mod)
  ::Sass::Script::Functions.declare :image_variants, []
  ::Sass::Script::Functions.declare :apropos_image_width, [:string]
  ::Sass::Script::Functions.declare :apropos_image_height, [:string]
  ::Sass::Script::Functions.declare :add_dpi_image_variant, []
  ::Sass::Script::Functions.declare :add_breakpoint_image_variant, []
  ::Sass::Script::Functions.declare :nth_polyfill, [:list, :index]
  ::Sass::Script::Functions.declare :str_contains, [:string, :substring]
end

.sass_function_exist?(meth) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/apropos/sass_functions.rb', line 10

def self.sass_function_exist?(meth)
  Sass::Script::Functions.instance_methods.include? meth
end

.value(val) ⇒ Object



24
25
26
# File 'lib/apropos/sass_functions.rb', line 24

def self.value(val)
  val.respond_to?(:value) ? val.value : val
end

Instance Method Details

#add_breakpoint_image_variant(id, query, sort = 0) ⇒ Object



74
75
76
77
78
# File 'lib/apropos/sass_functions.rb', line 74

def add_breakpoint_image_variant(id, query, sort=0)
  sort = ::Apropos::SassFunctions.value(sort)
  ::Apropos.add_breakpoint_image_variant(id.value, query.value, sort)
  ::Sass::Script::Bool.new(false)
end

#add_dpi_image_variant(id, query, sort = 0) ⇒ Object



68
69
70
71
72
# File 'lib/apropos/sass_functions.rb', line 68

def add_dpi_image_variant(id, query, sort=0)
  sort = ::Apropos::SassFunctions.value(sort)
  ::Apropos.add_dpi_image_variant(id.value, query.value, sort)
  ::Sass::Script::Bool.new(false)
end

#apropos_image_height(path) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/apropos/sass_functions.rb', line 54

def apropos_image_height(path)
  assert_type path, :String
  height = image_height(path)
  if ::Apropos.hidpi_only
    ::Sass::Script::Number.new(
      (height.value / 2).floor,
      height.numerator_units,
      height.denominator_units
    )
  else
    height
  end
end

#apropos_image_width(path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/apropos/sass_functions.rb', line 40

def apropos_image_width(path)
  assert_type path, :String
  width = image_width(path)
  if ::Apropos.hidpi_only
    ::Sass::Script::Number.new(
      (width.value / 2).floor,
      width.numerator_units,
      width.denominator_units
    )
  else
    width
  end
end

#image_variants(path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/apropos/sass_functions.rb', line 28

def image_variants(path)
  assert_type path, :String
  set = ::Apropos.image_set(path.value)
  set.invalid_variants.each do |variant|
    message = "Ignoring unknown extensions " +
      "'#{variant.invalid_codes.join("', '")}' (#{variant.path})"
    ::Sass.logger.info message
    $stderr.puts message
  end
  ::Apropos.convert_to_sass_value(set.valid_variant_rules)
end

#nth_polyfill(list, index) ⇒ Object

Can be replaced with stock ‘nth` once dca1498 makes it into a Sass release git.io/eGNOKA



82
83
84
85
86
87
# File 'lib/apropos/sass_functions.rb', line 82

def nth_polyfill(list, index)
  index = index.value
  list = list.value
  index = list.length + index + 1 if index < 0
  list[index - 1]
end

#str_contains(string, substring) ⇒ Object



89
90
91
92
93
# File 'lib/apropos/sass_functions.rb', line 89

def str_contains(string, substring)
  assert_type string, :String, :string
  assert_type substring, :String, :substring
  ::Sass::Script::Bool.new(string.value.include?(substring.value))
end