Class: Attributor::URI

Inherits:
Object
  • Object
show all
Includes:
Type
Defined in:
lib/attributor/types/uri.rb

Class Method Summary collapse

Methods included from Type

included

Class Method Details

.check_option!(name, definition) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/attributor/types/uri.rb', line 56

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

.dump(value, **_opts) ⇒ Object



41
42
43
# File 'lib/attributor/types/uri.rb', line 41

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

.example(_context = nil, options: {}) ⇒ Object



25
26
27
# File 'lib/attributor/types/uri.rb', line 25

def self.example(_context = nil, options: {})
  URI(Randgen.uri)
end

.familyObject



8
9
10
# File 'lib/attributor/types/uri.rb', line 8

def self.family
  String.family
end

.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/attributor/types/uri.rb', line 29

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

.native_typeObject



21
22
23
# File 'lib/attributor/types/uri.rb', line 21

def self.native_type
  ::URI::Generic
end

.valid_type?(value) ⇒ Boolean

Returns:



12
13
14
15
16
17
18
19
# File 'lib/attributor/types/uri.rb', line 12

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

.validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, attribute) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/attributor/types/uri.rb', line 45

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