Sha256: c0955ee7e36577f14677a329096194a22acbf84c7e92e9e373ee802606ce6932

Contents?: true

Size: 791 Bytes

Versions: 2

Compression:

Stored size: 791 Bytes

Contents

module Attributor
  class String
    include Type

    def self.native_type
      ::String
    end

    def self.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **options)
      if value.is_a?(Enumerable)
        raise IncompatibleTypeError.new(context: context, value_type: value.class, type: self)
      end

      value && String(value)
    rescue
      super
    end

    def self.example(_context = nil, options: {})
      if options[:regexp]
        begin
          # It may fail to generate an example, see bug #72.
          options[:regexp].gen
        rescue => e
          format('Failed to generate example for %s : %s', options[:regexp].inspect, e.message)
        end
      else
        /\w+/.gen
      end
    end

    def self.family
      'string'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
attributor-5.4 lib/attributor/types/string.rb
attributor-5.3 lib/attributor/types/string.rb