Class: Attributor::URI
- Inherits:
-
Object
- Object
- Attributor::URI
- Includes:
- Type
- Defined in:
- lib/attributor/types/uri.rb
Class Method Summary collapse
- .check_option!(name, definition) ⇒ Object
- .dump(value, **_opts) ⇒ Object
- .example(_context = nil, options: {}) ⇒ Object
- .family ⇒ Object
- .load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) ⇒ Object
- .native_type ⇒ Object
- .valid_type?(value) ⇒ Boolean
- .validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, attribute) ⇒ Object
Methods included from Type
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 |
.family ⇒ Object
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, **) 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_type ⇒ Object
21 22 23 |
# File 'lib/attributor/types/uri.rb', line 21 def self.native_type ::URI::Generic end |
.valid_type?(value) ⇒ Boolean
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.[:path]) unless value.path =~ attribute.[:path] errors << "#{Attributor.humanize_context(context)} value (#{value}) does not match path (#{definition.inspect})" end end errors end |