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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cc78f64835960e1b8d8724bee79adc324f710bb7
4
- data.tar.gz: 647dab7d9050d3ec4642b55a2cbc2211bfac1ab9
3
+ metadata.gz: bd8199acb7af5c492da8d005e07eac93636d5890
4
+ data.tar.gz: b318741c9a421485ce660f3e9c94b7682eccad89
5
5
  SHA512:
6
- metadata.gz: d8f71fc6b11b45bca65a0f599ef0cfa161d43d0c93e208fa47ec42b840c9584a76a7f358b4063e3901bcc51e65c3b8db948e86d3b336363ec87be098db6cdc26
7
- data.tar.gz: c11234863a1c7f3ccc8180c3e4f55b9d88844c648f34efa06aef0d30a31869aae7c21a345a1fcd0df7a0c3463cd71880f09863ce18cc4460dc35c0e0caafa46c
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..5
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
- "minimum": 1,
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
- dsl.evaluate block if block
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 ResolveMissingToCustomType
20
+ klass.include MissingMethods
21
21
  end
22
22
  end
23
23
 
24
- module ResolveMissingToCustomType
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
- SchemaTypes.key?(method) || domain.types.key?(method) || super
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
@@ -11,15 +11,17 @@ module Jimmy
11
11
  SchemaTypes.register self
12
12
  end
13
13
 
14
- def trait(name_or_type, &handler)
15
- case name_or_type
16
- when Symbol
17
- handler ||= proc { |value| attrs[name_or_type] = value }
18
- self::DSL.__send__ :define_method, name_or_type, handler
19
- when Class
20
- Schema.set_argument_handler self, name_or_type, handler
21
- else
22
- raise 'Trait must be a Symbol or a Class'
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 { |name| instance_eval *domain.partials[name.to_s] }
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(Numeric) { |value| minimum value; maximum value }
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
@@ -1,3 +1,3 @@
1
1
  module Jimmy
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2'
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.1.1
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil E. Pearson