Module: Locomotive::Steam::Liquid::Filters::Misc

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

Instance Method Summary collapse

Instance Method Details

#default(input, value) ⇒ Object



21
22
23
# File 'lib/locomotive/steam/liquid/filters/misc.rb', line 21

def default(input, value)
  input.blank? ? value : input
end

#hexdigest(input, key, digest = nil) ⇒ Object



47
48
49
# File 'lib/locomotive/steam/liquid/filters/misc.rb', line 47

def hexdigest(input, key, digest = nil)
  OpenSSL::HMAC.hexdigest(digest || 'sha1', key, input)
end

#index(array, position) ⇒ Object

Get the nth element of the passed in array



13
14
15
# File 'lib/locomotive/steam/liquid/filters/misc.rb', line 13

def index(array, position)
  array.at(position) if array.respond_to?(:at)
end

#map(input, property) ⇒ Object

map/collect on a given property (support to_f, to_i)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/locomotive/steam/liquid/filters/misc.rb', line 30

def map(input, property)
  ::Liquid::StandardFilters::InputIterator.new(input).map do |e|
    e = e.call if e.is_a?(Proc)

    if property == 'to_liquid'.freeze
      e
    elsif property == 'to_f'.freeze
      e.to_f
    elsif property == 'to_i'.freeze
      e.to_i
    elsif e.respond_to?(:[])
      e[property]
    end
  end

end

#random(input) ⇒ Object



25
26
27
# File 'lib/locomotive/steam/liquid/filters/misc.rb', line 25

def random(input)
  rand(input.to_i)
end

#shuffle(array) ⇒ Object



17
18
19
# File 'lib/locomotive/steam/liquid/filters/misc.rb', line 17

def shuffle(array)
  array.to_a.shuffle
end

#str_modulo(word, index, modulo) ⇒ Object

was called modulo at first



8
9
10
# File 'lib/locomotive/steam/liquid/filters/misc.rb', line 8

def str_modulo(word, index, modulo)
  (index.to_i + 1) % modulo == 0 ? word : ''
end