Module: Yaks::Util
- Extended by:
- Forwardable, Util
- Included in:
- DefaultPolicy, Format, Mapper, Mapper::Association, Mapper::Attribute, Mapper::Attribute, Mapper::Form, Mapper::HasMany, Mapper::Link, Mapper::Link, Reader::Hal, Runner, Util
- Defined in:
- lib/yaks/util.rb
Defined Under Namespace
Modules: Deprecated
Instance Method Summary collapse
- #camelize(str) ⇒ Object
- #extract_options(args) ⇒ Object
- #reject_keys(hash, *keys) ⇒ Object
-
#Resolve(maybe_proc, context = nil) ⇒ Object
Turn what is maybe a Proc into its result (or itself).
- #slice_hash(hash, *keys) ⇒ Object
- #symbolize_keys(hash) ⇒ Object
- #underscore(str) ⇒ Object
Instance Method Details
#camelize(str) ⇒ Object
15 16 17 18 |
# File 'lib/yaks/util.rb', line 15 def camelize(str) str.gsub(%r{/(.?)}) { "::#{ $1.upcase }" } .gsub!(/(?:^|_)(.)/) { $1.upcase } end |
#extract_options(args) ⇒ Object
32 33 34 |
# File 'lib/yaks/util.rb', line 32 def (args) args.last.instance_of?(Hash) ? [args[0..-2], args.last] : [args, {}] end |
#reject_keys(hash, *keys) ⇒ Object
24 25 26 |
# 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 |
#Resolve(maybe_proc, context = nil) ⇒ Object
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.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# 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 |
#slice_hash(hash, *keys) ⇒ Object
20 21 22 |
# 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) ⇒ Object
28 29 30 |
# 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) ⇒ Object
8 9 10 11 12 13 |
# 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 |