Class: OpenStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/everythingrb/core/ostruct.rb

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Checks if the OpenStruct has no attributes

Returns:

  • (Boolean)


10
11
12
# File 'lib/everythingrb/core/ostruct.rb', line 10

def blank?
  @table.blank?
end

#filter_map(&block) ⇒ Enumerator, Array

Maps over OpenStruct entries and returns an array without nil values

Returns:

  • (Enumerator, Array)

    Returns a new array, or enumerator if block is nil



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

def filter_map(&block)
  return enum_for(:filter_map) unless block

  map(&block).compact
end

#join_map(join_with = "") {|Object| ... } ⇒ 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"
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:

  • (Object)

    Block that filters and transforms hash elements

Returns:

  • (String)

    Joined string of filtered and transformed elements



65
66
67
68
69
# File 'lib/everythingrb/core/ostruct.rb', line 65

def join_map(join_with = "", &block)
  block = ->(kv_pair) { kv_pair.compact } if block.nil?

  filter_map(&block).join(join_with)
end

#mapEnumerator, Array

Maps over OpenStruct entries and returns an array

Returns:

  • (Enumerator, Array)

    Returns a new array, or enumerator if block is nil



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

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

#present?Boolean

Checks if the OpenStruct has any attributes

Returns:

  • (Boolean)


19
20
21
# File 'lib/everythingrb/core/ostruct.rb', line 19

def present?
  @table.present?
end

#to_ostructself

Returns:

  • (self)


74
75
76
# File 'lib/everythingrb/core/ostruct.rb', line 74

def to_ostruct
  self
end