Module: Sass::Script::Functions::Dashy

Included in:
Sass::Script::Functions
Defined in:
lib/sass/script/functions/dashy.rb

Instance Method Summary collapse

Instance Method Details

#list_series(*lists) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sass/script/functions/dashy.rb', line 63

def list_series *lists
  lists.map! do |list|
    list = list.to_a
    sentinel = list.first

    first, second, operator, *rest, last = list.map &:value
    next list unless Numeric === last &&
                     Numeric === first &&
                     Numeric === second &&
                     /^_+$/ === operator &&
                     rest.all?(&Numeric.method(:===))

    delta = second - first
    case operator
    when "_"  then (first..  last)
    when "__" then (first... last)
    end.step(delta).map do |value|
      number_new value, sentinel.numerator_units, sentinel.denominator_units
    end
  end
  list_new lists.flatten, :comma
end

#select(*selectors) ⇒ Object



4
5
6
# File 'lib/sass/script/functions/dashy.rb', line 4

def select *selectors
  select_as unquoted_string(""), *selectors
end

#select_as(separator, *selectors) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sass/script/functions/dashy.rb', line 14

def select_as separator, *selectors
  unquote   = method :unquote
  separator = separator.value
  selectors = selectors.first if 1 == selectors.length
  selectors = selectors.to_a.map { |selector| selector.to_a.map &unquote }
  
  selectors = selectors.shift.product *selectors
  selectors.map! do |selector|
    selector = selector.join separator
    
    selector.gsub! %r{                 [*] (?=[\\\w\-\.\#\[]) }x, '' # Remove any wildcards before any tags, classes, ids or attributes.
    selector.gsub! %r{ (?<=[\\\w\-\]]) [*]                    }x, '' # Remove any wildcards after any words or attributes.

    select_new selector
  end
  
  list_new selectors
end

#select_either(*selectors) ⇒ Object



33
34
35
# File 'lib/sass/script/functions/dashy.rb', line 33

def select_either *selectors
  list_new selectors.map(&:to_a).flatten
end

#string_join(strings, separator) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/sass/script/functions/dashy.rb', line 43

def string_join strings, separator
  strings = strings.to_a
  type = strings.first.type
  strings = strings.map { |string| string.to_s quote: :none }
  separator = separator.to_s quote: :none
  string_new strings.join(separator), type
end

#to_fraction_from_number(num) ⇒ Object



90
91
92
# File 'lib/sass/script/functions/dashy.rb', line 90

def to_fraction_from_number num
  string_new Rational(num.value).rationalize(0.0001).to_s, :identifier
end

#to_number_from_fraction(str) ⇒ Object



86
87
88
# File 'lib/sass/script/functions/dashy.rb', line 86

def to_number_from_fraction str
  number_new eval "1.0 * #{ str.value }" if %r{ ^ [0-9]+ / [0-9]+ $ }x === str.value
end