Sha256: a3ab4bd5967526f0c146273f70b0bda2536ce9b5e57b2fd9836eb062a8f49616

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

require 'ns-options'
require 'pathname'
require 'set'

require 'sanford/version'
require 'sanford/host'
require 'sanford/logger'
require 'sanford/runner'
require 'sanford/server'
require 'sanford/service_handler'

ENV['SANFORD_SERVICES_FILE'] ||= 'config/services'

module Sanford

  def self.register(host)
    @hosts.add(host)
  end

  def self.hosts
    @hosts
  end

  def self.config
    Sanford::Config
  end

  def self.configure(&block)
    self.config.define(&block)
    self.config
  end

  def self.init
    @hosts ||= Hosts.new
    require self.config.services_file
  end

  module Config
    include NsOptions::Proxy
    option :services_file,  Pathname, :default => ENV['SANFORD_SERVICES_FILE']
    option :logger,                   :default => Sanford::NullLogger.new
    option :runner,                   :default => Sanford::DefaultRunner
  end

  class Hosts

    def initialize(values = [])
      @set = Set.new(values)
    end

    # We want class names to take precedence over a configured name, so that if
    # a user specifies a specific class, they always get it
    def find(name)
      self.find_by_class_name(name) || self.find_by_name(name)
    end

    def find_by_class_name(class_name)
      @set.detect{|host_class| host_class.to_s == class_name.to_s }
    end

    def find_by_name(name)
      @set.detect{|host_class| host_class.name == name.to_s }
    end

    def method_missing(method, *args, &block)
      @set.send(method, *args, &block)
    end

    def respond_to?(method)
      super || @set.respond_to?(method)
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sanford-0.8.0 lib/sanford.rb
sanford-0.7.0 lib/sanford.rb
sanford-0.6.5 lib/sanford.rb
sanford-0.6.4 lib/sanford.rb