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

  • #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



30
31
32
# File 'lib/everythingrb/ostruct.rb', line 30

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



78
79
80
81
82
# File 'lib/everythingrb/ostruct.rb', line 78

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



105
106
107
108
109
# File 'lib/everythingrb/ostruct.rb', line 105

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



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

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



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

def present?
  @table.present?
end

#to_ostructself

Returns self (identity method for consistent interfaces)

Returns:

  • (self)

    Returns the OpenStruct



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

def to_ostruct
  self
end