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
-
#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.
-
#symbolize_keys(h) ⇒ Object
Symbolizes hash keys.
-
#titleize(str) ⇒ String
Capitalizes the first letter of each word in string.
-
#uniqid ⇒ String
Generates uniq random string.
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.
25 26 27 28 29 30 31 |
# File 'lib/pulse-meter/mixins/utils.rb', line 25 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.
42 43 44 45 46 47 48 49 |
# File 'lib/pulse-meter/mixins/utils.rb', line 42 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
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
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
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 |
#uniqid ⇒ String
Generates uniq random string
53 54 55 |
# File 'lib/pulse-meter/mixins/utils.rb', line 53 def uniqid SecureRandom.hex(32) end |