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
Checks if the OpenStruct has no attributes
29 30 31 |
# File 'lib/everythingrb/ostruct.rb', line 29 def blank? @table.blank? end |
#filter_map {|key, value| ... } ⇒ Array, Enumerator
Maps over OpenStruct entries and returns an array without nil values
75 76 77 78 79 |
# File 'lib/everythingrb/ostruct.rb', line 75 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
102 103 104 105 106 |
# File 'lib/everythingrb/ostruct.rb', line 102 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
58 59 60 |
# File 'lib/everythingrb/ostruct.rb', line 58 def map(&) @table.map(&) end |
#present? ⇒ Boolean
Checks if the OpenStruct has any attributes
38 39 40 |
# File 'lib/everythingrb/ostruct.rb', line 38 def present? @table.present? end |
#to_deep_h ⇒ Hash
Recursively converts the OpenStruct and all nested objects to hashes
129 130 131 |
# File 'lib/everythingrb/ostruct.rb', line 129 def to_deep_h to_h.to_deep_h end |
#to_ostruct ⇒ self
Returns self (identity method for consistent interfaces)
113 114 115 |
# File 'lib/everythingrb/ostruct.rb', line 113 def to_ostruct self end |