Sha256: 7bd84cd92346ee8eacb52cc95a3ad436ad5d5d33413f583e01db59bcfe227a81

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require_relative 'observers/null_observer'

module StateInspector
  # OBSERVABLE PATTERN
  # HASH-LIKE KEY OF OJECT TO OBSERVER/LOGGER INSTANCE
  module Reporter
    class << self
      def [](key)
        reporters[key]
      end

      def []=(key, value)
        reporters[key] = value
      end

      def has_key? key
        reporters.has_key?(key)
      end

      def has_observer? key
        reporters.has_key?(key) || (reporters.has_key?(key.class) if key.respond_to?(:class_exec))
      end

      def get key
        unless key.respond_to?(:class_exec)
          unless reporters.has_key?(key)
            return self[key.class] 
          end
        end
        self[key] 
      end

      def drop key
        reporters.delete key
      end

      def default observer=nil
        @default = observer if observer
        reporters.default = @default 
        @default
      end

      private
      def reporters
        @reporters ||= Hash.new.tap {|h|
          h.default = @default || Observers::NullObserver
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
state_inspector-1.0.5 lib/state_inspector/reporter.rb