Sha256: 2a4941c4f51fd9ab28522418fd2fa6a6f0c7d71c82dd713931cb8974b1a9b508

Contents?: true

Size: 1.16 KB

Versions: 7

Compression:

Stored size: 1.16 KB

Contents

module Ixtlan
  module Core
    module ConfigurationManager
      
      def self.included(model)
        model.send :include, Slf4r::Logger
        model.after_save :fire_on_change
        raise "configuration class must have instance method" unless model.respond_to? :instance
        model.class_eval do
          class << self
            alias :instance_old :instance
            def instance
              Thread.current[:ixtlan_configuration] ||= instance_old
            end
            def clear_instance
              Thread.current[:ixtlan_configuration] = nil
            end
          end

          private
          def self.registry
            @registry ||= {}
          end
        end
      end
      
      def register(name, &block)
        raise "need block" unless block
        logger.info{"register config for: #{name}"}
        registry[name.to_sym] = block
      end
      
      def fire_on_change
        registry.each do |name, callback|
          logger.debug{ "configure #{name}" }
          callback.call(self)
        end
      end              
      
      private
      
      def registry
        self.class.registry
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ixtlan-core-0.6.1 lib/ixtlan/core/configuration_manager.rb
ixtlan-core-0.6.0 lib/ixtlan/core/configuration_manager.rb
ixtlan-core-0.5.0 lib/ixtlan/core/configuration_manager.rb
ixtlan-core-0.4.1 lib/ixtlan/core/configuration_manager.rb
ixtlan-core-0.4.0 lib/ixtlan/core/configuration_manager.rb
ixtlan-core-0.2.0 lib/ixtlan/core/configuration_manager.rb
ixtlan-core-0.1.1 lib/ixtlan/core/configuration_manager.rb