module StackerBee::Utilities
Constants
- REGEX
Public Instance Methods
camel_case(string, lower = false)
click to toggle source
TODO: avoid flag arguments
# File lib/stacker_bee/utilities.rb, line 16 def camel_case(string, lower = false) string.to_s.split(REGEX).each_with_object('') do |word, memo| memo << (memo.empty? && lower ? word[0].downcase : word[0].upcase) memo << word[1..-1] end end
hash_deeply(hash, &block)
click to toggle source
# File lib/stacker_bee/utilities.rb, line 23 def hash_deeply(hash, &block) block.call hash hash.values .select { |val| val.respond_to?(:to_hash) } .each { |val| hash_deeply val, &block } end
map_a_hash(hash) { |*pair| ... }
click to toggle source
# File lib/stacker_bee/utilities.rb, line 31 def map_a_hash(hash) hash.each_with_object({}) do |pair, new_hash| key, value = yield(*pair) new_hash[key] = value end end
snake_case(string)
click to toggle source
# File lib/stacker_bee/utilities.rb, line 11 def snake_case(string) string.to_s.gsub(/(.)([A-Z])/, '\1_\2').gsub(/(\W|_)+/, '_').downcase end
transform_hash_keys(hash) { |key| ... }
click to toggle source
# File lib/stacker_bee/utilities.rb, line 44 def transform_hash_keys(hash) map_a_hash(hash) do |key, val| [yield(key), val] end end
transform_hash_values(hash) { |val| ... }
click to toggle source
# File lib/stacker_bee/utilities.rb, line 38 def transform_hash_values(hash) map_a_hash(hash) do |key, val| [key, yield(val)] end end
uncase(string)
click to toggle source
# File lib/stacker_bee/utilities.rb, line 7 def uncase(string) string.to_s.downcase.gsub(REGEX, '') end