Class: OpenStruct
- Inherits:
-
Object
- Object
- OpenStruct
- Includes:
- Everythingrb::InspectQuotable
- 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
-
#in_quotes, #with_quotes: Wrap struct in quotes
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).
Methods included from Everythingrb::InspectQuotable
Instance Method Details
#blank? ⇒ Boolean
Only available when ActiveSupport is loaded
Checks if the OpenStruct has no attributes
31 32 33 |
# File 'lib/everythingrb/ostruct.rb', line 31 def blank? @table.blank? end |
#filter_map {|key, value| ... } ⇒ Array, Enumerator
Maps over OpenStruct entries and returns an array without nil values
79 80 81 82 83 |
# File 'lib/everythingrb/ostruct.rb', line 79 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
106 107 108 109 110 |
# File 'lib/everythingrb/ostruct.rb', line 106 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
62 63 64 |
# File 'lib/everythingrb/ostruct.rb', line 62 def map(&) @table.map(&) end |
#present? ⇒ Boolean
Only available when ActiveSupport is loaded
Checks if the OpenStruct has any attributes
42 43 44 |
# File 'lib/everythingrb/ostruct.rb', line 42 def present? @table.present? end |
#to_deep_h ⇒ Hash
Recursively converts the OpenStruct and all nested objects to hashes
This method will convert the OpenStruct and all nested OpenStructs, Structs, Data objects, and other convertible objects to plain hashes.
136 137 138 |
# File 'lib/everythingrb/ostruct.rb', line 136 def to_deep_h to_h.to_deep_h end |
#to_ostruct ⇒ self
Returns self (identity method for consistent interfaces)
117 118 119 |
# File 'lib/everythingrb/ostruct.rb', line 117 def to_ostruct self end |