Class: Attributor::String
- Inherits:
-
Object
- Object
- Attributor::String
- Includes:
- Type
- Defined in:
- lib/attributor/types/string.rb
Class Method Summary collapse
-
.as_json_schema(shallow: false, example: nil, attribute_options: {}) ⇒ Object
TODO: we’re passing the attribute options for now…might need to rethink …although these are type-specific…
- .example(_context = nil, options: {}) ⇒ Object
- .family ⇒ Object
- .human_readable_regexp(reg) ⇒ Object
- .json_schema_type ⇒ Object
- .load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **options) ⇒ Object
- .native_type ⇒ Object
Class Method Details
.as_json_schema(shallow: false, example: nil, attribute_options: {}) ⇒ Object
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
42 43 44 45 46 47 48 |
# File 'lib/attributor/types/string.rb', line 42 def self.as_json_schema( shallow: false, example: nil, attribute_options: {} ) h = super opts = ( self.respond_to?(:options) ) ? self..merge( ) : h[:pattern] = self.human_readable_regexp(opts[:regexp]) if opts[:regexp] # TODO: minLength, maxLength h end |
.example(_context = nil, options: {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/attributor/types/string.rb', line 19 def self.example(_context = nil, options: {}) if [:regexp] begin # It may fail to generate an example, see bug #72. [:regexp].gen rescue => e format('Failed to generate example for %s : %s', [:regexp].inspect, e.) end else /\w+/.gen end end |
.family ⇒ Object
32 33 34 |
# File 'lib/attributor/types/string.rb', line 32 def self.family 'string' end |
.human_readable_regexp(reg) ⇒ Object
50 51 52 53 |
# File 'lib/attributor/types/string.rb', line 50 def self.human_readable_regexp( reg ) return $1 if reg.to_s =~ /\(\?[^:]+:(.+)\)/ reg end |
.json_schema_type ⇒ Object
36 37 38 |
# File 'lib/attributor/types/string.rb', line 36 def self.json_schema_type :string end |
.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **options) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/attributor/types/string.rb', line 9 def self.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **) 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 ⇒ Object
5 6 7 |
# File 'lib/attributor/types/string.rb', line 5 def self.native_type ::String end |