Class: OpenStruct
- Inherits:
-
Object
- Object
- OpenStruct
- Defined in:
- lib/everythingrb/ostruct.rb
Overview
Extensions to Ruby’s OpenStruct class
Provides:
-
#map, #filter_map: Enumeration methods for OpenStruct entries
-
#join_map: Combine filter_map and join operations
-
#blank?, #present?: ActiveSupport integrations when available
-
#to_deep_h: Recursively convert to hash with all nested objects
Instance Method Summary collapse
-
#blank? ⇒ Boolean
Checks if the OpenStruct has no attributes.
-
#filter_map {|key, value| ... } ⇒ Array, Enumerator
Maps over OpenStruct entries and returns an array without nil values.
-
#join_map(join_with = "") {|key, value| ... } ⇒ String
Combines filter_map and join operations.
-
#map {|key, value| ... } ⇒ Array, Enumerator
Maps over OpenStruct entries and returns an array.
-
#present? ⇒ Boolean
Checks if the OpenStruct has any attributes.
-
#to_deep_h ⇒ Hash
Recursively converts the OpenStruct and all nested objects to hashes.
-
#to_ostruct ⇒ self
Returns self (identity method for consistent interfaces).
Instance Method Details
#blank? ⇒ Boolean
Checks if the OpenStruct has no attributes
26 27 28 |
# File 'lib/everythingrb/ostruct.rb', line 26 def blank? @table.blank? end |
#filter_map {|key, value| ... } ⇒ Array, Enumerator
Maps over OpenStruct entries and returns an array without nil values
72 73 74 75 76 |
# File 'lib/everythingrb/ostruct.rb', line 72 def filter_map(&block) return enum_for(:filter_map) unless block map(&block).compact end |
#join_map(join_with = "") {|key, value| ... } ⇒ String
Combines filter_map and join operations
99 100 101 102 103 |
# File 'lib/everythingrb/ostruct.rb', line 99 def join_map(join_with = "", &block) block = ->(kv_pair) { kv_pair.compact } if block.nil? filter_map(&block).join(join_with) end |
#map {|key, value| ... } ⇒ Array, Enumerator
Maps over OpenStruct entries and returns an array
55 56 57 |
# File 'lib/everythingrb/ostruct.rb', line 55 def map(&) @table.map(&) end |
#present? ⇒ Boolean
Checks if the OpenStruct has any attributes
35 36 37 |
# File 'lib/everythingrb/ostruct.rb', line 35 def present? @table.present? end |
#to_deep_h ⇒ Hash
Recursively converts the OpenStruct and all nested objects to hashes
126 127 128 |
# File 'lib/everythingrb/ostruct.rb', line 126 def to_deep_h to_h.to_deep_h end |
#to_ostruct ⇒ self
Returns self (identity method for consistent interfaces)
110 111 112 |
# File 'lib/everythingrb/ostruct.rb', line 110 def to_ostruct self end |