Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/everythingrb/core/hash.rb
Instance Method Summary collapse
-
#join_map(join_with = "") {|Object| ... } ⇒ String
Combines filter_map and join operations.
-
#to_istruct ⇒ Data
Converts hash to an immutable Data structure.
-
#to_ostruct ⇒ OpenStruct
Converts hash to an OpenStruct recursively.
-
#to_struct ⇒ Struct
Converts hash to a Struct recursively.
Instance Method Details
#join_map(join_with = "") {|Object| ... } ⇒ String
Combines filter_map and join operations
23 24 25 26 27 |
# File 'lib/everythingrb/core/hash.rb', line 23 def join_map(join_with = "", &block) block = ->(kv_pair) { kv_pair.compact } if block.nil? filter_map(&block).join(join_with) end |
#to_istruct ⇒ Data
Converts hash to an immutable Data structure
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/everythingrb/core/hash.rb', line 36 def to_istruct recurse = lambda do |input| case input when Hash input.to_istruct when Array input.map(&recurse) else input end end Data.define(*keys.map(&:to_sym)).new(*values.map { |value| recurse.call(value) }) end |
#to_ostruct ⇒ OpenStruct
Converts hash to an OpenStruct recursively
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/everythingrb/core/hash.rb', line 77 def to_ostruct recurse = lambda do |value| case value when Hash value.to_ostruct when Array value.map(&recurse) else value end end OpenStruct.new(**transform_values { |value| recurse.call(value) }) end |
#to_struct ⇒ Struct
Converts hash to a Struct recursively
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/everythingrb/core/hash.rb', line 57 def to_struct recurse = lambda do |value| case value when Hash value.to_struct when Array value.map(&recurse) else value end end Struct.new(*keys.map(&:to_sym)).new(*values.map { |value| recurse.call(value) }) end |