Sha256: ce4bc99fdbe326a8dc0c5f3950167f1b0960b4caf3348d4d950f9a2aab829aad

Contents?: true

Size: 742 Bytes

Versions: 13

Compression:

Stored size: 742 Bytes

Contents

class Cl
  module Cast
    TRUE  = /^(true|yes|on)$/
    FALSE = /^(false|no|off)$/

    def cast(value)
      case type
      when nil
        value
      when :array
        Array(value).compact.flatten.map { |value| value.to_s.split(',') }.flatten
      when :string, :str
        value.to_s unless value.to_s.empty?
      when :flag, :boolean, :bool
        return true  if value.to_s =~ TRUE
        return false if value.to_s =~ FALSE
        !!value
      when :integer, :int
        Integer(value)
      when :float
        Float(value)
      else
        raise ArgumentError, "Unknown type: #{type}" if value
      end
    rescue ::ArgumentError => e
      raise ArgumentError.new(:wrong_type, value.inspect, type)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
cl-0.1.12 lib/cl/cast.rb
cl-0.1.11 lib/cl/cast.rb
cl-0.1.10 lib/cl/cast.rb
cl-0.1.9 lib/cl/cast.rb
cl-0.1.8 lib/cl/cast.rb
cl-0.1.7 lib/cl/cast.rb
cl-0.1.6 lib/cl/cast.rb
cl-0.1.5 lib/cl/cast.rb
cl-0.1.4 lib/cl/cast.rb
cl-0.1.3 lib/cl/cast.rb
cl-0.1.2 lib/cl/cast.rb
cl-0.1.1 lib/cl/cast.rb
cl-0.1.0 lib/cl/cast.rb