Class: Bocuse::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/bocuse/value.rb

Overview

This is a value object that by default starts out life as not having decided what exactly it is.

On first adding another object, it assumes an internal form.

It will complain if its internal form does not correspond to the assumed external one, ie. when you want to << something, but it has already assumed the internal form of a hash (<< does not exist on a hash).

Defined Under Namespace

Modules: Empty

Instance Method Summary collapse

Constructor Details

#initialize(internal = Empty) ⇒ Value

Returns a new instance of Value.



16
17
18
# File 'lib/bocuse/value.rb', line 16

def initialize internal = Empty
  @internal = internal
end

Instance Method Details

#<<(element) ⇒ Object



29
30
31
32
# File 'lib/bocuse/value.rb', line 29

def << element
  @internal = [] if empty?
  @internal << element
end

#[]=(key, value) ⇒ Object



24
25
26
27
# File 'lib/bocuse/value.rb', line 24

def []= key, value
  @internal = {} if empty?
  @internal[key] = value
end

#dupObject



38
39
40
# File 'lib/bocuse/value.rb', line 38

def dup
  Value.new(@internal.dup)
end

#empty?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/bocuse/value.rb', line 20

def empty?
  @internal && @internal.empty?
end

#to_hObject



34
35
36
# File 'lib/bocuse/value.rb', line 34

def to_h
  @internal unless Empty == @internal
end