Module: Kanrisuru::OsPackage::Collection

Included in:
Remote::Cluster
Defined in:
lib/kanrisuru/os_package/collection.rb

Instance Method Summary collapse

Instance Method Details

#os_collection(mod, opts = {}) ⇒ Object



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
# File 'lib/kanrisuru/os_package/collection.rb', line 6

def os_collection(mod, opts = {})
  os_method_properties = mod.instance_variable_get(:@os_method_properties)
  os_method_names = os_method_properties.keys

  namespace          = opts[:namespace]
  namespace_instance = nil

  if namespace
    ## Define the namespace as an eigen class instance on the host.
    ## Namespaced instances will access core host methods
    ## with @host instance variable.

    if Kanrisuru::Remote::Cluster.instance_variable_defined?("@#{namespace}")
      namespace_class = Kanrisuru::Remote::Cluster.const_get(Kanrisuru::Util.camelize(namespace))
      namespace_instance = instance_variable_get("@#{namespace}")
    else
      namespace_class    = Kanrisuru::Remote::Cluster.const_set(Kanrisuru::Util.camelize(namespace), Class.new)
      namespace_instance = Kanrisuru::Remote::Cluster.instance_variable_set("@#{namespace}", namespace_class.new)

      class_eval do
        define_method namespace do
          namespace_instance.instance_variable_set(:@cluster, self)
          namespace_instance
        end
      end
    end

    namespace_class.class_eval do
      os_method_names.each do |method_name|
        define_method method_name do |*args, &block|
          cluster = namespace_instance.instance_variable_get(:@cluster)
          cluster.map do |host|
            host.send(namespace).send(method_name, *args, &block)
          end
        end
      end
    end
  else
    class_eval do
      os_method_names.each do |method_name|
        define_method method_name do |*args, &block|
          map do |host|
            host.send(method_name, *args, &block)
          end
        end
      end
    end
  end
end