Class: HashGenerator
- Inherits:
-
Object
- Object
- HashGenerator
- Defined in:
- lib/hash_generator.rb
Constant Summary collapse
- VERSION =
'1.0.0'
Instance Method Summary collapse
- #end_scope ⇒ Object
-
#initialize ⇒ HashGenerator
constructor
A new instance of HashGenerator.
- #new_array ⇒ Object
- #new_object ⇒ Object
- #push_scope ⇒ Object
- #reopen_scope(key) ⇒ Object
- #store(key, value) ⇒ Object
- #store_scope(key) ⇒ Object
- #to_h ⇒ Object (also: #to_hash)
Constructor Details
#initialize ⇒ HashGenerator
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_scope ⇒ Object
9 10 11 |
# File 'lib/hash_generator.rb', line 9 def end_scope @scope = @stack.pop end |
#new_array ⇒ Object
13 14 15 |
# File 'lib/hash_generator.rb', line 13 def new_array begin_scope([]) end |
#new_object ⇒ Object
17 18 19 |
# File 'lib/hash_generator.rb', line 17 def new_object begin_scope({}) end |
#push_scope ⇒ Object
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_h ⇒ Object Also known as: to_hash
41 42 43 |
# File 'lib/hash_generator.rb', line 41 def to_h @value end |