Class: Ribbon::EventBus::Config

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

Defined Under Namespace

Classes: ConfigError, NotAddableError, ProcArray

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, &block) ⇒ Config

Returns a new instance of Config.



23
24
25
26
# File 'lib/ribbon/event_bus/config.rb', line 23

def initialize(name=nil, &block)
  @name = name
  define(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ribbon/event_bus/config.rb', line 53

def method_missing(meth, *args, &block)
  meth_str = meth.to_s

  if /^(\w+)\=$/.match(meth_str)
    _set($1, *args, &block)
  elsif args.length > 0 || block_given?
    _add(meth, *args, &block)
  elsif /^(\w+)\?$/.match(meth_str)
    !!_get($1)
  else
    object = _get(meth)
    object = _set(meth, Config.new((name ? "#{name}." : '') + meth_str)) unless object
    object
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/ribbon/event_bus/config.rb', line 21

def name
  @name
end

Instance Method Details

#define(&block) ⇒ Object



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

def define(&block)
  case block.arity
  when 0 then instance_eval(&block)
  when 1 then block.call(self)
  else   raise ConfigError, 'invalid config block arity'
  end
end

#dupObject

Deep dup all the values.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ribbon/event_bus/config.rb', line 37

def dup
  super.tap do |new_config|
    new_config.instance_variable_set(
      :@_nested,
      Hash[
        _nested.map { |key, object|
          [
            key,
            object.is_a?(Config) ? object.dup : object
          ]
        }
      ]
    )
  end
end

#merge_config_file!(file) ⇒ Object



97
98
99
# File 'lib/ribbon/event_bus/config.rb', line 97

def merge_config_file!(file)
  merge_hash!(YAML.load_file(file))
end

#merge_hash!(hash) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ribbon/event_bus/config.rb', line 85

def merge_hash!(hash)
  hash.each { |k, v|
    if v.is_a?(Hash)
      send(k).merge_hash!(v)
    else
      send("#{k}=", v)
    end
  }

  self
end

#nest_value(name, value) ⇒ Object



73
74
75
# File 'lib/ribbon/event_bus/config.rb', line 73

def nest_value(name, value)
  nested()
end

#nested(name) ⇒ Object



69
70
71
# File 'lib/ribbon/event_bus/config.rb', line 69

def nested(name)
  _nested[name.to_sym]
end

#nested_configs(namespace) ⇒ Object



81
82
83
# File 'lib/ribbon/event_bus/config.rb', line 81

def nested_configs(namespace)
  _nested_configs[namespace.to_sym]
end

#nested_values(key) ⇒ Object



77
78
79
# File 'lib/ribbon/event_bus/config.rb', line 77

def nested_values(key)
  _nested_values[key.to_sym]
end