Module: PulseMeter::Mixins::Utils

Included in:
Observer, Sensor::Configuration, Sensor::Multi, Sensor::Timeline
Defined in:
lib/pulse_meter/mixins/utils.rb

Overview

Mixin with various useful functions

Instance Method Summary collapse

Instance Method Details

#assert_array!(options, key, default = nil) ⇒ Array

Ensures that hash value specified by key is Array

Parameters:

  • options (Hash)

    hash to be looked at

  • key (Object)

    hash key

  • default (Object) (defaults to: nil)

    default value to be returned

Returns:

  • (Array)

Raises:

  • (ArgumentError)

    unless value is Array



24
25
26
27
28
29
# File 'lib/pulse_meter/mixins/utils.rb', line 24

def assert_array!(options, key, default = nil)
  value = options[key] || default
  raise ArgumentError, "#{key} should be defined" unless value
  raise ArgumentError, "#{key} should be array" unless value.is_a?(Array)
  value
end

#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



38
39
40
41
42
43
44
# File 'lib/pulse_meter/mixins/utils.rb', line 38

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



55
56
57
58
59
60
61
62
# File 'lib/pulse_meter/mixins/utils.rb', line 55

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



84
85
86
87
88
89
# File 'lib/pulse_meter/mixins/utils.rb', line 84

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



131
132
133
134
135
136
137
138
139
140
# File 'lib/pulse_meter/mixins/utils.rb', line 131

def camelize_keys(item)
  case item
  when Array
    item.map{|i| camelize_keys(i)}
  when Hash
    item.each_with_object({}) { |(k, v), h| h[camelize(k)] = camelize_keys(v)}
  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

#each_subset(array) ⇒ Object

Yields block for each subset of given array

Parameters:

  • array (Array)

    given array



144
145
146
# File 'lib/pulse_meter/mixins/utils.rb', line 144

def each_subset(array)
  subsets_of(array).each {|subset| yield(subset)}
end

#parse_time(str) ⇒ Time

Converts string of the form YYYYmmddHHMMSS (considered as UTC) to Time

Parameters:

  • str (String)

    string to be converted

Returns:

  • (Time)

Raises:

  • (ArgumentError)

    unless passed value responds to to_s



108
109
110
111
112
113
114
115
116
# File 'lib/pulse_meter/mixins/utils.rb', line 108

def parse_time(str)
  raise ArgumentError unless str.respond_to?(:to_s)
  m = str.to_s.match(/\A(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})\z/)
  if m
    Time.gm(*m.captures.map(&:to_i))
  else
    raise ArgumentError, "`#{str}' is not a YYYYmmddHHMMSS time"
  end
end

#subsets_of(array) ⇒ Object

Returs all array’s subsets

Parameters:

  • array (Array)


150
151
152
# File 'lib/pulse_meter/mixins/utils.rb', line 150

def subsets_of(array)
  0.upto(array.length).flat_map { |n| array.combination(n).to_a }
end

#symbolize_keys(h) ⇒ Object

Symbolizes hash keys



119
120
121
122
123
124
125
126
127
128
# File 'lib/pulse_meter/mixins/utils.rb', line 119

def symbolize_keys(h)
  h.each_with_object({}) do |(k, v), acc|
    new_k = if k.respond_to?(:to_sym)
      k.to_sym
    else
      k
    end
    acc[new_k] = v
  end
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



74
75
76
77
# File 'lib/pulse_meter/mixins/utils.rb', line 74

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

#underscore(str) ⇒ String

Converts string from CamelCase to snake_case

Parameters:

  • str (String)

    string to be underscore

Returns:

  • (String)

Raises:

  • (ArgumentError)

    unless passed value responds to to_s



95
96
97
98
99
100
101
102
# File 'lib/pulse_meter/mixins/utils.rb', line 95

def underscore(str)
  raise ArgumentError unless str.respond_to?(:to_s)
  str.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end

#uniqidString

Generates uniq random string

Returns:

  • (String)


66
67
68
# File 'lib/pulse_meter/mixins/utils.rb', line 66

def uniqid
  SecureRandom.hex(32)
end