Module: FatZebra::Util

Defined in:
lib/fat_zebra/util.rb

Constant Summary collapse

DATE_FORMAT =
'%Y/%m/%d'.freeze
REGEXP_HTTP =
%r{http[s]?\:\/\/}

Class Method Summary collapse

Class Method Details

.camelize(string) ⇒ Object

Convert string to camelize

Examples:

camelize('my_model') => 'MyModel'

Parameters:

  • (String)


28
29
30
# File 'lib/fat_zebra/util.rb', line 28

def camelize(string)
  string.split('_').map(&:capitalize).join
end

.cleanup_host(uri) ⇒ Object



8
9
10
# File 'lib/fat_zebra/util.rb', line 8

def cleanup_host(uri)
  uri.gsub(REGEXP_HTTP, '')
end

.compact(hash) ⇒ Hash

Remove nil value from hash

Returns:

  • (Hash)


16
17
18
19
# File 'lib/fat_zebra/util.rb', line 16

def compact(hash)
  # Can use Hash#compact when Ruby 2.3 compatability is dropped
  hash.reject { |_, value| value.nil? }
end

.format_dates_in_hash(hash) ⇒ Hash

Format Date attribute in the params

Parameters:

  • params (Hash)

Returns:

  • (Hash)

    date formated params



60
61
62
63
64
65
66
67
# File 'lib/fat_zebra/util.rb', line 60

def format_dates_in_hash(hash)
  # Can use Hash#transform_values! when Ruby 2.3 compatablility is dropped
  hash.each do |(key, value)|
    hash[key] = value.strftime DATE_FORMAT if value.respond_to? :strftime
  end

  hash
end

.hash_except(hash, *keys) ⇒ Object



48
49
50
51
52
# File 'lib/fat_zebra/util.rb', line 48

def hash_except(hash, *keys)
  copy = hash.dup
  keys.each { |key| copy.delete(key) }
  copy
end

.underscore(string) ⇒ Object

Convert string to underscore

Examples:

underscore('MyModel') => 'my_model'

Parameters:

  • (String)


39
40
41
42
43
44
45
46
# File 'lib/fat_zebra/util.rb', line 39

def underscore(string)
  string
    .gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr('-', '_')
    .downcase
end