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

Note:

Only available when ActiveSupport is loaded

Checks if the OpenStruct has no attributes

Returns:

  • (Boolean)

    true 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

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



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

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



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

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



62
63
64
# File 'lib/everythingrb/ostruct.rb', line 62

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

#present?Boolean

Note:

Only available when ActiveSupport is loaded

Checks if the OpenStruct has any attributes

Returns:

  • (Boolean)

    true if the OpenStruct has any attributes



42
43
44
# File 'lib/everythingrb/ostruct.rb', line 42

def present?
  @table.present?
end

#to_deep_hHash

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.

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



136
137
138
# File 'lib/everythingrb/ostruct.rb', line 136

def to_deep_h
  to_h.to_deep_h
end

#to_ostructself

Returns self (identity method for consistent interfaces)

Returns:

  • (self)

    Returns the OpenStruct



117
118
119
# File 'lib/everythingrb/ostruct.rb', line 117

def to_ostruct
  self
end