Class: Jschematic::Attributes::Type
- Inherits:
-
Object
- Object
- Jschematic::Attributes::Type
- Includes:
- Element
- Defined in:
- lib/jschematic/attributes/type.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Attributes included from Element
Instance Method Summary collapse
- #accepts?(instance) ⇒ Boolean
-
#initialize(type) ⇒ Type
constructor
A new instance of Type.
Methods included from Element
#required?, #schema_for, #title, #to_s
Constructor Details
#initialize(type) ⇒ Type
Returns a new instance of Type.
10 11 12 |
# File 'lib/jschematic/attributes/type.rb', line 10 def initialize(type) @type = type end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
8 9 10 |
# File 'lib/jschematic/attributes/type.rb', line 8 def type @type end |
Instance Method Details
#accepts?(instance) ⇒ Boolean
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/jschematic/attributes/type.rb', line 14 def accepts?(instance) return true unless type case type when /^object$/ assert_kind_of([Hash], instance) when /^number$/ assert_kind_of([Numeric], instance) when /^integer$/ assert_kind_of([Integer], instance) when /^boolean$/ assert_kind_of([TrueClass, FalseClass], instance) when /^null$/ assert_kind_of([NilClass], instance) when /^any$/ true when Array # union # TODO: this is gross. A specific Union type is likely called for type.any? do |union_type| begin if String===union_type Type.new(union_type).accepts?(instance) elsif Hash===union_type Schema.new(union_type).accepts?(instance) end rescue ValidationError false end end || fail_validation!(type, instance) else # TODO: probably worth just putting in explicit mapping for all # JSON schema types--there are only a few left assert_kind_of([constantize(type)], instance) end end |