Sha256: 9825dc20422f64871f94659412bb03966c7c50d7c60492af4a22bc5ed52adb71

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

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

3 entries across 3 versions & 1 rubygems

Version Path
skippy-0.5.2.a lib/skippy/config_accessors.rb
skippy-0.5.1.a lib/skippy/config_accessors.rb
skippy-0.5.0.a lib/skippy/config_accessors.rb