jimmy 0.1.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -3
- data/lib/jimmy/schema.rb +8 -2
- data/lib/jimmy/schema_creation.rb +21 -5
- data/lib/jimmy/schema_type.rb +17 -11
- data/lib/jimmy/schema_types/number.rb +6 -2
- data/lib/jimmy/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bd8199acb7af5c492da8d005e07eac93636d5890
|
4
|
+
data.tar.gz: b318741c9a421485ce660f3e9c94b7682eccad89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1e02737e075e1251d217fe3c89a33242fc8fb766bd69f9d68f40b32dbdf590680a1796ca7ffa132500427b0fbba749943259bd30a4bd3c2df88a6f5e4ebcbf0
|
7
|
+
data.tar.gz: c3e3de0769a8931338a1d185675396e4776da762a8da6e091adf40b011219d47c71a0eeb1f4e3fc33126ca77d47ac3912ec953d7250ae2b675d92d12f2509348
|
data/README.md
CHANGED
@@ -77,7 +77,7 @@ object do
|
|
77
77
|
array :points_of_interest do
|
78
78
|
object do
|
79
79
|
string :title, 3..150
|
80
|
-
integer :popularity, 1
|
80
|
+
integer :popularity, [1, 3, 5, 7]
|
81
81
|
geopoint :location
|
82
82
|
boolean :featured
|
83
83
|
require :title
|
@@ -123,8 +123,7 @@ Here's the full result (though we expect you get the gist of it by now):
|
|
123
123
|
},
|
124
124
|
"popularity": {
|
125
125
|
"type": "integer",
|
126
|
-
"
|
127
|
-
"maximum": 5
|
126
|
+
"enum": [1, 3, 5, 7]
|
128
127
|
},
|
129
128
|
"location": {
|
130
129
|
"$ref": "/types/geopoint#"
|
data/lib/jimmy/schema.rb
CHANGED
@@ -50,7 +50,7 @@ module Jimmy
|
|
50
50
|
|
51
51
|
private
|
52
52
|
|
53
|
-
def initialize(type, domain, *args, &block)
|
53
|
+
def initialize(type, domain, locals, *args, &block)
|
54
54
|
@attrs = {}
|
55
55
|
@type = type
|
56
56
|
@domain = domain
|
@@ -67,7 +67,13 @@ module Jimmy
|
|
67
67
|
dsl.evaluate handler, arg
|
68
68
|
end
|
69
69
|
end
|
70
|
-
|
70
|
+
if block
|
71
|
+
if dsl.respond_to? :with_locals
|
72
|
+
dsl.with_locals(locals) { dsl.evaluate block }
|
73
|
+
else
|
74
|
+
dsl.evaluate block
|
75
|
+
end
|
76
|
+
end
|
71
77
|
end
|
72
78
|
|
73
79
|
end
|
@@ -12,27 +12,43 @@ module Jimmy
|
|
12
12
|
%i(one all any).each do |condition|
|
13
13
|
klass.__send__ :define_method, :"#{condition}_of" do |*args, &inner_block|
|
14
14
|
Combination.new(condition, domain).tap do |combo|
|
15
|
-
combo.evaluate inner_block
|
15
|
+
combo.with_locals(locals) { combo.evaluate inner_block }
|
16
16
|
instance_exec combo, *args, &handler
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
20
|
-
klass.include
|
20
|
+
klass.include MissingMethods
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
module
|
24
|
+
module MissingMethods
|
25
|
+
|
26
|
+
def locals
|
27
|
+
@locals ||= {}
|
28
|
+
end
|
29
|
+
|
30
|
+
def with_locals(**locals)
|
31
|
+
# TODO: validate locals
|
32
|
+
original = locals
|
33
|
+
@locals = original.merge(locals)
|
34
|
+
yield.tap { @locals = original }
|
35
|
+
end
|
25
36
|
|
26
37
|
def respond_to_missing?(method, *)
|
27
|
-
|
38
|
+
locals.key?(method) ||
|
39
|
+
SchemaTypes.key?(method) ||
|
40
|
+
domain.types.key?(method) ||
|
41
|
+
super
|
28
42
|
end
|
29
43
|
|
30
44
|
def method_missing(method, *args, &block)
|
45
|
+
return locals[method] if locals.key?(method)
|
46
|
+
|
31
47
|
if SchemaTypes.key? method
|
32
48
|
handler = SchemaCreation.handlers[self.class]
|
33
49
|
self.class.__send__ :define_method, method do |*inner_args, &inner_block|
|
34
50
|
handler_args = handler && inner_args.shift(handler.arity - 1)
|
35
|
-
schema = Schema.new(method, domain, *inner_args, &inner_block)
|
51
|
+
schema = Schema.new(method, domain, locals, *inner_args, &inner_block)
|
36
52
|
instance_exec schema, *handler_args, &handler if handler
|
37
53
|
schema.dsl
|
38
54
|
end
|
data/lib/jimmy/schema_type.rb
CHANGED
@@ -11,15 +11,17 @@ module Jimmy
|
|
11
11
|
SchemaTypes.register self
|
12
12
|
end
|
13
13
|
|
14
|
-
def trait(
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
14
|
+
def trait(*args, &handler)
|
15
|
+
args.each do |name_or_type|
|
16
|
+
case name_or_type
|
17
|
+
when Symbol
|
18
|
+
handler ||= proc { |value| attrs[name_or_type] = value }
|
19
|
+
self::DSL.__send__ :define_method, name_or_type, handler
|
20
|
+
when Class
|
21
|
+
Schema.set_argument_handler self, name_or_type, handler
|
22
|
+
else
|
23
|
+
raise 'Trait must be a Symbol or a Class'
|
24
|
+
end
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
@@ -57,8 +59,12 @@ module Jimmy
|
|
57
59
|
schema.is_a?(Symbol) ? {'$ref' => "/types/#{schema}.json#"} : schema.compile
|
58
60
|
end
|
59
61
|
|
60
|
-
def include(*partial_names)
|
61
|
-
partial_names.each
|
62
|
+
def include(*partial_names, **locals)
|
63
|
+
partial_names.each do |name|
|
64
|
+
with_locals locals do
|
65
|
+
instance_eval *domain.partials[name.to_s]
|
66
|
+
end
|
67
|
+
end
|
62
68
|
end
|
63
69
|
|
64
70
|
end
|
@@ -9,7 +9,11 @@ module Jimmy
|
|
9
9
|
trait(:<=) { |value| maximum value; attrs[:exclusive_maximum] = nil; self }
|
10
10
|
trait(:>) { |value| minimum value; attrs[:exclusive_minimum] = true; self }
|
11
11
|
trait(:>=) { |value| minimum value; attrs[:exclusive_minimum] = nil; self }
|
12
|
-
trait(
|
12
|
+
trait(:enum) do |*values|
|
13
|
+
attrs[:enum] ||= []
|
14
|
+
attrs[:enum] |= values.flatten
|
15
|
+
end
|
16
|
+
trait(Numeric, Array) { |value| enum value }
|
13
17
|
trait(Range) do |range|
|
14
18
|
if range.first <= range.last
|
15
19
|
minimum range.first
|
@@ -23,7 +27,7 @@ module Jimmy
|
|
23
27
|
end
|
24
28
|
|
25
29
|
compile do |hash|
|
26
|
-
hash.merge! camelize_attrs(%i[minimum maximum exclusive_minimum exclusive_maximum multiple_of])
|
30
|
+
hash.merge! camelize_attrs(%i[minimum maximum exclusive_minimum exclusive_maximum multiple_of enum])
|
27
31
|
end
|
28
32
|
|
29
33
|
end
|
data/lib/jimmy/version.rb
CHANGED