Class: OpenStruct

Inherits:
Object show all
Defined in:
lib/more/facets/ostruct.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil) {|_self| ... } ⇒ OpenStruct

Allows the initialization of an OpenStruct with a block:

person = OpenStruct.new do |p|
  p.name    = 'John Smith'
  p.gender  = :M
  p.age     = 71
end

You can still provide a hash for initialization purposes, and even combine the two approaches if you wish.

person = OpenStruct.new(:name => 'John Smith', :age => 31) do |p|
  p.gender = :M 
end

CREDIT Noah Gibbs
CREDIT Gavin Sinclair

Yields:

  • (_self)

Yield Parameters:

  • _self (OpenStruct)

    the object that the method was called on



54
55
56
57
58
59
60
61
62
63
# File 'lib/more/facets/ostruct.rb', line 54

def initialize(hash=nil) # :yield: self
  @table = {}
  if hash
    for k,v in hash
      @table[k.to_sym] = v
      new_ostruct_member(k)
    end
  end
  yield self if block_given?
end

Instance Method Details

#[](key) ⇒ Object

Access a value in the OpenStruct by key, like a Hash. This increases OpenStruct’s “duckiness”.

o = OpenStruct.new
o.t = 4
o['t']  #=> 4


82
83
84
85
# File 'lib/more/facets/ostruct.rb', line 82

def [](key)
  key = key.to_sym unless key.is_a?(Symbol)
  @table[key]
end

#[]=(key, val) ⇒ Object

Set a value in the OpenStruct by key, like a Hash.

o = OpenStruct.new
o['t'] = 4
o.t  #=> 4

Raises:

  • (TypeError)


93
94
95
96
97
# File 'lib/more/facets/ostruct.rb', line 93

def []=(key,val)
  raise TypeError, "can't modify frozen #{self.class}", caller(1) if self.frozen?
  key = key.to_sym unless key.is_a?(Symbol)
  @table[key]=val
end

#__merge__(other) ⇒ Object

Merge hash data creating a new OpenStruct object.

o = OpenStruct.new
o.ostruct_merge { :a => 2 }
o.a  #=> 2


172
173
174
175
176
# File 'lib/more/facets/ostruct.rb', line 172

def __merge__(other)
  o = dup
  o.__update__(other)
  o
end

#__update__(other) ⇒ Object

Insert/update hash data on the fly.

o = OpenStruct.new
o.ostruct_update { :a => 2 }
o.a  #=> 2


158
159
160
161
162
163
164
# File 'lib/more/facets/ostruct.rb', line 158

def __update__(other)
  #other = other.to_hash #to_h?
  for k,v in other
    @table[k.to_sym] = v
  end
  self
end

#each(&blk) ⇒ Object



66
67
68
# File 'lib/more/facets/ostruct.rb', line 66

def each(&blk)
  @table.each(&blk)
end

#instance_delegateObject Also known as: ostruct_delegate

Provides access to an OpenStruct’s inner table.

o = OpenStruct.new
o.a = 1
o.b = 2
o.instance_delegate.each { |k, v| puts "#{k} #{v}" }

produces

a 1
b 2


115
116
117
# File 'lib/more/facets/ostruct.rb', line 115

def instance_delegate
  @table
end

#ostruct_merge(other) ⇒ Object

Merge hash data creating a new OpenStruct object.

o = OpenStruct.new
o.ostruct_merge { :a => 2 }
o.a  #=> 2


140
141
142
143
144
# File 'lib/more/facets/ostruct.rb', line 140

def ostruct_merge(other)
  o = dup
  o.ostruct_update(other)
  o
end

#ostruct_update(other) ⇒ Object

Insert/update hash data on the fly.

o = OpenStruct.new
o.ostruct_update { :a => 2 }
o.a  #=> 2


126
127
128
129
130
131
132
# File 'lib/more/facets/ostruct.rb', line 126

def ostruct_update(other)
  #other = other.to_hash  #to_h ?
  for k,v in other
    @table[k.to_sym] = v
  end
  self
end

#to_hObject



71
72
73
# File 'lib/more/facets/ostruct.rb', line 71

def to_h
  @table
end