File: configuration.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 (169 lines) | stat: -rw-r--r-- 4,649 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
module Celluloid
  module Supervision
    class Configuration
      class << self
        def deploy(options = {})
          define(options).deploy
        end

        def define(options = {})
          new(options)
        end
      end

      extend Forwardable

      def_delegators :current_instance,
                     :delete,
                     :key?,
                     :set,
                     :get,
                     :[],
                     :[]=,
                     :injection!,
                     :injections!

      attr_accessor :instances

      def initialize(options = {})
        @instances = [Instance.new]
        @branch = :services
        @i = 0 # incrementer of instances in this branch
        resync_accessors
        @configuration = options

        if options.is_a? Hash
          options[:configuration] ||= Container::Behavior.configure(options)
          @configuration = instance_eval(&options[:configuration])
          @supervisor ||= @configuration.fetch(:supervisor, :"Celluloid.services")
        end
        @supervisor ||= :"Celluloid.services"

        define(@configuration) if (@configuration.is_a?(Hash) || @configuration.is_a?(Array)) && @configuration.any?
      end

      def provider
        @provider ||= if @supervisor.is_a? Hash
                        @supervisor[:type].run!(@supervisor)
                      elsif @supervisor.is_a? Symbol
                        @supervisor = Object.module_eval(@supervisor.to_s)
                        provider
                      elsif @supervisor.is_a? Class
                        @supervisor.run!
                      elsif @supervisor.respond_to? :supervise
                        @supervisor
                      else
                        raise Error::InvalidSupervisor
                      end
      end

      def deploy(options = {})
        define(options) if options.any?
        @instances.each do |instance|
          provider.add instance.merge(branch: @branch)
        end
        provider
      end

      def count
        @instances.count
      end

      def each(&block)
        @instances.each(&block)
      end

      def resync_accessors
        # methods for setting and getting the usual defaults
        Configuration.parameters(:mandatory, :optional, :plugins, :meta).each do |key|
          [:"#{key}!", :"#{key}="].each do |m|
            self.class.instance_eval do
              remove_method :"#{m}" rescue nil # avoid warnings in tests
              define_method(m) { |p| current_instance.send(m, p) }
            end
          end
          [:"#{key}?", :"#{key}"].each do |m|
            self.class.instance_eval do
              remove_method :"#{m}" rescue nil # avoid warnings in tests
              define_method(m) { current_instance.send(m) }
            end
          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
      end

      def merge!(values)
        if values.is_a?(Configuration) || values.is_a?(Hash)
          current_instance.merge!(values)
        else
          raise Error::Invalid
        end
      end

      def merge(values)
        if values.is_a?(Configuration) || values.is_a?(Hash)
          current_instance.merge(values)
        else
          raise Error::Invalid
        end
      end

      def export
        return current_instance.to_hash if @i == 0
        @instances.map(&:export)
      end

      def include?(name)
        @instances.map(&:name).include? name
      end

      def define(configuration, fail = false)
        if configuration.is_a? Array
          configuration.each { |c| define(c, fail) }
        else
          unless include? configuration[:as]
            begin
              current_instance.define(configuration, fail)
            rescue Error::AlreadyDefined
              increment
              retry
            end
          end
        end
        self
      end

      def increment
        @i += 1
      end
      alias another increment

      def add(options)
        define(options)
        provider.supervise options if Configuration.valid? options
      end

      def shutdown
        @provider.shutdown
      end

      private

      def current_instance
        @instances[@i] ||= Instance.new
      end

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