jimmy 0.2.1 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jimmy.rb +4 -0
- data/lib/jimmy/definitions.rb +23 -0
- data/lib/jimmy/link.rb +56 -0
- data/lib/jimmy/reference.rb +13 -0
- data/lib/jimmy/schema.rb +16 -6
- data/lib/jimmy/schema_creation.rb +1 -1
- data/lib/jimmy/schema_type.rb +37 -4
- data/lib/jimmy/schema_types/array.rb +2 -2
- data/lib/jimmy/schema_types/object.rb +1 -1
- data/lib/jimmy/type_reference.rb +13 -0
- data/lib/jimmy/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f47c1f31b48652bdbb4943497e356fe9bc0dbf57
|
4
|
+
data.tar.gz: 0d3d60874cdd2a4cd28068ed570c424140add30d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c02c4739d222fa9690046d3a6516a9cda574411de138e29f96a3a267fb627e8cfd34842f5a4a21727efdc5abcf719ece6438fbecc38307841767d2a8633fd952
|
7
|
+
data.tar.gz: cfd705bf01d1195764285f3c94ab3d334eec1e23853a5fc6eb3d5bd68bf73038592048debbea6ceee5af59436c5864b417f88dabee5a28cccb1881f33ce06798
|
data/lib/jimmy.rb
CHANGED
@@ -9,8 +9,12 @@ require_relative 'jimmy/symbol_array'
|
|
9
9
|
|
10
10
|
require_relative 'jimmy/domain'
|
11
11
|
require_relative 'jimmy/schema'
|
12
|
+
require_relative 'jimmy/reference'
|
13
|
+
require_relative 'jimmy/type_reference'
|
12
14
|
require_relative 'jimmy/schema_creation'
|
13
15
|
require_relative 'jimmy/schema_types'
|
14
16
|
require_relative 'jimmy/schema_type'
|
15
17
|
require_relative 'jimmy/combination'
|
18
|
+
require_relative 'jimmy/link'
|
16
19
|
require_relative 'jimmy/validation_error'
|
20
|
+
require_relative 'jimmy/definitions'
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Jimmy
|
2
|
+
class Definitions < Hash
|
3
|
+
attr_reader :schema
|
4
|
+
|
5
|
+
def initialize(schema)
|
6
|
+
@schema = schema
|
7
|
+
end
|
8
|
+
|
9
|
+
def evaluate(&block)
|
10
|
+
instance_exec &block
|
11
|
+
end
|
12
|
+
|
13
|
+
def domain
|
14
|
+
schema.domain
|
15
|
+
end
|
16
|
+
|
17
|
+
def compile
|
18
|
+
map { |k, v| [k.to_s, v.compile] }.to_h
|
19
|
+
end
|
20
|
+
|
21
|
+
SchemaCreation.apply_to(self) { |schema, name| self[name] = schema }
|
22
|
+
end
|
23
|
+
end
|
data/lib/jimmy/link.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
module Jimmy
|
2
|
+
class Link < Hash
|
3
|
+
attr_reader :schema
|
4
|
+
|
5
|
+
def initialize(schema, rel, href)
|
6
|
+
@schema = schema
|
7
|
+
merge! 'rel' => rel.to_s,
|
8
|
+
'href' => href.to_s
|
9
|
+
end
|
10
|
+
|
11
|
+
def dsl
|
12
|
+
@dsl ||= DSL.new(self)
|
13
|
+
end
|
14
|
+
|
15
|
+
def schemas
|
16
|
+
@schemas ||= {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def domain
|
20
|
+
schema.domain
|
21
|
+
end
|
22
|
+
|
23
|
+
def compile
|
24
|
+
merge schemas.map { |k, v| [(k ? "#{k}Schema" : 'schema'), v.compile] }.to_h
|
25
|
+
end
|
26
|
+
|
27
|
+
class DSL
|
28
|
+
attr_reader :link
|
29
|
+
|
30
|
+
def initialize(link)
|
31
|
+
@link = link
|
32
|
+
end
|
33
|
+
|
34
|
+
def domain
|
35
|
+
link.domain
|
36
|
+
end
|
37
|
+
|
38
|
+
def title(value)
|
39
|
+
link['title'] = value
|
40
|
+
end
|
41
|
+
|
42
|
+
def method(value)
|
43
|
+
link['method'] = value.to_s.upcase
|
44
|
+
end
|
45
|
+
|
46
|
+
def evaluate(&block)
|
47
|
+
instance_exec &block
|
48
|
+
end
|
49
|
+
|
50
|
+
def schema(type = nil, &block)
|
51
|
+
link.schemas[type] = Schema.new(:object, domain, {}, &block)
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/jimmy/schema.rb
CHANGED
@@ -28,6 +28,8 @@ module Jimmy
|
|
28
28
|
schema_class = schema_class.superclass
|
29
29
|
end
|
30
30
|
{'type' => type.to_s}.tap do |hash|
|
31
|
+
hash['definitions'] = definitions.compile unless definitions.empty?
|
32
|
+
hash['links'] = links.map &:compile unless links.empty?
|
31
33
|
dsl.evaluate compiler, hash if compiler
|
32
34
|
end
|
33
35
|
end
|
@@ -36,11 +38,21 @@ module Jimmy
|
|
36
38
|
"#{domain.root}/#{name}.json#"
|
37
39
|
end
|
38
40
|
|
41
|
+
def definitions
|
42
|
+
@definitions ||= Definitions.new(self)
|
43
|
+
end
|
44
|
+
|
45
|
+
def links
|
46
|
+
@links ||= []
|
47
|
+
end
|
48
|
+
|
49
|
+
def hash
|
50
|
+
@hash ||= {'$schema' => 'http://json-schema.org/draft-04/schema#'}
|
51
|
+
end
|
52
|
+
|
39
53
|
def to_h
|
40
|
-
|
41
|
-
|
42
|
-
hash.merge! compile
|
43
|
-
end
|
54
|
+
hash['id'] = url if name
|
55
|
+
hash.merge! compile
|
44
56
|
end
|
45
57
|
|
46
58
|
def validate(data)
|
@@ -48,8 +60,6 @@ module Jimmy
|
|
48
60
|
raise ValidationError.new(self, data, errors) unless errors.empty?
|
49
61
|
end
|
50
62
|
|
51
|
-
private
|
52
|
-
|
53
63
|
def initialize(type, domain, locals, *args, &block)
|
54
64
|
@attrs = {}
|
55
65
|
@type = type
|
@@ -57,7 +57,7 @@ module Jimmy
|
|
57
57
|
domain.autoload_type method
|
58
58
|
|
59
59
|
if domain.types.key? method
|
60
|
-
return instance_exec method, *args, &SchemaCreation.handlers[self.class]
|
60
|
+
return instance_exec TypeReference.new(method), *args, &SchemaCreation.handlers[self.class]
|
61
61
|
end
|
62
62
|
|
63
63
|
super method, *args, &block
|
data/lib/jimmy/schema_type.rb
CHANGED
@@ -55,10 +55,6 @@ module Jimmy
|
|
55
55
|
included_args.map { |arg| [arg.to_s.gsub(/_([a-z])/) { $1.upcase }, attrs[arg]] }.to_h
|
56
56
|
end
|
57
57
|
|
58
|
-
def compile_schema(schema)
|
59
|
-
schema.is_a?(Symbol) ? {'$ref' => "/types/#{schema}.json#"} : schema.compile
|
60
|
-
end
|
61
|
-
|
62
58
|
def include(*partial_names, **locals)
|
63
59
|
partial_names.each do |name|
|
64
60
|
with_locals locals do
|
@@ -67,6 +63,43 @@ module Jimmy
|
|
67
63
|
end
|
68
64
|
end
|
69
65
|
|
66
|
+
def set(**values)
|
67
|
+
values.each { |k, v| schema.hash[k.to_s] = v }
|
68
|
+
end
|
69
|
+
|
70
|
+
def definitions(&block)
|
71
|
+
schema.definitions.evaluate &block
|
72
|
+
end
|
73
|
+
|
74
|
+
def define(type, *args, &block)
|
75
|
+
definitions { __send__ type, *args, &block }
|
76
|
+
end
|
77
|
+
|
78
|
+
%i[title description default].each { |k| define_method(k) { |v| set k => v } }
|
79
|
+
|
80
|
+
def ref(*args, uri)
|
81
|
+
handler = SchemaCreation.handlers[self.class]
|
82
|
+
instance_exec(Reference.new(uri), *args, &handler) if handler
|
83
|
+
end
|
84
|
+
|
85
|
+
def link(rel_and_href, &block)
|
86
|
+
link = Link.new(self, *rel_and_href.first)
|
87
|
+
schema.links << link
|
88
|
+
link.dsl.evaluate &block if block
|
89
|
+
end
|
90
|
+
|
91
|
+
def method_missing(name, *args, &block)
|
92
|
+
if schema.definitions[name]
|
93
|
+
ref *args, "#/definitions/#{name}"
|
94
|
+
else
|
95
|
+
super
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def respond_to_missing?(name, *)
|
100
|
+
schema.definitions.key?(name) || super
|
101
|
+
end
|
102
|
+
|
70
103
|
private
|
71
104
|
|
72
105
|
# Minimize collisions with local scope (hence the weird name __args)
|
@@ -19,9 +19,9 @@ module Jimmy
|
|
19
19
|
hash.merge! camelize_attrs(%i[min_items max_items])
|
20
20
|
items = attrs[:items] || []
|
21
21
|
if items.length > 1
|
22
|
-
hash['items'] = {'anyOf' => items.map
|
22
|
+
hash['items'] = {'anyOf' => items.map(&:compile)}
|
23
23
|
elsif items.length == 1
|
24
|
-
hash['items'] =
|
24
|
+
hash['items'] = items.first.compile
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -32,7 +32,7 @@ module Jimmy
|
|
32
32
|
['properties', key.to_s]
|
33
33
|
end
|
34
34
|
hash[collection] ||= {}
|
35
|
-
hash[collection][key] =
|
35
|
+
hash[collection][key] = value.compile
|
36
36
|
end
|
37
37
|
hash['required'] = (attrs[:required] || []).to_a
|
38
38
|
hash['additionalProperties'] = !!attrs[:additional_properties]
|
data/lib/jimmy/version.rb
CHANGED
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.
|
4
|
+
version: '0.3'
|
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-
|
11
|
+
date: 2015-08-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json-schema
|
@@ -82,7 +82,10 @@ files:
|
|
82
82
|
- jimmy.gemspec
|
83
83
|
- lib/jimmy.rb
|
84
84
|
- lib/jimmy/combination.rb
|
85
|
+
- lib/jimmy/definitions.rb
|
85
86
|
- lib/jimmy/domain.rb
|
87
|
+
- lib/jimmy/link.rb
|
88
|
+
- lib/jimmy/reference.rb
|
86
89
|
- lib/jimmy/schema.rb
|
87
90
|
- lib/jimmy/schema_creation.rb
|
88
91
|
- lib/jimmy/schema_type.rb
|
@@ -95,6 +98,7 @@ files:
|
|
95
98
|
- lib/jimmy/schema_types/object.rb
|
96
99
|
- lib/jimmy/schema_types/string.rb
|
97
100
|
- lib/jimmy/symbol_array.rb
|
101
|
+
- lib/jimmy/type_reference.rb
|
98
102
|
- lib/jimmy/validation_error.rb
|
99
103
|
- lib/jimmy/version.rb
|
100
104
|
homepage: https://github.com/orionvm/jimmy
|