Sha256: e628c84c883a51f541e3d16c052b85d67004e58bf66949ab9eb86eb90fdec486

Contents?: true

Size: 809 Bytes

Versions: 1

Compression:

Stored size: 809 Bytes

Contents

class Str2Duck::Parser

  DEFAULT_STRATEGIES = [:datetime, :date, :time, :true, :false, :float, :integer, :json].freeze

  def initialize(*parse_strategies)

    @parse_strategies = parse_strategies.map(&:to_s).map(&:to_sym)
    @parse_strategies.push(*DEFAULT_STRATEGIES) if @parse_strategies.empty?

    require_activesupport

  end

  def parse(str)

    raise(ArgumentError, 'invalid input, must be string like') unless str.class <= String

    @parse_strategies.each do |method_name|
      var = Str2Duck::Converter.public_send(method_name, str)
      return var unless var.nil?
    end

    return str

  end

  protected

  def require_activesupport
    require(File.join 'active_support', 'time') unless @activesupport_required
  rescue LoadError
  ensure
    @activesupport_required = true
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
str2duck-2.0.0 lib/str2duck/parser.rb