Sha256: 77718b620f2d620616f440f865cb3b4e226f57b6511d2b5c86fd03f6b5d40c74

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

# Represents a href type.
require 'uri'

module Attributor
  class URI
    include Attributor::Type

    def self.family
      String.family
    end

    def self.valid_type?(value)
      case value
      when ::String, ::URI::Generic
        true
      else
        false
      end
    end

    def self.native_type
      return ::URI::Generic
    end

    def self.example(context=nil, options={})
      URI(/[:uri:]/.gen)
    end

    def self.load(value, context=Attributor::DEFAULT_ROOT_CONTEXT, **options)
      return nil if value.nil?
      case value
      when self.native_type
        value
      when ::String
        URI(value)
      else
        raise CoercionError, context: context, from: value.class, to: self, value: value
      end
    end

    def self.dump(value, **opts)
      value.to_s
    end

    def self.validate(value,context=Attributor::DEFAULT_ROOT_CONTEXT,attribute)
      errors = []

      if attribute && (definition = attribute.options[:path])
        unless value.path =~ attribute.options[:path]
          errors << "#{Attributor.humanize_context(context)} value (#{value}) does not match path (#{definition.inspect})"
        end
      end
      errors
    end

    def self.check_option!(name, definition)
      case name
      when :path
        unless definition.is_a? ::Regexp
          raise AttributorException.new("Value for option :path is not a Regexp object. Got (#{definition.inspect})")
        end
        :ok
      else
        :unknown
      end
    end

  end
end

class Randgen
  def self.uri
    "http://example.com/#{word}/#{rand(10 ** 9)}"
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
attributor-5.0.2 lib/attributor/types/uri.rb
attributor-5.0.1 lib/attributor/types/uri.rb
attributor-5.0 lib/attributor/types/uri.rb
attributor-4.2.0 lib/attributor/types/uri.rb
attributor-4.1.0 lib/attributor/types/uri.rb