Sha256: f05ea40c2bef4872e829a27c6f2d523924b71c708e65427ad5830adbc6da8a91

Contents?: true

Size: 954 Bytes

Versions: 4

Compression:

Stored size: 954 Bytes

Contents


module Corundum
  module Configurable
    def local_attrs
      @local_attrs ||=
        begin
          mod = Module.new
          extend mod
          mod
        end
    end

    def local_attrs=(mod)
      extend mod
      @local_attrs = mod
    end

    def setting(name, default_value = nil)
      local_attrs.instance_eval do
        attr_accessor(name)
      end
      instance_variable_set("@#{name}", default_value)
    end

    def dup
      result = super
      result.extend Configurable
      result.local_attrs = @local_attrs
      result
    end

    def settings(hash)
      hash.each_pair do |name, value|
        setting(name, value)
      end
      return self
    end

    def nested(hash=nil)
      obj = Object.new
      obj.extend Configurable
      obj.settings(hash || {})
      return obj
    end

    def nil_fields(*names)
      names.each do |name|
        setting(name, nil)
      end
      return self
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
corundum-0.0.6 lib/corundum/configurable.rb
corundum-0.0.5 lib/corundum/configurable.rb
corundum-0.0.3 lib/corundum/configurable.rb
corundum-0.0.2 lib/corundum/configurable.rb