Class: Jimmy::SchemerFactory
- Inherits:
-
Object
- Object
- Jimmy::SchemerFactory
- Defined in:
- lib/jimmy/schemer_factory.rb
Overview
Factory class for making JSONSchemer::Schema::Base
instances
Class Method Summary collapse
-
.available? ⇒ true, false
Returns true if the
json_schemer
gem is loaded.
Instance Method Summary collapse
-
#initialize(schema, *resolvers, cache_resolvers: true, **opts) ⇒ SchemerFactory
constructor
A new instance of SchemerFactory.
- #resolve(uri) ⇒ Hash{String => Object}?
-
#schemer ⇒ JSONSchemer::Schema::Base
Get an instance of
JSONSchemer::Schema::Base
that can be used to validate JSON documents against the given Schema.
Constructor Details
#initialize(schema, *resolvers, cache_resolvers: true, **opts) ⇒ SchemerFactory
Returns a new instance of SchemerFactory.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/jimmy/schemer_factory.rb', line 16 def initialize(schema, *resolvers, cache_resolvers: true, **opts) unless self.class.available? raise LoadError, 'Please add the json_schemer gem to your Gemfile' end @schema = schema @resolvers = resolvers.map(&method(:cast_resolver)) @options = opts.dup return if @resolvers.empty? res = method(:resolve) res = JSONSchemer::CachedRefResolver.new(&res) if cache_resolvers @options[:ref_resolver] = res end |
Class Method Details
.available? ⇒ true, false
Returns true if the json_schemer
gem is loaded.
8 9 10 |
# File 'lib/jimmy/schemer_factory.rb', line 8 def self.available? defined? ::JSONSchemer end |
Instance Method Details
#resolve(uri) ⇒ Hash{String => Object}?
41 42 43 44 45 46 47 48 49 |
# File 'lib/jimmy/schemer_factory.rb', line 41 def resolve(uri) @resolvers.each do |resolver| return resolver.call(uri) unless resolver.respond_to? :resolve schema = resolver.resolve(uri) return schema.as_json if schema end nil end |
#schemer ⇒ JSONSchemer::Schema::Base
Get an instance of JSONSchemer::Schema::Base
that can be used to validate JSON documents against the given Jimmy::Schema.
35 36 37 |
# File 'lib/jimmy/schemer_factory.rb', line 35 def schemer @schemer ||= JSONSchemer.schema(@schema.as_json, **@options) end |