module Yaks::Util

Public Instance Methods

Resolve(maybe_proc, context = nil) click to toggle source

Turn what is maybe a Proc into its result (or itself)

When input can be either a value or a proc that returns a value, this conversion function can be used to resolve the thing to a value.

The proc can be evaluated (instance_evaled) in a certain context, or evaluated as a closure.

@param [Object|Proc] maybe_proc

A proc or a plain value

@param [Object] context

(optional) A context used to instance_eval the proc
# File lib/yaks/util.rb, line 49
def Resolve(maybe_proc, context = nil) # rubocop:disable Style/MethodName
  if maybe_proc.respond_to?(:to_proc) && !maybe_proc.instance_of?(Symbol)
    if context
      if maybe_proc.arity > 0
        context.instance_eval(&maybe_proc)
      else
        # In case it's a lambda with zero arity instance_eval fails
        context.instance_exec(&maybe_proc)
      end
    else
      maybe_proc.to_proc.call()
    end
  else
    maybe_proc
  end
end
camelize(str) click to toggle source
# File lib/yaks/util.rb, line 15
def camelize(str)
  str.gsub(%r{/(.?)})    { "::#{ $1.upcase }" }
    .gsub!(/(?:^|_)(.)/) { $1.upcase          }
end
extract_options(args) click to toggle source
# File lib/yaks/util.rb, line 32
def extract_options(args)
  args.last.instance_of?(Hash) ? [args[0..-2], args.last] : [args, {}]
end
reject_keys(hash, *keys) click to toggle source
# File lib/yaks/util.rb, line 24
def reject_keys(hash, *keys)
  hash.keys.each_with_object({}) {|k, dest| dest[k] = hash[k] unless keys.include?(k) }
end
slice_hash(hash, *keys) click to toggle source
# File lib/yaks/util.rb, line 20
def slice_hash(hash, *keys)
  keys.each_with_object({}) {|k, dest| dest[k] = hash[k] if hash.key?(k) }
end
symbolize_keys(hash) click to toggle source
# File lib/yaks/util.rb, line 28
def symbolize_keys(hash)
  hash.each_with_object({}) {|(k, v), hsh| hsh[k.to_sym] = v}
end
underscore(str) click to toggle source
# File lib/yaks/util.rb, line 8
def underscore(str)
  str.gsub(/::/, '/')
    .gsub(%r{(?<!^|/)([A-Z])(?=[a-z$])|(?<=[a-z])([A-Z])}, '_\1\2')
    .tr("-", "_")
    .downcase
end