Class: HashGenerator

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

Constant Summary collapse

VERSION =
'1.0.0'

Instance Method Summary collapse

Constructor Details

#initializeHashGenerator

Returns a new instance of HashGenerator.



4
5
6
7
# File 'lib/hash_generator.rb', line 4

def initialize
  @value = @scope = {}
  @stack = []
end

Instance Method Details

#end_scopeObject



9
10
11
# File 'lib/hash_generator.rb', line 9

def end_scope
  @scope = @stack.pop
end

#new_arrayObject



13
14
15
# File 'lib/hash_generator.rb', line 13

def new_array
  begin_scope([])
end

#new_objectObject



17
18
19
# File 'lib/hash_generator.rb', line 17

def new_object
  begin_scope({})
end

#push_scopeObject



21
22
23
24
25
# File 'lib/hash_generator.rb', line 21

def push_scope
  value = @scope
  end_scope
  push(value)
end

#reopen_scope(key) ⇒ Object



27
28
29
# File 'lib/hash_generator.rb', line 27

def reopen_scope(key)
  begin_scope(@scope.fetch(key))
end

#store(key, value) ⇒ Object



31
32
33
# File 'lib/hash_generator.rb', line 31

def store(key, value)
  @scope.store(key, value)
end

#store_scope(key) ⇒ Object



35
36
37
38
39
# File 'lib/hash_generator.rb', line 35

def store_scope(key)
  value = @scope
  end_scope
  store(key, value)
end

#to_hObject Also known as: to_hash



41
42
43
# File 'lib/hash_generator.rb', line 41

def to_h
  @value
end