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
-
#assert_array!(options, key, default = nil) ⇒ Array
Ensures that hash value specified by key is Array.
-
#assert_positive_integer!(options, key, default = nil) ⇒ Fixnum
Ensures that hash value specified by key can be converted to positive integer.
-
#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.
-
#camelize(str, first_letter_upper = false) ⇒ String
Converts string from snake_case to CamelCase.
-
#camelize_keys(item) ⇒ Object
Deeply capitalizes Array values or Hash keys.
-
#constantize(const_name) ⇒ Class, NilClass
Tries to find a class with the name specified in the argument string.
-
#each_subset(array) ⇒ Object
Yields block for each subset of given array.
-
#parse_time(str) ⇒ Time
Converts string of the form YYYYmmddHHMMSS (considered as UTC) to Time.
-
#subsets_of(array) ⇒ Object
Returs all array’s subsets.
-
#symbolize_keys(h) ⇒ Object
Symbolizes hash keys.
-
#titleize(str) ⇒ String
Capitalizes the first letter of each word in string.
-
#underscore(str) ⇒ String
Converts string from CamelCase to snake_case.
-
#uniqid ⇒ String
Generates uniq random string.
Instance Method Details
#assert_array!(options, key, default = nil) ⇒ Array
Ensures that hash value specified by key is Array
24 25 26 27 28 29 |
# File 'lib/pulse_meter/mixins/utils.rb', line 24 def assert_array!(, key, default = nil) value = [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.
38 39 40 41 42 43 44 |
# File 'lib/pulse_meter/mixins/utils.rb', line 38 def assert_positive_integer!(, key, default = nil) value = [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 [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.
55 56 57 58 59 60 61 62 |
# File 'lib/pulse_meter/mixins/utils.rb', line 55 def assert_ranged_float!(, key, from, to) f = [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 [key] = f end |
#camelize(str, first_letter_upper = false) ⇒ String
Converts string from snake_case to CamelCase
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
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
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
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
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
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
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 |
#uniqid ⇒ String
Generates uniq random string
66 67 68 |
# File 'lib/pulse_meter/mixins/utils.rb', line 66 def uniqid SecureRandom.hex(32) end |