Module: FqdnFacts

Defined in:
lib/fqdn_facts.rb,
lib/fqdn_facts/errors.rb,
lib/fqdn_facts/handler.rb,
lib/fqdn_facts/version.rb

Overview

Copyright © 2015 Carl P. Corliss <[email protected]>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Defined Under Namespace

Classes: Error, Handler

Constant Summary collapse

MAJOR =
0
MINOR =
3
PATCH =
0
VERSION =
"%d.%d.%d" % [ MAJOR, MINOR, PATCH ]
GEM_VERSION =
Gem::Version.new(VERSION)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.registryObject (readonly)

Returns the value of attribute registry.



33
34
35
# File 'lib/fqdn_facts.rb', line 33

def registry
  @registry
end

Class Method Details

.handler(fqdn) ⇒ Handler

Retrieves a handler that is capable of generating facts for the given FQDN

Parameters:

  • fqdn (String)

    Fully qualified domain name to resolve into a handler

Returns:

Raises:



78
79
80
81
82
83
# File 'lib/fqdn_facts.rb', line 78

def handler(fqdn)
  if (handlers = @registry.values.select { |klass| klass.match?(fqdn) }).empty?
    fail Error::UnresolveableHandler, "unable to find a handler for FQDN:#{fqdn}"
  end
  handlers.sort.first.tap { |handler| handler.fqdn = fqdn }
end

.register(klass, options = {}, &block) ⇒ Handler

Registers a FQDN Fact Handler

Parameters:

  • klass (Symbol)

    name of handler to register

  • options (Hash) (defaults to: {})

    options to pass

  • block (Block)

    block of DSL code

Options Hash (options):

  • :copy (Symbol)

    existing handler to copy definition from

Returns:

Raises:



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
# File 'lib/fqdn_facts.rb', line 44

def register(klass, options = {}, &block)
  parent_name  = (v = options.delete(:copy)).nil? ? :handler : v.to_sym
  parent_class = parent_name == :handler ? Handler : parent_name.constantize(Handler)

  klass_const = klass.to_s.camelize
  unless Handler.const_defined? klass_const
    Handler.const_set(klass_const, Class.new(parent_class))
  end

  if parent_name != :handler
    unless parent = @registry[parent_name]
      fail Error::HandlerNotFound, other
    end
    @registry[klass.to_sym] = Handler.const_get(klass_const).copy_from(parent)
  else
    @registry[klass.to_sym] = Handler.const_get(klass_const).new
  end

  if block.arity == 1
    yield @registry[klass.to_sym]
  else
    @registry[klass.to_sym].instance_eval(&block)
  end

  @registry[klass.to_sym]
end