Module: PulseMeter::Mixins::Utils

Included in:
Sensor::Configuration, Sensor::Timeline
Defined in:
lib/pulse-meter/mixins/utils.rb

Overview

Mixin with various useful functions

Instance Method Summary collapse

Instance Method Details

#assert_positive_integer!(options, key, default = nil) ⇒ Fixnum

Ensures that hash value specified by key can be converted to positive integer. In case it can makes in-place conversion and returns the value.

Parameters:

  • options (Hash)

    hash to be looked at

  • key (Object)

    hash key

  • default (Object) (defaults to: nil)

    default value to be returned

Returns:

  • (Fixnum)

Raises:

  • (ArgumentError)

    unless value is positive integer



25
26
27
28
29
30
31
# File 'lib/pulse-meter/mixins/utils.rb', line 25

def assert_positive_integer!(options, key, default = nil)
  value = options[key] || default
  raise ArgumentError, "#{key} should be defined" unless value
  raise ArgumentError, "#{key} should be integer" unless value.respond_to?(:to_i)
  raise ArgumentError, "#{key} should be positive" unless value.to_i > 0
  options[key] = value.to_i
end

#assert_ranged_float!(options, key, from, to) ⇒ Float

Ensures that hash value specified by key is can be converted to float and it is within given range. In case it can makes in-place conversion and returns the value.

Parameters:

  • options (Hash)

    hash to be looked at

  • key (Object)

    hash key

  • from (Float)

    lower bound

  • to (Float)

    upper bound

Returns:

  • (Float)

Raises:

  • (ArgumentError)

    unless value is float within given range



42
43
44
45
46
47
48
49
# File 'lib/pulse-meter/mixins/utils.rb', line 42

def assert_ranged_float!(options, key, from, to)
  f = options[key]
  raise ArgumentError, "#{key} should be defined" unless f
  raise ArgumentError, "#{key} should be float" unless f.respond_to?(:to_f)
  f = f.to_f
  raise ArgumentError, "#{key} should be between #{from} and #{to}" unless f >= from && f <= to
  options[key] = f
end

#camelize(str, first_letter_upper = false) ⇒ String

Converts string from snake_case to CamelCase

Parameters:

  • str (String)

    string to be camelized

  • first_letter_upper (TrueClass, FalseClass) (defaults to: false)

    says if the first letter must be uppercased

Returns:

  • (String)

Raises:

  • (ArgumentError)

    unless passed value responds to to_s



71
72
73
74
75
76
# File 'lib/pulse-meter/mixins/utils.rb', line 71

def camelize(str, first_letter_upper = false)
  raise ArgumentError unless str.respond_to?(:to_s)
  terms = str.to_s.split(/_/)
  first = terms.shift
  (first_letter_upper ? first.capitalize : first.downcase) + terms.map(&:capitalize).join
end

#camelize_keys(item) ⇒ Object

Deeply capitalizes Array values or Hash keys



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pulse-meter/mixins/utils.rb', line 79

def camelize_keys(item)
  case item
    when Array
      item.map{|i| camelize_keys(i)}
    when Hash
      h = {}
      item.each{ |k, v| h[camelize(k)] = camelize_keys(v)}
      h
  else
    item
  end
end

#constantize(const_name) ⇒ Class, NilClass

Tries to find a class with the name specified in the argument string

Parameters:

  • const_name (String)

    class name

Returns:

  • (Class)

    if given class definde

  • (NilClass)

    if given class is not defined



11
12
13
14
15
16
# File 'lib/pulse-meter/mixins/utils.rb', line 11

def constantize(const_name)
  return unless const_name.respond_to?(:to_s)
  const_name.to_s.split('::').reduce(Module, :const_get)
rescue NameError
  nil
end

#symbolize_keys(h) ⇒ Object

Symbolizes hash keys



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pulse-meter/mixins/utils.rb', line 93

def symbolize_keys(h)
  res = {}
  h.each do |k, v|
    new_k = if k.is_a?(String)
      k.to_sym
    else
      k
    end
    res[new_k] = v
  end
  res
end

#titleize(str) ⇒ String

Capitalizes the first letter of each word in string

Parameters:

  • str (String)

Returns:

  • (String)

Raises:

  • (ArgumentError)

    unless passed value responds to to_s



61
62
63
64
# File 'lib/pulse-meter/mixins/utils.rb', line 61

def titleize(str)
  raise ArgumentError unless str.respond_to?(:to_s)
  str.to_s.split(/[\s_]+/).map(&:capitalize).join(' ')
end

#uniqidString

Generates uniq random string

Returns:

  • (String)


53
54
55
# File 'lib/pulse-meter/mixins/utils.rb', line 53

def uniqid
  SecureRandom.hex(32)
end