class Attributor::String

Public Class Methods

as_json_schema( shallow: false, example: nil, attribute_options: {} ) click to toggle source

TODO: we’re passing the attribute options for now…might need to rethink …although these are type-specific… TODO: multipleOf, minimum, maximum, exclusiveMinimum and exclusiveMaximum

Calls superclass method
# File lib/attributor/types/string.rb, line 33
def self.as_json_schema( shallow: false, example: nil, attribute_options: {} )
  h = super
  opts = ( self.respond_to?(:options) ) ? self.options.merge( attribute_options ) : attribute_options
  h[:pattern] = self.human_readable_regexp(opts[:regexp]) if opts[:regexp]
  # TODO: minLength, maxLength
  h
end
example(_context = nil, options: {}) click to toggle source
# File lib/attributor/types/string.rb, line 19
def self.example(_context = nil, options: {})
  Faker::Lorem.word
end
family() click to toggle source
# File lib/attributor/types/string.rb, line 23
def self.family
  'string'
end
human_readable_regexp( reg ) click to toggle source
# File lib/attributor/types/string.rb, line 41
def self.human_readable_regexp( reg )
  return $1 if reg.to_s =~ /\(\?[^:]+:(.+)\)/
  reg
end
json_schema_type() click to toggle source
# File lib/attributor/types/string.rb, line 27
def self.json_schema_type
  :string
end
load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **options) click to toggle source
Calls superclass method
# File lib/attributor/types/string.rb, line 9
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
native_type() click to toggle source
# File lib/attributor/types/string.rb, line 5
def self.native_type
  ::String
end