Module: Locomotive::Steam::Liquid::Filters::Array

Defined in:
lib/locomotive/steam/liquid/filters/array.rb

Instance Method Summary collapse

Instance Method Details

#in_groups_of(array, number, fill_with = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/locomotive/steam/liquid/filters/array.rb', line 36

def in_groups_of(array, number, fill_with = nil)
  return array unless array.respond_to?(:all) || array.respond_to?(:each_slice)

  number        = number.to_i
  grouped_array = array.respond_to?(:all) ? array.all : array

  if fill_with != false
    # size % number gives how many extra we have;
    # subtracting from number gives how many to add;
    # modulo number ensures we don't add group of just fill.
    padding = (number - grouped_array.size % number) % number
    grouped_array = grouped_array + ::Array.new(padding, fill_with)
  end

  grouped_array.each_slice(number).to_a
end

#pop(array, input = 1) ⇒ Object



8
9
10
11
12
13
# File 'lib/locomotive/steam/liquid/filters/array.rb', line 8

def pop(array, input = 1)
  return array unless array.is_a?(::Array)
  new_ary = array.dup
  new_ary.pop(input.to_i || 1)
  new_ary
end

#push(array, input) ⇒ Object



15
16
17
18
19
20
# File 'lib/locomotive/steam/liquid/filters/array.rb', line 15

def push(array, input)
  return array unless array.is_a?(::Array)
  new_ary = array.dup
  new_ary.push(input)
  new_ary
end

#shift(array, input = 1) ⇒ Object



22
23
24
25
26
27
# File 'lib/locomotive/steam/liquid/filters/array.rb', line 22

def shift(array, input = 1)
  return array unless array.is_a?(::Array)
  new_ary = array.dup
  new_ary.shift(input.to_i || 1)
  new_ary
end

#unshift(array, input) ⇒ Object



29
30
31
32
33
34
# File 'lib/locomotive/steam/liquid/filters/array.rb', line 29

def unshift(array, input)
  return array unless array.is_a?(::Array)
  new_ary = array.dup
  new_ary.unshift(*input)
  new_ary
end