Sha256: 2045c0d62682153cf3ea0653b12e3949bfee05adf263d638afcd61c1be072c31

Contents?: true

Size: 1.83 KB

Versions: 6

Compression:

Stored size: 1.83 KB

Contents

# encoding: utf-8

require 'pathname'
require 'necromancer'

require_relative 'converter_dsl'

module TTY
  class Prompt
    module Converters
      extend ConverterDSL

      # Delegate Necromancer errors
      #
      # @api private
      def self.on_error
        if block_given?
          yield
        else
          raise ArgumentError, 'You need to provide a block argument.'
        end
      rescue Necromancer::ConversionTypeError => e
        raise ConversionError, e.message
      end

      converter(:bool) do |input|
        on_error { Necromancer.convert(input).to(:boolean, strict: true) }
      end

      converter(:string) do |input|
        String(input).chomp
      end

      converter(:symbol) do |input|
        input.to_sym
      end

      converter(:date) do |input|
        on_error { Necromancer.convert(input).to(:date, strict: true) }
      end

      converter(:datetime) do |input|
        on_error { Necromancer.convert(input).to(:datetime, strict: true) }
      end

      converter(:int) do |input|
        on_error { Necromancer.convert(input).to(:integer, strict: true) }
      end

      converter(:float) do |input|
        on_error { Necromancer.convert(input).to(:float, strict: true) }
      end

      converter(:range) do |input|
        on_error { Necromancer.convert(input).to(:range, strict: true) }
      end

      converter(:regexp) do |input|
        Regexp.new(input)
      end

      converter(:file) do |input|
        directory = ::File.expand_path(::File.dirname($0))
        ::File.open(::File.join(directory, input))
      end

      converter(:path) do |input|
        directory = ::File.expand_path(::File.dirname($0))
        Pathname.new(::File.join(directory, input))
      end

      converter(:char) do |input|
        String(input).chars.to_a[0]
      end
    end # Converters
  end # Prompt
end # TTY

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
tty-prompt-0.13.2 lib/tty/prompt/converters.rb
tty-prompt-0.13.1 lib/tty/prompt/converters.rb
tty-prompt-0.13.0 lib/tty/prompt/converters.rb
austb-tty-prompt-0.13.0 lib/tty/prompt/converters.rb
tty-prompt-0.12.0 lib/tty/prompt/converters.rb
tty-prompt-0.11.0 lib/tty/prompt/converters.rb