Sha256: 800b4d6f8ea6d9b5c6bda2cd972fdee054e0e1659b18517aeeddb2286751f0bb

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

module ClassConfig

  VERSION = '0.0.2'

  def attr_config(name, default_value=nil)
    configuration_defaults[name.to_sym] = default_value

    define_singleton_method name do
      configuration[name.to_sym]
    end

    define_singleton_method "#{name}=" do |value|
      configuration_values[name.to_sym] = value
    end
  end

  def configure(&block)
    tap &block
    @after_config_callback.call self if @after_config_callback
  end

  def after_config(&block)
    @after_config_callback = block
  end

  def configuration
    configuration_defaults.merge configuration_values
  end

  def restore_default_configuration
    configuration_values.clear
  end

  def inherited(subclass)
    %w(configuration_defaults configuration_values after_config_callback).each do |var|
      subclass.instance_variable_set "@#{var}", instance_variable_get("@#{var}")
    end
  end

  private

  def configuration_defaults
    @configuration_defaults ||= {}
  end

  def configuration_values
    @configuration_values ||= {}
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
class_config-0.0.2 lib/class_config.rb