Class | OpenStruct |
In: |
lib/more/facets/ostruct.rb
|
Parent: | Object |
Allows the initialization of an OpenStruct with a setter block:
person = OpenStruct.new do |o| o.name = 'John Smith' o.gender = :M o.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
Alternatively you can provide a default block:
stuff = OpenStruct.new{ |o,k| o[k] = [] } stuff.place << :a stuff.place << :b stuff.place #=> [:a, :b]
A setter block versus a defualt block is determined by the arity of the block. You can not provide both at the same time.
CREDIT: Noah Gibbs, Gavin Sinclair
# File lib/more/facets/ostruct.rb, line 58 58: def initialize(hash=nil, &block) 59: if block && block.arity==2 60: @table = Hash.new(&block) 61: else 62: @table = {} 63: end 64: if hash 65: for k,v in hash 66: @table[k.to_sym] = v 67: new_ostruct_member(k) 68: end 69: end 70: if block && block.arity==1 71: yield self 72: end 73: end
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
# File lib/more/facets/ostruct.rb, line 92 92: def [](key) 93: key = key.to_sym unless key.is_a?(Symbol) 94: @table[key] 95: end
Set a value in the OpenStruct by key, like a Hash.
o = OpenStruct.new o['t'] = 4 o.t #=> 4
# File lib/more/facets/ostruct.rb, line 103 103: def []=(key,val) 104: raise TypeError, "can't modify frozen #{self.class}", caller(1) if self.frozen? 105: key = key.to_sym unless key.is_a?(Symbol) 106: @table[key]=val 107: end
Merge hash data creating a new OpenStruct object.
o = OpenStruct.new o.ostruct_merge { :a => 2 } o.a #=> 2
# File lib/more/facets/ostruct.rb, line 185 185: def __merge__(other) 186: o = dup 187: o.__update__(other) 188: o 189: end
Insert/update hash data on the fly.
o = OpenStruct.new o.ostruct_update { :a => 2 } o.a #=> 2
# File lib/more/facets/ostruct.rb, line 170 170: def __update__(other) 171: raise TypeError, "can't modify frozen #{self.class}", caller(1) if self.frozen? 172: #other = other.to_hash #to_h? 173: for k,v in other 174: @table[k.to_sym] = v 175: end 176: self 177: end
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
# File lib/more/facets/ostruct.rb, line 125 125: def instance_delegate 126: @table 127: end
Merge hash data creating a new OpenStruct object.
o = OpenStruct.new o.ostruct_merge { :a => 2 } o.a #=> 2
# File lib/more/facets/ostruct.rb, line 152 152: def ostruct_merge(other) 153: o = dup 154: o.ostruct_update(other) 155: o 156: end
Insert/update hash data on the fly.
o = OpenStruct.new o.ostruct_update { :a => 2 } o.a #=> 2
# File lib/more/facets/ostruct.rb, line 137 137: def ostruct_update(other) 138: raise TypeError, "can't modify frozen #{self.class}", caller(1) if self.frozen? 139: #other = other.to_hash #to_h ? 140: for k,v in other 141: @table[k.to_sym] = v 142: end 143: self 144: end