Class: OpenStruct

Inherits:
Object show all
Defined in:
lib/standard/facets/ostruct.rb,
lib/standard/facets/ostruct/each.rb,
lib/standard/facets/ostruct/to_h.rb,
lib/standard/facets/ostruct/merge.rb,
lib/standard/facets/ostruct/op_fetch.rb,
lib/standard/facets/ostruct/to_ostruct.rb,
lib/standard/facets/ostruct/initialize.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil, &block) ⇒ OpenStruct

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 cannot provide both at the same time.

CREDIT: Noah Gibbs, Gavin Sinclair


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/standard/facets/ostruct/initialize.rb', line 31

def initialize(hash=nil, &block)
  if block && block.arity==2
    @table = Hash.new(&block)
  else
    @table = {}
  end
  if hash
    for k,v in hash
      @table[k.to_sym] = v
      new_ostruct_member(k)
    end
  end
  if block && block.arity==1
    yield self
  end
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

12
13
14
15
# File 'lib/standard/facets/ostruct/op_fetch.rb', line 12

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)

23
24
25
26
27
# File 'lib/standard/facets/ostruct/op_fetch.rb', line 23

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
x = o.__merge__(:a => 2)
x.a  #=> 2

83
84
85
86
87
# File 'lib/standard/facets/ostruct.rb', line 83

def __merge__(other)
  o = dup
  o.merge!(other)
  o
end

#__update__(other) ⇒ Object

Deprecated.

Use #merge! instead.

Insert/update hash data on the fly.

o = OpenStruct.new
o.ostruct_update(:a => 2)
o.a  #=> 2

Raises:

  • (NameError)

57
58
59
60
# File 'lib/standard/facets/ostruct.rb', line 57

def __update__(other)
  raise NameError, "OpenSrtuct#__update__ has been deprecated. Use #merge! instead."
  #merge!(other)
end

#each(&blk) ⇒ Object

Iterate over key-value pairs.


7
8
9
# File 'lib/standard/facets/ostruct/each.rb', line 7

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

#instance_delegateObject Also known as: ostruct_delegate

Deprecated.

Use `#marshal_dump` instead.

Provides access to an OpenStruct's inner table.

o = OpenStruct.new
o.a = 1
o.b = 2
o.instance_delegate.map { |k, v| "#{k} #{v}" }
#=> ["a 1", "b 2"]

28
29
30
31
# File 'lib/standard/facets/ostruct.rb', line 28

def instance_delegate
  warn "OpenStruct#instance_delegate is deprecated, use #marshal_dump instead."
  @table
end

#merge!(other) ⇒ Object

Insert/update hash data on the fly.

o = OpenStruct.new
o.merge!(:a => 2)
o.a  #=> 2

Raises:

  • (TypeError)

11
12
13
14
15
16
17
18
# File 'lib/standard/facets/ostruct/merge.rb', line 11

def merge!(other)
  raise TypeError, "can't modify frozen #{self.class}", caller(1) if self.frozen?
  ##other = other.to_hash #to_h?
  for k,v in other
    @table[k.to_sym] = v
  end
  self
end

#ostruct_merge(other) ⇒ Object

Merge hash data creating a new OpenStruct object.

o = OpenStruct.new
x = o.ostruct_merge(:a => 2)
x.a  #=> 2

71
72
73
74
75
# File 'lib/standard/facets/ostruct.rb', line 71

def ostruct_merge(other)
  o = dup
  o.merge!(other)
  o
end

#ostruct_update(other) ⇒ Object

Deprecated.

Use #merge! instead.

Insert/update hash data on the fly.

o = OpenStruct.new
o.ostruct_update(:a => 2)
o.a  #=> 2

44
45
46
47
# File 'lib/standard/facets/ostruct.rb', line 44

def ostruct_update(other)
  warn "OpenSrtuct#ostruct_update has been deprecated. Use #merge! instead."
  merge!(other)
end

#to_hObject


6
7
8
# File 'lib/standard/facets/ostruct/to_h.rb', line 6

def to_h
  @table.dup
end

#to_ostructObject


5
6
7
# File 'lib/standard/facets/ostruct/to_ostruct.rb', line 5

def to_ostruct
  self
end