Class: Praxis::ConfigHash

Inherits:
BasicObject
Defined in:
lib/praxis-blueprints/config_hash.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, &block) ⇒ ConfigHash

Returns a new instance of ConfigHash.



10
11
12
13
# File 'lib/praxis-blueprints/config_hash.rb', line 10

def initialize(hash = {}, &block)
  @hash = hash
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, value, *rest, &block) ⇒ Object

rubocop:disable Style/MethodMissing



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/praxis-blueprints/config_hash.rb', line 24

def method_missing(name, value, *rest, &block) # rubocop:disable Style/MethodMissing
  if (existing = @hash[name])
    if block
      existing << [value, block]
    else
      existing << value
      rest.each do |v|
        existing << v
      end
    end
  else
    @hash[name] = if rest.any?
                    [value] + rest
                  else
                    value
                  end
  end
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



4
5
6
# File 'lib/praxis-blueprints/config_hash.rb', line 4

def hash
  @hash
end

Class Method Details

.from(hash = {}, &block) ⇒ Object



6
7
8
# File 'lib/praxis-blueprints/config_hash.rb', line 6

def self.from(hash = {}, &block)
  new(hash, &block)
end

Instance Method Details

#respond_to_missing?(_method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/praxis-blueprints/config_hash.rb', line 20

def respond_to_missing?(_method_name, _include_private = false)
  true
end

#to_hashObject



15
16
17
18
# File 'lib/praxis-blueprints/config_hash.rb', line 15

def to_hash
  instance_eval(&@block)
  @hash
end