Sha256: 818f52c509b9a4e01876c473589b787fe9297b9233a43f4f3771c47898be4103

Contents?: true

Size: 1.33 KB

Versions: 12

Compression:

Stored size: 1.33 KB

Contents

require 'monkey/string'

module Tagomatic

  class SystemConfiguration < Hash

    def initialize(&block)
      instance_eval(&block) if block_given?
    end

    alias :super_method_missing :method_missing

    def method_missing(name, *arguments)
      handler = MetaHandler.new(self, name)
      handler.invoke(arguments)
    end

    def retrieve(key)
      key = key.to_sym
      self[key] || raise("global object #{key} not registered")
    end

    def register(assignment_hash)
      assignment_hash.each do |key, value|
        key = key.to_sym
        self[key] = value
      end
    end

    class MetaHandler

      def initialize(target, name)
        @target = target
        @name = name
      end

      def invoke(arguments)
        if is_calling?(:register)
          execute_invoke :register, arguments
        elsif is_calling?(:retrieve) or is_calling?(:get)
          execute_invoke :retrieve, arguments
        else
          raise "unsupported invocation: #{@name}"
        end
      end

      def is_calling?(method)
        @name.to_s.starts_with?("#{method}_")
      end

      def execute_invoke(method, arguments)
        key = extract_key
        full_arguments = [key] + arguments
        @target.send(method, *full_arguments)
      end

      def extract_key
        @name.to_s.sub(/^[^_]+_/, '')
      end

    end
  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
tagomatic-0.1.9 lib/tagomatic/system_configuration.rb
tagomatic-0.1.8 lib/tagomatic/system_configuration.rb
tagomatic-0.1.7 lib/tagomatic/system_configuration.rb
tagomatic-0.1.6 lib/tagomatic/system_configuration.rb
tagomatic-0.1.5 lib/tagomatic/system_configuration.rb
tagomatic-0.1.4 lib/tagomatic/system_configuration.rb
tagomatic-0.1.3 lib/tagomatic/system_configuration.rb
tagomatic-0.1.2 lib/tagomatic/system_configuration.rb
tagomatic-0.1.1 lib/tagomatic/system_configuration.rb
tagomatic-0.1.0 lib/tagomatic/system_configuration.rb
tagomatic-0.0.3 lib/tagomatic/system_configuration.rb
tagomatic-0.0.2 lib/tagomatic/system_configuration.rb