Module: Liquidize::Helper

Defined in:
lib/liquidize/helper.rb

Class Method Summary collapse

Class Method Details

.activerecord?(object) ⇒ Boolean

Checks if the object is an ActiveRecord class or instance

Parameters:

  • object (Object)

    any object

Returns:

  • (Boolean)

    whether it is AR class or instance



43
44
45
46
47
48
49
50
# File 'lib/liquidize/helper.rb', line 43

def self.activerecord?(object)
  return false unless defined?(ActiveRecord)
  if object.is_a?(Class)
    object.ancestors.include?(ActiveRecord::Base)
  else
    object.class.ancestors.include?(ActiveRecord::Base)
  end
end

.decode(dump) ⇒ Object

TODO:

Find better alternative to Marshal.load

Decodes dump into the Ruby object rubocop:disable Security/MarshalLoad

Parameters:

  • dump (String)

    encoded dump

Returns:

  • (Object)

    decoded object



28
29
30
# File 'lib/liquidize/helper.rb', line 28

def self.decode(dump)
  Marshal.load(Base64.strict_decode64(dump))
end

.encode(value) ⇒ String

Encodes Ruby object into marshalled dump

Parameters:

  • value (Object)

    Ruby object

Returns:

  • (String)

    encoded dump



19
20
21
# File 'lib/liquidize/helper.rb', line 19

def self.encode(value)
  Base64.strict_encode64(Marshal.dump(value))
end

.present?(value) ⇒ Boolean

Analogue of the ActiveSupport #present? method

Parameters:

  • value (Object)

    value that should be checked

Returns:

  • (Boolean)

    whether value is present



36
37
38
# File 'lib/liquidize/helper.rb', line 36

def self.present?(value)
  !value.to_s.strip.empty?
end

.recursive_stringify_keys(options) ⇒ Hash

Converts all keys to strings

Returns:

  • (Hash)

    the same hash with stringified keys



6
7
8
9
10
11
12
13
14
# File 'lib/liquidize/helper.rb', line 6

def self.recursive_stringify_keys(options)
  if options.is_a?(Hash)
    options.stringify_keys!
    options.each_value { |v| recursive_stringify_keys(v) }
  elsif options.is_a?(Array)
    options.map! { |a| recursive_stringify_keys(a) }
  end
  options
end