Class: HashDsl

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHashDsl

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

#hashObject (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