File: instance.rb

package info (click to toggle)
ruby-celluloid 0.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 848 kB
  • sloc: ruby: 7,579; makefile: 10
file content (113 lines) | stat: -rw-r--r-- 3,341 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
module Celluloid
  module Supervision
    class Configuration
      class Instance
        attr_accessor :configuration

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

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

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

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

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

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

        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

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

        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

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

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

        def get(key)
          @configuration[key]
        end
        alias [] get

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

        private

        def invoke_injection(_point)
          # de puts "injection? #{point}"
        end
      end
    end
  end
end