Sha256: b5d9091c1749a73f7201ad765d5dbb94b7398a5e60937f55fbb0a77165af518b

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

require "king_konf/decoder"

module KingKonf
  class Variable
    attr_reader :name, :type, :default, :description, :options

    def initialize(name:, type:, default: nil, description: "", required: false, options: {})
      @name, @type, @default = name, type, default
      @description = description
      @required = required
      @options = options
    end

    def cast(value)
      case @type
      when :float then value.to_f
      else value
      end
    end

    def required?
      @required
    end

    def valid?(value)
      case @type
      when :string then value.is_a?(String) || value.nil?
      when :list then value.is_a?(Array)
      when :integer then value.is_a?(Integer) || value.nil?
      when :float then value.is_a?(Float) || value.is_a?(Integer) || value.nil?
      when :boolean then value == true || value == false
      else raise "invalid type #{@type}"
      end
    end

    def decode(value)
      Decoder.public_send(@type, value, **options)
    end

    %i(boolean integer float string list).each do |type|
      define_method("#{type}?") do
        @type == type
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
king_konf-0.1.10 lib/king_konf/variable.rb
king_konf-0.1.9 lib/king_konf/variable.rb
king_konf-0.1.8 lib/king_konf/variable.rb
king_konf-0.1.7 lib/king_konf/variable.rb