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

get_memoized_collection_class, set_memoized_collection_class

Class Method Details

.check_option!(name, definition) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/attributor/types/uri.rb', line 64

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



49
50
51
# File 'lib/attributor/types/uri.rb', line 49

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

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



33
34
35
# File 'lib/attributor/types/uri.rb', line 33

def self.example(_context = nil, options: {})
  URI("https://example.com/#{Attributor::String.example}")
end

.familyObject



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

def self.family
  String.family
end

.json_schema_string_formatObject



29
30
31
# File 'lib/attributor/types/uri.rb', line 29

def self.json_schema_string_format
  :uri
end

.json_schema_typeObject



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

def self.json_schema_type
  :string
end

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



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/attributor/types/uri.rb', line 37

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.new(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



53
54
55
56
57
58
59
60
61
62
# File 'lib/attributor/types/uri.rb', line 53

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