jimmy 0.3 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f47c1f31b48652bdbb4943497e356fe9bc0dbf57
4
- data.tar.gz: 0d3d60874cdd2a4cd28068ed570c424140add30d
3
+ metadata.gz: 8e19cc9c83e2881cb849f0e706878b4768171fcf
4
+ data.tar.gz: b0700ff1202645eb217e13ea2ae65bdccc043f89
5
5
  SHA512:
6
- metadata.gz: c02c4739d222fa9690046d3a6516a9cda574411de138e29f96a3a267fb627e8cfd34842f5a4a21727efdc5abcf719ece6438fbecc38307841767d2a8633fd952
7
- data.tar.gz: cfd705bf01d1195764285f3c94ab3d334eec1e23853a5fc6eb3d5bd68bf73038592048debbea6ceee5af59436c5864b417f88dabee5a28cccb1881f33ce06798
6
+ metadata.gz: 00dd76c52214222acca04a4bfe638f4a969c00484969c269d31b436c7cb77c53a2acda9cb61eefff14391d03899f0e12581beba3c3eb60775f0331ad4b13307c
7
+ data.tar.gz: 5ddeef3280aecdfd8598d027a0d3810dbca2440dce29fc90f599a78668e9f1a5f7310708fdc84a2246a41c09b437e8b740027a094a0b6eab4ae8b474d2570e12
@@ -18,6 +18,10 @@ module Jimmy
18
18
  map { |k, v| [k.to_s, v.compile] }.to_h
19
19
  end
20
20
 
21
+ def [](key)
22
+ super || (schema.parent && schema.parent.definitions[key])
23
+ end
24
+
21
25
  SchemaCreation.apply_to(self) { |schema, name| self[name] = schema }
22
26
  end
23
27
  end
data/lib/jimmy/link.rb CHANGED
@@ -51,6 +51,9 @@ module Jimmy
51
51
  link.schemas[type] = Schema.new(:object, domain, {}, &block)
52
52
  end
53
53
 
54
+ def set(**values)
55
+ values.each { |k, v| link[k.to_s] = v }
56
+ end
54
57
  end
55
58
  end
56
59
  end
data/lib/jimmy/schema.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  module Jimmy
2
2
  class Schema
3
+ JSON_SCHEMA_URI = 'http://json-schema.org/draft-04/schema#'
4
+ JSON_HYPER_SCHEMA_URI = 'http://json-schema.org/draft-04/hyper-schema#'
3
5
 
4
- attr_reader :dsl, :attrs, :domain, :type
6
+ attr_reader :dsl, :attrs, :domain, :type, :parent
5
7
  attr_accessor :name
6
8
 
7
9
  @argument_handlers = Hash.new { |hash, key| hash[key] = {} }
@@ -46,13 +48,24 @@ module Jimmy
46
48
  @links ||= []
47
49
  end
48
50
 
49
- def hash
50
- @hash ||= {'$schema' => 'http://json-schema.org/draft-04/schema#'}
51
+ def data
52
+ @data ||= {}
53
+ end
54
+
55
+ def hyper?
56
+ links.any?
57
+ end
58
+
59
+ def schema_uri
60
+ hyper? ? JSON_HYPER_SCHEMA_URI : JSON_SCHEMA_URI
51
61
  end
52
62
 
53
63
  def to_h
54
- hash['id'] = url if name
55
- hash.merge! compile
64
+ {'$schema' => schema_uri}.tap do |h|
65
+ h.merge! data
66
+ h['id'] = url if name
67
+ h.merge! compile
68
+ end
56
69
  end
57
70
 
58
71
  def validate(data)
@@ -60,11 +73,12 @@ module Jimmy
60
73
  raise ValidationError.new(self, data, errors) unless errors.empty?
61
74
  end
62
75
 
63
- def initialize(type, domain, locals, *args, &block)
76
+ def initialize(type, parent, locals, *args, &block)
64
77
  @attrs = {}
65
78
  @type = type
66
- @domain = domain
79
+ @domain = parent.domain
67
80
  @dsl = SchemaTypes.dsls[type].new(self)
81
+ @parent = parent if parent.is_a? self.class
68
82
  args.each do |arg|
69
83
  case arg
70
84
  when Symbol
@@ -47,9 +47,15 @@ module Jimmy
47
47
  handler = SchemaCreation.handlers[self.class]
48
48
  self.class.__send__ :define_method, method do |*inner_args, &inner_block|
49
49
  handler_args = handler && inner_args.shift(handler.arity - 1)
50
- schema = Schema.new(method, domain, locals, *inner_args, &inner_block)
51
- instance_exec schema, *handler_args, &handler if handler
52
- schema.dsl
50
+ child_schema = Schema.new(
51
+ method,
52
+ respond_to?(:schema) ? schema : domain,
53
+ locals,
54
+ *inner_args,
55
+ &inner_block
56
+ )
57
+ instance_exec child_schema, *handler_args, &handler if handler
58
+ child_schema.dsl
53
59
  end
54
60
  return __send__ method, *args, &block
55
61
  end
@@ -64,7 +64,7 @@ module Jimmy
64
64
  end
65
65
 
66
66
  def set(**values)
67
- values.each { |k, v| schema.hash[k.to_s] = v }
67
+ values.each { |k, v| schema.data[k.to_s] = v }
68
68
  end
69
69
 
70
70
  def definitions(&block)
@@ -5,6 +5,15 @@ module Jimmy
5
5
  trait :min_length
6
6
  trait :max_length
7
7
  trait(:pattern) { |regex| attrs[:pattern] = regex.is_a?(Regexp) ? regex.inspect.gsub(%r`^/|/[a-z]*$`, '') : regex }
8
+ trait(:format) { |value| attrs[:format] = value.to_s.gsub('_', '-') }
9
+ %i[
10
+ date_time
11
+ email
12
+ hostname
13
+ ipv4
14
+ ipv6
15
+ uri
16
+ ].each { |k| trait(k) { format k } }
8
17
  trait(Regexp) { |regex| pattern regex }
9
18
  trait Range do |value|
10
19
  variation = value.exclude_end? ? 1 : 0
@@ -20,7 +29,7 @@ module Jimmy
20
29
  trait(Array) { |value| attrs[:enum] = value.map(&:to_s) }
21
30
 
22
31
  compile do |hash|
23
- hash.merge! camelize_attrs(%i[min_length max_length pattern enum])
32
+ hash.merge! camelize_attrs(%i[min_length max_length pattern enum format])
24
33
  end
25
34
 
26
35
  end
data/lib/jimmy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Jimmy
2
- VERSION = '0.3'
2
+ VERSION = '0.3.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jimmy
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil E. Pearson