Sha256: 1944624d43f6ebdb28208cf8f75d2a55d96f53d9edf3834c5f9de948d50fe0f6

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

module Midori
  # Provide flexible configuration for Midori Server
  module Configurable
    # Modified from Sinatra
    # Sets an option to the given value.
    # @param [Symbol] option the name of config
    # @param [Object] value value to the name
    # @param [Boolean] read_only Generate option= method or not
    def set(option, value = (not_set = true), read_only = false)
      raise ArgumentError if not_set

      setter = proc { |val| set option, val }
      getter = proc { value }

      define_singleton("#{option}=", setter) unless read_only
      define_singleton(option, getter)
      define_singleton("#{option}?", "!!#{option}") unless method_defined? "#{option}?"

      self
    end

    private

    # Dynamically defines a method on settings.
    def define_singleton(name, content = Proc.new)
      singleton_class.class_eval do
        undef_method(name) if method_defined? name
        String === content ? class_eval("def #{name}() #{content}; end") : define_method(name, &content)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
em-midori-0.1.0 lib/em-midori/core_ext/configurable.rb