Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/safestruct.rb
Overview
add “beautiful” convenience helpers
Class Method Summary collapse
Class Method Details
.of(*args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/safestruct.rb', line 58 def self.of( *args ) ## e.g. gets passed in [{Address=>Integer}] ## check for Integer - use Hash.new(0) ## check for Bool - use Hash.new(False) if args[0].is_a?( Hash ) && args.size == 1 arg = args[0].to_a ## convert to array (for easier access) klass_key = arg[0][0] ## note: for nested Hash.of or Array.of a ("prototype") object ## gets passed in (NOT class) - auto-convert to use class klass_or_proto_value = arg[0][1] klass_value = klass_or_proto_value.is_a?( Class ) ? klass_or_proto_value : klass_or_proto_value.class klass = Safe::SafeHash.build_class( klass_key, klass_value ) klass.new else raise ArgumentError.new( "[Hash.of] wrong argument; expected (default) hash e.g. String => Integer" ) end end |