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
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/everythingrb/core/hash.rb', line 34 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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/everythingrb/core/hash.rb', line 74 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
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/everythingrb/core/hash.rb', line 54 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 |