Sha256: 54ce2ac69221b14b1a3bacc389230c99fdb39bd438aa489b34a3813a14877c2d

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

require 'json'
require 'pathname'

module Skippy::ConfigAccessors

  private

  def config_attr(*symbols, key: nil, default: nil, type: nil)
    config_attr_reader(*symbols, key: key, type: type, default: default)
    config_attr_writer(*symbols, key: key, type: type)
    nil
  end

  def config_attr_reader(*symbols, key: nil, default: nil, type: nil)
    class_eval do
      symbols.each { |symbol|
        raise TypeError unless symbol.is_a?(Symbol)

        define_method(symbol) do
          value = @config.get(key || symbol, default)
          value = type.new(value) if type && !value.is_a?(type)
          value
        end
      }
    end
    nil
  end

  def config_attr_writer(*symbols, key: nil, type: nil)
    class_eval do
      symbols.each { |symbol|
        raise TypeError unless symbol.is_a?(Symbol)

        symbol_set = "#{symbol}=".intern
        define_method(symbol_set) do |value|
          value = type.new(value) if type && !value.is_a?(type)
          @config.set(key || symbol, value)
          value
        end
      }
    end
    nil
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
skippy-0.4.3.a lib/skippy/config_accessors.rb
skippy-0.4.2.a lib/skippy/config_accessors.rb