module Compass::SassExtensions::Functions::Lists

Public Instance Methods

_compass_list(arg) click to toggle source

Returns a list object from a value that was passed. This can be used to unpack a space separated list that got turned into a string by sass before it was passed to a mixin.

# File lib/compass/sass_extensions/functions/lists.rb, line 43
def _compass_list(arg)
  if arg.is_a?(Sass::Script::List)
    Sass::Script::List.new(arg.value.dup, arg.separator)
  else
    Sass::Script::List.new([arg], :space)
  end
end
_compass_list_size(list) click to toggle source

Returns the size of the list.

# File lib/compass/sass_extensions/functions/lists.rb, line 62
def _compass_list_size(list)
  assert_list list
  Sass::Script::Number.new(list.value.size)
end
_compass_nth(list, place) click to toggle source

Get the nth value from a list

# File lib/compass/sass_extensions/functions/lists.rb, line 29
def _compass_nth(list, place)
  assert_type list, :List
  if place.value == "first"
    list.value.first
  elsif place.value == "last"
    list.value.last
  else
    list.value[place.value - 1]
  end
end
_compass_slice(list, start_index, end_index = nil) click to toggle source

slice a sublist from a list

# File lib/compass/sass_extensions/functions/lists.rb, line 68
def _compass_slice(list, start_index, end_index = nil)
  end_index ||= Sass::Script::Number.new(-1)
  start_index = start_index.value
  end_index = end_index.value
  start_index -= 1 unless start_index < 0
  end_index -= 1 unless end_index < 0
  Sass::Script::List.new list.values[start_index..end_index], list.separator
end
_compass_space_list(list) click to toggle source

If the argument is a list, it will return a new list that is space delimited Otherwise it returns a new, single element, space-delimited list.

# File lib/compass/sass_extensions/functions/lists.rb, line 53
def _compass_space_list(list)
  if list.is_a?(Sass::Script::List)
    Sass::Script::List.new(list.value.dup, :space)
  else
    Sass::Script::List.new([list], :space)
  end
end
blank(obj) click to toggle source

Returns true when the object is false, an empty string, or an empty list

# File lib/compass/sass_extensions/functions/lists.rb, line 4
def blank(obj)
  case obj
  when Sass::Script::Bool
    Sass::Script::Bool.new !obj.to_bool
  when Sass::Script::String
    Sass::Script::Bool.new obj.value.strip.size == 0
  when Sass::Script::List
    Sass::Script::Bool.new obj.value.size == 0 || obj.value.all?{|el| blank(el).to_bool}
  else
    Sass::Script::Bool.new false
  end
end
compact(*args) click to toggle source

Returns a new list after removing any non-true values

# File lib/compass/sass_extensions/functions/lists.rb, line 18
def compact(*args)
  sep = :comma
  if args.size == 1 && args.first.is_a?(Sass::Script::List)
    list = args.first
    args = list.value
    sep = list.separator
  end
  Sass::Script::List.new(args.reject{|a| !a.to_bool}, sep)
end
first_value_of(list) click to toggle source

returns the first value of a space delimited list.

# File lib/compass/sass_extensions/functions/lists.rb, line 78
def first_value_of(list)
  if list.is_a?(Sass::Script::String)
    Sass::Script::String.new(list.value.split(%r\s+/).first)
  elsif defined?(Sass::Script::List) && list.is_a?(Sass::Script::List)
    list.value.first
  else
    list
  end
end

Protected Instance Methods

assert_list(value) click to toggle source
# File lib/compass/sass_extensions/functions/lists.rb, line 90
def assert_list(value)
  unless value.is_a?(Sass::Script::List)
    raise ArgumentError.new("#{value.inspect} is not a list")
  end
end