jimmy 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b1cb2f2d598473a594b870040ab3b022caf9ca4
4
- data.tar.gz: f283a986673ede842adf3f2e944410005fb87695
3
+ metadata.gz: fa577a95bf9548660e2354097ef6777df04ed9b0
4
+ data.tar.gz: 6f229b95ec4d14465d439a9e2fea644a0088d1a4
5
5
  SHA512:
6
- metadata.gz: 49ecc7449f9195e2839429944dcf9aa7e42db540e3cac4cd201f350c72798d27601ba75beee800abb1c7c5c8790e11578703496715322ce3d99eb0d179a75d4f
7
- data.tar.gz: 12f4c5e48b02c700cb815bed05cf89a5e299cec8ce22907e1345eb51159fbd1e6c5daede18b9d6484b7fcbb5df69bae29dfdf5105ee403a2616f95b85ad3d7c5
6
+ metadata.gz: 35a2afb25ad10e4bc5bdf15b509d3a48973c948fc42faf4e20a9035f7d2fa899080edb24ae9146fe779ed6f5419314206902648aa6c2c1b2aaafbde509214840
7
+ data.tar.gz: 06cfcc5aacaf030fac97db89d5ae976bf74828b195825e35b442663659cc379de74bfadf31c64c23ca7b7c13674e4b76b8abf0dedf15443846093febe96d2e18
@@ -2,6 +2,7 @@ require_relative 'schema_types'
2
2
 
3
3
  module Jimmy
4
4
  class Combination < Array
5
+ include SchemaCreation::Referencing
5
6
 
6
7
  attr_reader :condition, :schema
7
8
 
@@ -2,6 +2,7 @@ require 'forwardable'
2
2
 
3
3
  module Jimmy
4
4
  class Definitions
5
+ include SchemaCreation::Referencing
5
6
  extend Forwardable
6
7
  delegate %i[empty? key? map] => :@values
7
8
 
data/lib/jimmy/domain.rb CHANGED
@@ -16,6 +16,7 @@ module Jimmy
16
16
  @types = {}
17
17
  @partials = {}
18
18
  @import_paths = []
19
+ @uri_formatter = -> _, name { @root + "#{name}.json#" }
19
20
  end
20
21
 
21
22
  def domain
@@ -65,6 +66,14 @@ module Jimmy
65
66
  @types.each { |name, schema| export_schema schema, path + 'types' + "#{name.to_s}.json" }
66
67
  end
67
68
 
69
+ def uri_for(name)
70
+ @uri_formatter.call root, name
71
+ end
72
+
73
+ def uri_format(&block)
74
+ @uri_formatter = block
75
+ end
76
+
68
77
  private
69
78
 
70
79
  def glob(base_path, only: '.', ignore: nil, &block)
@@ -80,6 +89,7 @@ module Jimmy
80
89
  end
81
90
 
82
91
  def load_schema_from_path(path, name)
92
+ @schema_name = name
83
93
  instance_eval(path.read, path.to_s).schema.tap do |schema|
84
94
  schema.name = name.to_s
85
95
  JSON::Validator.add_schema JSON::Schema.new(schema.to_h, nil)
data/lib/jimmy/link.rb CHANGED
@@ -48,7 +48,7 @@ module Jimmy
48
48
  end
49
49
 
50
50
  def schema(type = nil, &block)
51
- link.schemas[type] = Schema.new(:object, domain, {}, &block)
51
+ link.schemas[type] = Schema.new(:object, link.schema).tap { |s| s.setup &block }
52
52
  end
53
53
 
54
54
  def set(**values)
data/lib/jimmy/schema.rb CHANGED
@@ -4,7 +4,8 @@ module Jimmy
4
4
  JSON_HYPER_SCHEMA_URI = 'http://json-schema.org/draft-04/hyper-schema#'
5
5
 
6
6
  attr_reader :dsl, :attrs, :domain, :type, :parent
7
- attr_accessor :name
7
+ attr_writer :name
8
+ attr_accessor :nullable
8
9
 
9
10
  @argument_handlers = Hash.new { |hash, key| hash[key] = {} }
10
11
 
@@ -29,16 +30,21 @@ module Jimmy
29
30
  compiler ||= SchemaTypes.compilers[schema_class]
30
31
  schema_class = schema_class.superclass
31
32
  end
32
- {'type' => type.to_s}.tap do |hash|
33
- hash['definitions'] = definitions.compile unless definitions.empty?
34
- hash['links'] = links.map &:compile unless links.empty?
35
- hash.merge! data
36
- dsl.evaluate compiler, hash if compiler
37
- end
33
+ hash = {}
34
+ hash['type'] = nullable ? ['null', type.to_s] : type.to_s
35
+ hash['definitions'] = definitions.compile unless definitions.empty?
36
+ hash['links'] = links.map &:compile unless links.empty?
37
+ hash.merge! data
38
+ dsl.evaluate compiler, hash if compiler
39
+ hash
40
+ end
41
+
42
+ def name
43
+ @name || (parent && parent.name)
38
44
  end
39
45
 
40
- def url
41
- "#{domain.root}/#{name}.json#"
46
+ def uri
47
+ domain.uri_for name
42
48
  end
43
49
 
44
50
  def definitions
@@ -63,22 +69,25 @@ module Jimmy
63
69
 
64
70
  def to_h
65
71
  {'$schema' => schema_uri}.tap do |h|
66
- h['id'] = url if name
72
+ h['id'] = uri.to_s if name
67
73
  h.merge! compile
68
74
  end
69
75
  end
70
76
 
71
77
  def validate(data)
72
- errors = JSON::Validator.fully_validate(JSON::Validator.schema_for_uri(url).schema, data, errors_as_objects: true)
78
+ errors = JSON::Validator.fully_validate(JSON::Validator.schema_for_uri(uri).schema, data, errors_as_objects: true)
73
79
  raise ValidationError.new(self, data, errors) unless errors.empty?
74
80
  end
75
81
 
76
- def initialize(type, parent, locals, *args, &block)
82
+ def initialize(type, parent)
77
83
  @attrs = {}
78
84
  @type = type
79
85
  @domain = parent.domain
80
86
  @dsl = SchemaTypes.dsls[type].new(self)
81
87
  @parent = parent if parent.is_a? self.class
88
+ end
89
+
90
+ def setup(*args, **locals, &block)
82
91
  args.each do |arg|
83
92
  case arg
84
93
  when Symbol
@@ -29,6 +29,29 @@ module Jimmy
29
29
  %i[title description default].each { |k| define_method(k) { |v| set k => v } }
30
30
  end
31
31
 
32
+ module Referencing
33
+ def method_missing(name, *args, &block)
34
+ if schema.definitions[name]
35
+ ref *args, definition(name)
36
+ else
37
+ super
38
+ end
39
+ end
40
+
41
+ def respond_to_missing?(name, *)
42
+ schema.definitions.key?(name) || super
43
+ end
44
+
45
+ def definition(id)
46
+ "/#{schema.name}#/definitions/#{id}"
47
+ end
48
+
49
+ def ref(*args, uri)
50
+ handler = SchemaCreation.handlers[self.class]
51
+ instance_exec(Reference.new(uri), *args, &handler) if handler
52
+ end
53
+ end
54
+
32
55
  module DefiningMethods
33
56
  include MetadataMethods
34
57
 
@@ -58,11 +81,10 @@ module Jimmy
58
81
  handler_args = handler && inner_args.shift(handler.arity - 1)
59
82
  child_schema = Schema.new(
60
83
  method,
61
- respond_to?(:schema) ? schema : domain,
62
- locals,
63
- *inner_args,
64
- &inner_block
84
+ respond_to?(:schema) ? schema : domain
65
85
  )
86
+ child_schema.name = @schema_name if is_a? Domain
87
+ child_schema.setup *inner_args, **locals, &inner_block
66
88
  instance_exec child_schema, *handler_args, &handler if handler
67
89
  child_schema.dsl
68
90
  end
@@ -37,6 +37,7 @@ module Jimmy
37
37
 
38
38
  class DSL
39
39
  extend Forwardable
40
+ include SchemaCreation::Referencing
40
41
  include SchemaCreation::MetadataMethods
41
42
 
42
43
  attr_reader :schema
@@ -76,27 +77,14 @@ module Jimmy
76
77
  schema.data
77
78
  end
78
79
 
79
- def ref(*args, uri)
80
- handler = SchemaCreation.handlers[self.class]
81
- instance_exec(Reference.new(uri), *args, &handler) if handler
82
- end
83
-
84
80
  def link(rel_and_href, &block)
85
- link = Link.new(self, *rel_and_href.first)
81
+ link = Link.new(schema, *rel_and_href.first)
86
82
  schema.links << link
87
83
  link.dsl.evaluate &block if block
88
84
  end
89
85
 
90
- def method_missing(name, *args, &block)
91
- if schema.definitions[name]
92
- ref *args, "#/definitions/#{name}"
93
- else
94
- super
95
- end
96
- end
97
-
98
- def respond_to_missing?(name, *)
99
- schema.definitions.key?(name) || super
86
+ def nullable
87
+ schema.nullable = true
100
88
  end
101
89
 
102
90
  private
@@ -34,7 +34,8 @@ module Jimmy
34
34
  hash[collection] ||= {}
35
35
  hash[collection][key] = value.compile
36
36
  end
37
- hash['required'] = (attrs[:required] || []).to_a
37
+ required = attrs[:required]
38
+ hash['required'] = required.to_a if required && !required.empty?
38
39
  hash['additionalProperties'] = !!attrs[:additional_properties]
39
40
  end
40
41
 
data/lib/jimmy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Jimmy
2
- VERSION = '0.3.3'
2
+ VERSION = '0.3.4'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jimmy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil E. Pearson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-17 00:00:00.000000000 Z
11
+ date: 2015-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema