Module: Swarm::Support
- Defined in:
- lib/swarm/support.rb
Class Method Summary collapse
- .camelize(string) ⇒ Object
- .combine_arrays(v1, v2, method: :concat) ⇒ Object
- .constantize(string) ⇒ Object
- .deep_merge(hsh1, hsh2, combine_arrays: :override) ⇒ Object
- .symbolize_keys(hsh) ⇒ Object
- .symbolize_keys!(hsh) ⇒ Object
- .tokenize(string) ⇒ Object
- .uuid_with_timestamp ⇒ Object
- .wait_until(timeout: 5, initial_delay: nil) ⇒ Object
Class Method Details
.camelize(string) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/swarm/support.rb', line 57 def camelize(string) string = string.sub(/^[a-z\d]*/) { ::Regexp.last_match(0).capitalize } string.gsub(%r{(?:_|(/))([a-z\d]*)}) { "#{::Regexp.last_match(1)}#{::Regexp.last_match(2).capitalize}" }.gsub('/', '::') end |
.combine_arrays(v1, v2, method: :concat) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/swarm/support.rb', line 31 def combine_arrays(v1, v2, method: :concat) case method.to_s when "uniq" v1 | v2 when "concat" v1.concat v2 when "override" v2 else raise ArgumentError, "unknown array combination method: #{method}" end end |
.constantize(string) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/swarm/support.rb', line 64 def constantize(string) name_parts = camelize(string).split('::') name_parts.shift if name_parts.first.empty? constant = Object name_parts.each do |name_part| const_defined_args = [name_part] const_defined_args << false unless Module.method(:const_defined?).arity == 1 constant_defined = constant.const_defined?(*const_defined_args) constant = constant_defined ? constant.const_get(name_part) : constant.const_missing(name_part) end constant end |
.deep_merge(hsh1, hsh2, combine_arrays: :override) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/swarm/support.rb', line 8 def deep_merge(hsh1, hsh2, combine_arrays: :override) hsh1.merge(hsh2) { |_key, v1, v2| if [v1, v2].all? { |v| v.is_a?(Array) } combine_arrays(v1, v2, method: combine_arrays) elsif [v1, v2].all? { |v| v.is_a?(Hash) } deep_merge(v1, v2) else v2 end } end |
.symbolize_keys(hsh) ⇒ Object
27 28 29 |
# File 'lib/swarm/support.rb', line 27 def symbolize_keys(hsh) symbolize_keys!(hsh.dup) end |
.symbolize_keys!(hsh) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/swarm/support.rb', line 20 def symbolize_keys!(hsh) hsh.keys.each do |key| hsh[key.to_sym] = hsh.delete(key) end hsh end |
.tokenize(string) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/swarm/support.rb', line 48 def tokenize(string) return nil if string.nil? string.to_s.gsub(/&/, ' and '). gsub(%r{[ /]+}, '_'). gsub(/([a-z\d])([A-Z])/, '\1_\2'). downcase end |
.uuid_with_timestamp ⇒ Object
44 45 46 |
# File 'lib/swarm/support.rb', line 44 def "#{Time.now.strftime("%Y%m%d-%H%M%S")}-#{SecureRandom.uuid}" end |
.wait_until(timeout: 5, initial_delay: nil) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/swarm/support.rb', line 78 def wait_until(timeout: 5, initial_delay: nil) sleep(initial_delay) if initial_delay.is_a?(Numeric) Timeout.timeout(timeout) do sleep 0.05 until yield end end |