Class: HashDsl
- Inherits:
-
Object
- Object
- HashDsl
- Defined in:
- lib/hash_dsl.rb
Instance Attribute Summary collapse
-
#hash ⇒ Object
(also: #to_hash, #to_h)
readonly
Returns the value of attribute hash.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ HashDsl
constructor
A new instance of HashDsl.
- #method_missing(method_name, *args, &block) ⇒ Object
Constructor Details
#initialize ⇒ HashDsl
Returns a new instance of HashDsl.
6 7 8 |
# File 'lib/hash_dsl.rb', line 6 def initialize @hash = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/hash_dsl.rb', line 18 def method_missing(method_name, *args, &block) if block_given? @hash[method_name] = HashDsl.hash_from_block(block) elsif args.length == 1 @hash[method_name] = args.first else super end end |
Instance Attribute Details
#hash ⇒ Object (readonly) Also known as: to_hash, to_h
Returns the value of attribute hash.
2 3 4 |
# File 'lib/hash_dsl.rb', line 2 def hash @hash end |
Class Method Details
.from_block(block) ⇒ Object
10 11 12 |
# File 'lib/hash_dsl.rb', line 10 def self.from_block(block) HashDsl.new.tap { |dsl| dsl.instance_eval(&block) } end |
.hash_from_block(block) ⇒ Object
14 15 16 |
# File 'lib/hash_dsl.rb', line 14 def self.hash_from_block(block) from_block(block).to_hash end |