Class: Celluloid::Supervision::Configuration::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/celluloid/supervision/configuration/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = {}) ⇒ Instance

Returns a new instance of Instance.


7
8
9
10
11
12
# File 'lib/celluloid/supervision/configuration/instance.rb', line 7

def initialize(configuration = {})
  @state = :initializing # :ready
  resync_accessors
  @configuration = configuration
  define(configuration) if configuration.any?
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration


5
6
7
# File 'lib/celluloid/supervision/configuration/instance.rb', line 5

def configuration
  @configuration
end

Instance Method Details

#define(instance, fail = false) ⇒ Object


25
26
27
28
29
30
# File 'lib/celluloid/supervision/configuration/instance.rb', line 25

def define(instance, fail = false)
  raise Configuration::Error::AlreadyDefined if ready? fail
  invoke_injection(:before_configuration)
  @configuration = Configuration.options(instance)
  ready?
end

#delete(k) ⇒ Object


101
102
103
# File 'lib/celluloid/supervision/configuration/instance.rb', line 101

def delete(k)
  @configuration.delete(k)
end

#exportObject


14
15
16
# File 'lib/celluloid/supervision/configuration/instance.rb', line 14

def export
  @configuration.reject { |k, _v| REMOVE_AT_EXPORT.include? k }
end

#get(key) ⇒ Object Also known as: []


96
97
98
# File 'lib/celluloid/supervision/configuration/instance.rb', line 96

def get(key)
  @configuration[key]
end

#injection!(key, proc) ⇒ Object


32
33
34
35
# File 'lib/celluloid/supervision/configuration/instance.rb', line 32

def injection!(key, proc)
  @configuration[:injections] ||= {}
  @configuration[:injections][key] = proc
end

#injections!(_procs) ⇒ Object


37
38
39
# File 'lib/celluloid/supervision/configuration/instance.rb', line 37

def injections!(_procs)
  @configuration[:injections] = proces
end

#key?(k) ⇒ Boolean

Returns:

  • (Boolean)

87
88
89
# File 'lib/celluloid/supervision/configuration/instance.rb', line 87

def key?(k)
  @configuration.key?(k)
end

#merge(values) ⇒ Object


77
78
79
80
81
82
83
84
85
# File 'lib/celluloid/supervision/configuration/instance.rb', line 77

def merge(values)
  if values.is_a? Configuration
    @configuration.merge(values.configuration)
  elsif values.is_a? Hash
    @configuration.merge(values)
  else
    raise Error::Invalid
  end
end

#merge!(values) ⇒ Object


73
74
75
# File 'lib/celluloid/supervision/configuration/instance.rb', line 73

def merge!(values)
  @configuration = @configuration.merge(values)
end

#ready?(fail = false) ⇒ Boolean

Returns:

  • (Boolean)

18
19
20
21
22
23
# File 'lib/celluloid/supervision/configuration/instance.rb', line 18

def ready?(fail = false)
  unless @state == :ready
    @state = :ready if Configuration.valid? @configuration, fail
  end
  @state == :ready
end

#resync_accessorsObject


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/celluloid/supervision/configuration/instance.rb', line 41

def resync_accessors
  # methods for setting and getting the usual defaults
  Configuration.parameters(:mandatory, :optional, :plugins, :meta).each do |key|
    self.class.instance_eval do
      remove_method :"#{key}!" rescue nil # avoid warnings in tests
      define_method(:"#{key}!") { |value| @configuration[key] = value }
    end
    self.class.instance_eval do
      remove_method :"#{key}=" rescue nil # avoid warnings in tests
      define_method(:"#{key}=") { |value| @configuration[key] = value }
    end
    self.class.instance_eval do
      remove_method :"#{key}?" rescue nil # avoid warnings in tests
      define_method(:"#{key}?") { !@configuration[key].nil? }
    end
    self.class.instance_eval do
      remove_method :"#{key}" rescue nil # avoid warnings in tests
      define_method(:"#{key}") { @configuration[key] }
    end
  end

  Configuration.aliases.each do |_alias, _original|
    ["!", :"=", :"?", :""]. each do |m|
      self.class.instance_eval do
        remove_method :"#{_alias}#{m}" rescue nil # avoid warnings in tests
        alias_method :"#{_alias}#{m}", :"#{_original}#{m}"
      end
    end
  end
  true
end

#set(key, value) ⇒ Object Also known as: []=


91
92
93
# File 'lib/celluloid/supervision/configuration/instance.rb', line 91

def set(key, value)
  @configuration[key] = value
end