Class: Usable::Config

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

Defined Under Namespace

Modules: Null

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



45
46
47
48
49
# File 'lib/usable/config.rb', line 45

def method_missing(method_name, *args)
  spec method_name, *args
rescue
  super
end

Instance Method Details

#[](key) ⇒ Object



37
38
39
# File 'lib/usable/config.rb', line 37

def [](key)
  spec key
end

#[]=(key, val) ⇒ Object



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

def []=(key, val)
  spec key, val
end

#add_module(mod) ⇒ Object



16
17
18
# File 'lib/usable/config.rb', line 16

def add_module(mod)
  modules << mod
end

#available_methodsObject

Class

Store and manage configuration settings. Keep methods to a minimum since this class relies on method_missing to read and write to the underlying @spec object



8
9
10
11
12
13
14
# File 'lib/usable/config.rb', line 8

def available_methods
  modules.each_with_object(Hash.new(Null.instance_method(:default_method))) do |mod, result|
    mod.instance_methods.each do |method_name|
      result[method_name] = mod.instance_method method_name
    end
  end
end

#each(&block) ⇒ Object



24
25
26
# File 'lib/usable/config.rb', line 24

def each(&block)
  @spec.to_h.each(&block)
end

#modulesObject



20
21
22
# File 'lib/usable/config.rb', line 20

def modules
  @modules ||= []
end

#respond_to_missing?(method_name, _private = false) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/usable/config.rb', line 51

def respond_to_missing?(method_name, _private = false)
  method_name.to_s.end_with?('=') || spec.respond_to?(method_name)
end

#spec(key, value = nil) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/usable/config.rb', line 28

def spec(key, value = nil)
  @spec ||= OpenStruct.new
  if value
    @spec[key.to_s.tr('=', '')] = value
  else
    @spec[key]
  end
end