Class: OpenStruct

Inherits:
Object
  • Object
show all
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

Examples:

require "everythingrb/ostruct"

person = OpenStruct.new(name: "Alice", age: 30)
person.map { |k, v| "#{k}: #{v}" }  # => ["name: Alice", "age: 30"]

Instance Method Summary collapse

Methods included from Everythingrb::InspectQuotable

#in_quotes

Instance Method Details

#blank?Boolean

Checks if the OpenStruct has no attributes

Returns:

  • (Boolean)

    true 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

Examples:

struct = OpenStruct.new(a: 1, b: nil, c: 2)
struct.filter_map { |key, value| value * 2 if value } # => [2, 4]

Yields:

  • (key, value)

    Block that transforms each key-value pair

Yield Parameters:

  • key (Symbol)

    The attribute name

  • value (Object)

    The attribute value

Returns:

  • (Array, Enumerator)

    Non-nil results of mapping or an Enumerator if no block given



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

Examples:

object = OpenStruct.new(a: 1, b: nil, c: 3)
object.join_map(" ") { |k, v| "#{k}-#{v}" if v }
# => "a-1 c-3"

Default behavior without block

object = OpenStruct.new(a: 1, b: nil, c: 3)
object.join_map(", ")
# => "a, 1, b, c, 3"

Parameters:

  • join_with (String) (defaults to: "")

    The delimiter to join elements with (defaults to empty string)

Yields:

  • (key, value)

    Block that filters and transforms OpenStruct entries

Yield Parameters:

  • key (Symbol)

    The attribute name

  • value (Object)

    The attribute value

Returns:

  • (String)

    Joined string of filtered and transformed entries



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

Examples:

struct = OpenStruct.new(a: 1, b: 2)
struct.map { |key, value| [key, value * 2] } # => [[:a, 2], [:b, 4]]

Yields:

  • (key, value)

    Block that transforms each key-value pair

Yield Parameters:

  • key (Symbol)

    The attribute name

  • value (Object)

    The attribute value

Returns:

  • (Array, Enumerator)

    Results of mapping or an Enumerator if no block given



58
59
60
# File 'lib/everythingrb/ostruct.rb', line 58

def map(&)
  @table.map(&)
end

#present?Boolean

Checks if the OpenStruct has any attributes

Returns:

  • (Boolean)

    true if the OpenStruct has attributes



38
39
40
# File 'lib/everythingrb/ostruct.rb', line 38

def present?
  @table.present?
end

#to_deep_hHash

Recursively converts the OpenStruct and all nested objects to hashes

Examples:

person = OpenStruct.new(
  name: "Alice",
  address: OpenStruct.new(city: "New York", country: "USA")
)
person.to_deep_h  # => {name: "Alice", address: {city: "New York", country: "USA"}}

Returns:

  • (Hash)

    A deeply converted hash of the OpenStruct



129
130
131
# File 'lib/everythingrb/ostruct.rb', line 129

def to_deep_h
  to_h.to_deep_h
end

#to_ostructself

Returns self (identity method for consistent interfaces)

Returns:

  • (self)

    Returns the OpenStruct



113
114
115
# File 'lib/everythingrb/ostruct.rb', line 113

def to_ostruct
  self
end