Class: OmfRc::ResourceFactory

Inherits:
Object
  • Object
show all
Includes:
ResourceProxyDSL
Defined in:
omf_rc/lib/omf_rc/resource_factory.rb

Overview

Factory class for managing available proxies and creating new resource proxy instances

Constant Summary

@@proxy_list =

List of registered resource proxies

Hashie::Mash.new

Constants included from ResourceProxyDSL

OmfRc::ResourceProxyDSL::DEFAULT_PROP_ACCESS, OmfRc::ResourceProxyDSL::PROXY_DIR, OmfRc::ResourceProxyDSL::UTIL_DIR

Class Method Summary (collapse)

Methods included from ResourceProxyDSL

#call_hook, included

Class Method Details

+ (Object) create(type, opts = {}, creation_opts = {}, &creation_callback) Also known as: new

Factory method to initiate new resource proxy



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'omf_rc/lib/omf_rc/resource_factory.rb', line 25

def create(type, opts = {}, creation_opts = {}, &creation_callback)
  unless @@proxy_list.include?(type)
    raise ArgumentError, "Resource type not found: #{type.to_s}" unless @@proxy_list.include?(type)
  end
  # Get relevant module identified by type
  emodule = @@proxy_list[type].proxy_module || "OmfRc::ResourceProxy::#{type.camelize}".constantize
  # Create a new instance of abstract resource
  resource = OmfRc::ResourceProxy::AbstractResource.new(type, opts, creation_opts, &creation_callback)
  # Extend newly created resource with proxy module
  resource.extend(emodule)

  # Initiate property hash
  resource.methods.each do |m|
    resource.__send__(m) if m =~ /default_property_(.+)/
  end
  # Execute resource before_ready hook if any
  call_hook(:before_ready, resource)

  resource
end

+ (Object) load_additional_resource_proxies(folder)

Require files from a folder contains resource proxy definition files



72
73
74
75
76
# File 'omf_rc/lib/omf_rc/resource_factory.rb', line 72

def load_additional_resource_proxies(folder)
  Dir["#{folder}/*.rb"].each do |file|
    require "#{folder}/#{File.basename(file).gsub(/\.rb/, '')}"
  end
end

+ (Object) load_default_resource_proxies

Require files from default resource proxy library folder



63
64
65
66
67
# File 'omf_rc/lib/omf_rc/resource_factory.rb', line 63

def load_default_resource_proxies
  Dir["#{File.dirname(__FILE__)}/resource_proxy/*.rb"].each do |file|
    require "omf_rc/resource_proxy/#{File.basename(file).gsub(/\.rb/, '')}"
  end
end

+ (Object) proxy_list

Return the proxy list



49
50
51
# File 'omf_rc/lib/omf_rc/resource_factory.rb', line 49

def proxy_list
  @@proxy_list
end

+ (Object) register_proxy(proxy_opts)

Add a proxy to the list



54
55
56
57
58
59
60
# File 'omf_rc/lib/omf_rc/resource_factory.rb', line 54

def register_proxy(proxy_opts)
  if @@proxy_list.has_key? proxy_opts[:name]
    raise StandardError, "Resource has been registered already"
  else
    @@proxy_list.update(proxy_opts)
  end
end