babl-json 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +4 -2
- data/babl.gemspec +0 -1
- data/lib/babl/nodes/create_pin.rb +2 -2
- data/lib/babl/nodes/dep.rb +2 -2
- data/lib/babl/nodes/each.rb +2 -2
- data/lib/babl/nodes/fixed_array.rb +2 -2
- data/lib/babl/nodes/goto_pin.rb +2 -2
- data/lib/babl/nodes/merge.rb +4 -4
- data/lib/babl/nodes/nav.rb +2 -2
- data/lib/babl/nodes/object.rb +2 -2
- data/lib/babl/nodes/parent.rb +3 -3
- data/lib/babl/nodes/static.rb +2 -2
- data/lib/babl/nodes/switch.rb +3 -3
- data/lib/babl/nodes/typed.rb +10 -10
- data/lib/babl/nodes/with.rb +2 -2
- data/lib/babl/rendering/compiled_template.rb +2 -2
- data/lib/babl/schema/any_of.rb +52 -20
- data/lib/babl/schema/dyn_array.rb +2 -2
- data/lib/babl/schema/fixed_array.rb +2 -2
- data/lib/babl/schema/object.rb +3 -3
- data/lib/babl/schema/static.rb +4 -2
- data/lib/babl/schema/typed.rb +6 -6
- data/lib/babl/utils/value.rb +37 -0
- data/lib/babl/version.rb +1 -1
- data/logo-babl.png +0 -0
- data/spec/operators/switch_spec.rb +30 -1
- data/spec/spec_helper/schema_utils.rb +1 -1
- data/spec/utils/value_spec.rb +54 -0
- metadata +6 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de6e2596717bacec917c4ccb9b293676ba32f7b5
|
4
|
+
data.tar.gz: 2756b7fd36d5fdc9c661d0a1341f1353d6200260
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78ebc4868c41208e176f32637aa781c292ec6025612b988edb88a20ed9a640e250d942864789be496687651c19208df26335e5d2c9959307900c96a304886ea8
|
7
|
+
data.tar.gz: 6f1fb869d2345fabc61d52eb4065b5d2202e641afdd1d344b7218c6cdd70f1de4b3c3b919019b1fb8328e176c116c5614ae8aeff25b99b2d3305148f4b10a0b7
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# BABL Changelog
|
2
2
|
|
3
|
+
## 0.2.3 (July 21, 2017)
|
4
|
+
- Try to simplify JSON-Schema when types are specified.
|
5
|
+
- Cut out dependency on [Values](https://github.com/tcrayford/Values).
|
6
|
+
|
3
7
|
## 0.2.2 (July 13, 2017)
|
4
8
|
|
5
9
|
- Added four typing operators: `integer`, `number`, `string` and `boolean`.
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
1
|
+
![BABL Logo](https://github.com/getbannerman/babl/raw/master/logo-babl.png)
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/getbannerman/babl.svg?branch=master)](https://travis-ci.org/getbannerman/babl)
|
4
|
+
[![Gem](https://img.shields.io/gem/v/babl-json.svg)](https://rubygems.org/gems/babl-json)
|
5
|
+
[![Downloads](https://img.shields.io/gem/dt/babl-json.svg)](https://rubygems.org/gems/babl-json)
|
4
6
|
|
5
7
|
BABL (Bannerman API Builder Language) is a templating langage for generating JSON in APIs.
|
6
8
|
|
@@ -24,7 +26,7 @@ BABL template:
|
|
24
26
|
```ruby
|
25
27
|
object(
|
26
28
|
document: object(
|
27
|
-
:id, :title
|
29
|
+
:id, :title,
|
28
30
|
|
29
31
|
owner: _.nullable.object(:id, :name),
|
30
32
|
authors: _.each.object(:id, :name),
|
data/babl.gemspec
CHANGED
data/lib/babl/nodes/dep.rb
CHANGED
data/lib/babl/nodes/each.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'babl/schema/dyn_array'
|
2
2
|
require 'babl/errors'
|
3
|
-
require '
|
3
|
+
require 'babl/utils/value'
|
4
4
|
|
5
5
|
module Babl
|
6
6
|
module Nodes
|
7
|
-
class Each < ::Value.new(:node)
|
7
|
+
class Each < Utils::Value.new(:node)
|
8
8
|
def dependencies
|
9
9
|
{ __each__: node.dependencies }
|
10
10
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'babl/utils/hash'
|
2
2
|
require 'babl/schema/fixed_array'
|
3
|
-
require '
|
3
|
+
require 'babl/utils/value'
|
4
4
|
|
5
5
|
module Babl
|
6
6
|
module Nodes
|
7
|
-
class FixedArray < ::Value.new(:nodes)
|
7
|
+
class FixedArray < Utils::Value.new(:nodes)
|
8
8
|
def schema
|
9
9
|
Schema::FixedArray.new(nodes.map(&:schema))
|
10
10
|
end
|
data/lib/babl/nodes/goto_pin.rb
CHANGED
data/lib/babl/nodes/merge.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'babl/utils/hash'
|
2
2
|
require 'babl/errors'
|
3
|
-
require '
|
3
|
+
require 'babl/utils/value'
|
4
4
|
|
5
5
|
module Babl
|
6
6
|
module Nodes
|
7
|
-
class Merge < ::Value.new(:nodes)
|
7
|
+
class Merge < Utils::Value.new(:nodes)
|
8
8
|
def dependencies
|
9
9
|
nodes.map(&:dependencies).reduce({}) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
|
10
10
|
end
|
@@ -64,7 +64,7 @@ module Babl
|
|
64
64
|
.map do |name, properties|
|
65
65
|
Schema::Object::Property.new(
|
66
66
|
name,
|
67
|
-
Schema::AnyOf.
|
67
|
+
Schema::AnyOf.canonicalized(properties.map(&:value)),
|
68
68
|
properties.size == all_docs.size && properties.all?(&:required)
|
69
69
|
)
|
70
70
|
end
|
@@ -89,7 +89,7 @@ module Babl
|
|
89
89
|
def allow_anything(property)
|
90
90
|
Schema::Object::Property.new(
|
91
91
|
property.name,
|
92
|
-
Schema::AnyOf.
|
92
|
+
Schema::AnyOf.canonicalized([property.value, Schema::Anything.instance]),
|
93
93
|
property.required
|
94
94
|
)
|
95
95
|
end
|
data/lib/babl/nodes/nav.rb
CHANGED
data/lib/babl/nodes/object.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'babl/utils/hash'
|
2
2
|
require 'babl/schema/object'
|
3
|
-
require '
|
3
|
+
require 'babl/utils/value'
|
4
4
|
|
5
5
|
module Babl
|
6
6
|
module Nodes
|
7
|
-
class Object < ::Value.new(:nodes)
|
7
|
+
class Object < Utils::Value.new(:nodes)
|
8
8
|
def dependencies
|
9
9
|
nodes.values.map(&:dependencies).reduce({}) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
|
10
10
|
end
|
data/lib/babl/nodes/parent.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'babl/utils/ref'
|
2
2
|
require 'babl/errors'
|
3
|
-
require '
|
3
|
+
require 'babl/utils/value'
|
4
4
|
|
5
5
|
module Babl
|
6
6
|
module Nodes
|
7
|
-
class Parent < ::Value.new(:node)
|
7
|
+
class Parent < Utils::Value.new(:node)
|
8
8
|
PARENT_MARKER = Utils::Ref.new
|
9
9
|
|
10
|
-
class Resolver < ::Value.new(:node)
|
10
|
+
class Resolver < Utils::Value.new(:node)
|
11
11
|
def dependencies
|
12
12
|
backpropagate_dependencies(node.dependencies)
|
13
13
|
end
|
data/lib/babl/nodes/static.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'babl/nodes/terminal_value'
|
2
2
|
require 'babl/schema/static'
|
3
|
-
require '
|
3
|
+
require 'babl/utils/value'
|
4
4
|
|
5
5
|
module Babl
|
6
6
|
module Nodes
|
7
|
-
class Static < ::Value.new(:value)
|
7
|
+
class Static < Utils::Value.new(:value)
|
8
8
|
def schema
|
9
9
|
Schema::Static.new(value)
|
10
10
|
end
|
data/lib/babl/nodes/switch.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'babl/utils/hash'
|
2
2
|
require 'babl/schema/any_of'
|
3
3
|
require 'babl/errors'
|
4
|
-
require '
|
4
|
+
require 'babl/utils/value'
|
5
5
|
|
6
6
|
module Babl
|
7
7
|
module Nodes
|
8
|
-
class Switch < ::Value.new(:nodes)
|
8
|
+
class Switch < Utils::Value.new(:nodes)
|
9
9
|
def dependencies
|
10
10
|
(nodes.values + nodes.keys).map(&:dependencies)
|
11
11
|
.reduce({}) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
|
@@ -17,7 +17,7 @@ module Babl
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def schema
|
20
|
-
Schema::AnyOf.
|
20
|
+
Schema::AnyOf.canonicalized(nodes.values.map(&:schema))
|
21
21
|
end
|
22
22
|
|
23
23
|
def render(ctx)
|
data/lib/babl/nodes/typed.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
|
-
require '
|
1
|
+
require 'babl/utils/value'
|
2
2
|
require 'babl/schema/typed'
|
3
3
|
|
4
4
|
module Babl
|
5
5
|
module Nodes
|
6
|
-
class Typed < ::Value.new(:
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
class Typed < Utils::Value.new(:schema, :node)
|
7
|
+
CURRIED = method(:new).curry(2)
|
8
|
+
|
9
|
+
BOOLEAN = CURRIED.call(Schema::Typed::BOOLEAN)
|
10
|
+
INTEGER = CURRIED.call(Schema::Typed::INTEGER)
|
11
|
+
NUMBER = CURRIED.call(Schema::Typed::NUMBER)
|
12
|
+
STRING = CURRIED.call(Schema::Typed::STRING)
|
11
13
|
|
12
14
|
def dependencies
|
13
15
|
node.dependencies
|
@@ -19,10 +21,8 @@ module Babl
|
|
19
21
|
|
20
22
|
def render(ctx)
|
21
23
|
value = node.render(ctx)
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
value
|
24
|
+
return value if schema.classes.any? { |clazz| clazz === value }
|
25
|
+
raise Errors::RenderingError, "Expected type '#{schema.type}': #{value}\n#{ctx.formatted_stack}"
|
26
26
|
end
|
27
27
|
|
28
28
|
private
|
data/lib/babl/nodes/with.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'oj'
|
2
2
|
require 'babl/rendering/context'
|
3
|
-
require '
|
3
|
+
require 'babl/utils/value'
|
4
4
|
|
5
5
|
module Babl
|
6
6
|
module Rendering
|
7
|
-
class CompiledTemplate < ::Value.new(:node, :dependencies, :preloader, :pretty, :json_schema)
|
7
|
+
class CompiledTemplate < Utils::Value.new(:node, :dependencies, :preloader, :pretty, :json_schema)
|
8
8
|
def json(root)
|
9
9
|
data = render(root)
|
10
10
|
::Oj.dump(data, indent: pretty ? 4 : 0, mode: :strict)
|
data/lib/babl/schema/any_of.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
require '
|
1
|
+
require 'babl/utils/value'
|
2
2
|
require 'set'
|
3
3
|
require 'babl/schema/static'
|
4
4
|
require 'babl/schema/object'
|
5
5
|
|
6
6
|
module Babl
|
7
7
|
module Schema
|
8
|
-
class AnyOf < ::Value.new(:choice_set)
|
8
|
+
class AnyOf < Utils::Value.new(:choice_set)
|
9
9
|
attr_reader :choices
|
10
10
|
|
11
11
|
def initialize(choices)
|
@@ -22,6 +22,8 @@ module Babl
|
|
22
22
|
# schema.
|
23
23
|
def simplify
|
24
24
|
simplify_single ||
|
25
|
+
simplify_boolean ||
|
26
|
+
simplify_typed_and_static ||
|
25
27
|
simplify_nullability ||
|
26
28
|
simplify_empty_array ||
|
27
29
|
simplify_push_down_dyn_array ||
|
@@ -30,7 +32,7 @@ module Babl
|
|
30
32
|
self
|
31
33
|
end
|
32
34
|
|
33
|
-
def self.
|
35
|
+
def self.canonicalized(choices)
|
34
36
|
new(choices).simplify
|
35
37
|
end
|
36
38
|
|
@@ -41,20 +43,44 @@ module Babl
|
|
41
43
|
choices.size == 1 ? choices.first : nil
|
42
44
|
end
|
43
45
|
|
44
|
-
#
|
46
|
+
# Anything is nullable, so AnyOf[null, Anything] can be replaced by Anything
|
47
|
+
# (in general, everything can be merged into Anything but I decided not to do it in order
|
48
|
+
# to retain as much information as possible)
|
45
49
|
def simplify_nullability
|
46
|
-
|
50
|
+
return unless choice_set.include?(Static::NULL) && choice_set.include?(Anything.instance)
|
51
|
+
AnyOf.canonicalized(choice_set - [Static::NULL])
|
52
|
+
end
|
53
|
+
|
54
|
+
# AnyOf[true, false] is just boolean
|
55
|
+
def simplify_boolean
|
56
|
+
return unless choice_set.include?(Static::TRUE) && choice_set.include?(Static::FALSE)
|
57
|
+
AnyOf.canonicalized(choice_set - [Static::TRUE, Static::FALSE] + [Typed::BOOLEAN])
|
58
|
+
end
|
59
|
+
|
60
|
+
# AnyOf[string, 'a string instance', 'another string'] is just string
|
61
|
+
# AnyOf[boolean, true, false] is just boolean
|
62
|
+
# AnyOf[number, 2, 3.1] is just number
|
63
|
+
# AnyOf[integer, 2, 1] is just integer
|
64
|
+
def simplify_typed_and_static
|
65
|
+
choices.each do |typed|
|
66
|
+
next unless Typed === typed
|
67
|
+
instances = choices.select { |instance|
|
68
|
+
Static === instance && typed.classes.any? { |clazz| clazz === instance.value }
|
69
|
+
}
|
70
|
+
next if instances.empty?
|
71
|
+
return AnyOf.canonicalized(choice_set - instances)
|
72
|
+
end
|
73
|
+
nil
|
47
74
|
end
|
48
75
|
|
49
76
|
# An always empty FixedArray is just a special case of a DynArray
|
50
77
|
# We can get rid of the former and only keep the DynArray
|
51
78
|
def simplify_empty_array
|
52
|
-
return unless
|
53
|
-
|
54
|
-
others.each do |other|
|
79
|
+
return unless choice_set.include?(FixedArray::EMPTY)
|
80
|
+
choice_set.each do |other|
|
55
81
|
next unless DynArray === other
|
56
82
|
new_other = DynArray.new(other.item)
|
57
|
-
return AnyOf.
|
83
|
+
return AnyOf.canonicalized(choice_set - [other, FixedArray::EMPTY] + [new_other])
|
58
84
|
end
|
59
85
|
nil
|
60
86
|
end
|
@@ -63,12 +89,13 @@ module Babl
|
|
63
89
|
# removed.
|
64
90
|
def simplify_dyn_and_fixed_array
|
65
91
|
fixed_arrays = choices.select { |s| FixedArray === s && s.items.uniq.size == 1 }
|
92
|
+
return if fixed_arrays.empty?
|
66
93
|
|
67
94
|
choices.each do |dyn|
|
68
95
|
next unless DynArray === dyn
|
69
96
|
fixed_arrays.each do |fixed|
|
70
97
|
new_dyn = DynArray.new(dyn.item)
|
71
|
-
return AnyOf.
|
98
|
+
return AnyOf.canonicalized(choice_set - [fixed, dyn] + [new_dyn]) if dyn.item == fixed.items.first
|
72
99
|
end
|
73
100
|
end
|
74
101
|
|
@@ -79,11 +106,15 @@ module Babl
|
|
79
106
|
# having a different type, then AnyOf can be pushed down to this property.
|
80
107
|
def simplify_many_objects_only_one_difference
|
81
108
|
choices.each_with_index { |obj1, index1|
|
109
|
+
next unless Object === obj1
|
110
|
+
|
82
111
|
choices.each_with_index { |obj2, index2|
|
83
|
-
|
84
|
-
|
85
|
-
next unless
|
86
|
-
|
112
|
+
break if index2 >= index1
|
113
|
+
|
114
|
+
next unless Object === obj2 &&
|
115
|
+
obj1.additional == obj2.additional &&
|
116
|
+
obj1.properties.map { |p| [p.name, p.required] }.to_set ==
|
117
|
+
obj2.properties.map { |p| [p.name, p.required] }.to_set
|
87
118
|
|
88
119
|
diff1 = obj1.properties - obj2.properties
|
89
120
|
diff2 = obj2.properties - obj1.properties
|
@@ -95,14 +126,14 @@ module Babl
|
|
95
126
|
next property unless property == diff1.first
|
96
127
|
Object::Property.new(
|
97
128
|
property.name,
|
98
|
-
AnyOf.
|
129
|
+
AnyOf.canonicalized([diff1.first.value, diff2.first.value]),
|
99
130
|
property.required
|
100
131
|
)
|
101
132
|
},
|
102
133
|
obj1.additional
|
103
134
|
)
|
104
135
|
|
105
|
-
return AnyOf.
|
136
|
+
return AnyOf.canonicalized(choice_set - [obj1, obj2] + [merged])
|
106
137
|
}
|
107
138
|
}
|
108
139
|
|
@@ -112,11 +143,12 @@ module Babl
|
|
112
143
|
# Push down the AnyOf to the item if all outputs are of type DynArray
|
113
144
|
def simplify_push_down_dyn_array
|
114
145
|
choices.each_with_index { |arr1, index1|
|
146
|
+
next unless DynArray === arr1
|
115
147
|
choices.each_with_index { |arr2, index2|
|
116
|
-
|
117
|
-
next unless DynArray ===
|
118
|
-
new_arr = DynArray.new(AnyOf.
|
119
|
-
return AnyOf.
|
148
|
+
break if index2 >= index1
|
149
|
+
next unless DynArray === arr2
|
150
|
+
new_arr = DynArray.new(AnyOf.canonicalized([arr1.item, arr2.item]))
|
151
|
+
return AnyOf.canonicalized(choice_set - [arr1, arr2] + [new_arr])
|
120
152
|
}
|
121
153
|
}
|
122
154
|
nil
|
data/lib/babl/schema/object.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
require '
|
1
|
+
require 'babl/utils/value'
|
2
2
|
require 'set'
|
3
3
|
|
4
4
|
module Babl
|
5
5
|
module Schema
|
6
|
-
class Object < ::Value.new(:property_set, :additional)
|
6
|
+
class Object < Utils::Value.new(:property_set, :additional)
|
7
7
|
attr_reader :properties
|
8
8
|
|
9
9
|
def initialize(properties, additional)
|
@@ -14,7 +14,7 @@ module Babl
|
|
14
14
|
EMPTY = new([], false)
|
15
15
|
EMPTY_WITH_ADDITIONAL = new([], true)
|
16
16
|
|
17
|
-
class Property < ::Value.new(:name, :value, :required)
|
17
|
+
class Property < Utils::Value.new(:name, :value, :required)
|
18
18
|
def initialize(name, value, required)
|
19
19
|
super(name, value, required)
|
20
20
|
end
|
data/lib/babl/schema/static.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
-
require '
|
1
|
+
require 'babl/utils/value'
|
2
2
|
|
3
3
|
module Babl
|
4
4
|
module Schema
|
5
|
-
class Static < ::Value.new(:value)
|
5
|
+
class Static < Utils::Value.new(:value)
|
6
6
|
NULL = new(nil)
|
7
|
+
TRUE = new(true)
|
8
|
+
FALSE = new(false)
|
7
9
|
|
8
10
|
def json
|
9
11
|
return { type: 'null' } if value.nil?
|
data/lib/babl/schema/typed.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
require '
|
1
|
+
require 'babl/utils/value'
|
2
2
|
|
3
3
|
module Babl
|
4
4
|
module Schema
|
5
|
-
class Typed < ::Value.new(:type)
|
6
|
-
INTEGER = new('integer')
|
7
|
-
BOOLEAN = new('boolean')
|
8
|
-
NUMBER = new('number')
|
9
|
-
STRING = new('string')
|
5
|
+
class Typed < Utils::Value.new(:type, :classes)
|
6
|
+
INTEGER = new('integer', [::Integer])
|
7
|
+
BOOLEAN = new('boolean', [::TrueClass, ::FalseClass])
|
8
|
+
NUMBER = new('number', [::Numeric])
|
9
|
+
STRING = new('string', [::String])
|
10
10
|
|
11
11
|
def json
|
12
12
|
{ type: type }
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Babl
|
2
|
+
module Utils
|
3
|
+
# Construct deeply immutable value objects
|
4
|
+
# Similar to Struct, but:
|
5
|
+
# - Properties are assumed deeply immutable (#hash is assumed constant)
|
6
|
+
# - Constructor requires all arguments
|
7
|
+
# - #== has the same meaning as #eql?
|
8
|
+
class Value
|
9
|
+
def self.new(*fields)
|
10
|
+
::Class.new(::Struct.new(:_cached_hash, *fields)) do
|
11
|
+
field_aliases = ::Array.new(fields.size) { |i| "v#{i}" }
|
12
|
+
const_set(:FIELDS, fields.map(&:to_sym))
|
13
|
+
class_eval <<-RUBY
|
14
|
+
def initialize(#{field_aliases.join(',')})
|
15
|
+
super(#{['nil', field_aliases].join(',')})
|
16
|
+
hash
|
17
|
+
freeze
|
18
|
+
end
|
19
|
+
|
20
|
+
def hash
|
21
|
+
self._cached_hash ||= super
|
22
|
+
end
|
23
|
+
|
24
|
+
def ==(other)
|
25
|
+
eql?(other)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.with(hash = {})
|
29
|
+
raise ::ArgumentError unless ::Hash === hash && (hash.keys - FIELDS).empty?
|
30
|
+
new(*FIELDS.map { |f| hash.fetch(f) })
|
31
|
+
end
|
32
|
+
RUBY
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/babl/version.rb
CHANGED
data/logo-babl.png
ADDED
Binary file
|
@@ -113,9 +113,38 @@ describe Babl::Operators::Switch do
|
|
113
113
|
it { expect(schema).to eq s_any_of(s_null, s_dyn_array(s_any_of(s_static('a'), s_static('c'), s_static('b')))) }
|
114
114
|
end
|
115
115
|
|
116
|
+
context 'switch between true and false' do
|
117
|
+
template { switch(1 => true, 2 => false) }
|
118
|
+
it { expect(schema).to eq s_boolean }
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'switch between true and a string' do
|
122
|
+
template { switch(1 => true, 3 => string) }
|
123
|
+
it { expect(schema).to eq s_any_of(s_static(true), s_string) }
|
124
|
+
end
|
125
|
+
|
126
|
+
context 'switch between any string and a specific string' do
|
127
|
+
template { switch(1 => string, 3 => 'lol') }
|
128
|
+
it { expect(schema).to eq s_string }
|
129
|
+
end
|
130
|
+
|
131
|
+
context 'switch between any boolean and a specific boolean' do
|
132
|
+
template { switch(1 => boolean, 3 => true) }
|
133
|
+
it { expect(schema).to eq s_boolean }
|
134
|
+
end
|
135
|
+
|
136
|
+
context 'switch between a specific float, specific integer and any number' do
|
137
|
+
template { switch(1 => 1.2, 2 => 2, 3 => number) }
|
138
|
+
it { expect(schema).to eq s_number }
|
139
|
+
end
|
140
|
+
|
141
|
+
context 'switch between a specific float, specific integer and any integer' do
|
142
|
+
template { switch(1 => 1.2, 2 => 2, 3 => integer) }
|
143
|
+
it { expect(schema).to eq s_any_of(s_integer, s_static(1.2)) }
|
144
|
+
end
|
145
|
+
|
116
146
|
context 'with dependencies' do
|
117
147
|
template { nav(:test).switch(nav(:keke) => parent.nav(:lol)) }
|
118
|
-
|
119
148
|
it { expect(dependencies).to eq(test: { keke: {} }, lol: {}) }
|
120
149
|
end
|
121
150
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'babl/utils/value'
|
2
|
+
|
3
|
+
describe Babl::Utils::Value do
|
4
|
+
let(:clazz) { described_class.new(:val1) }
|
5
|
+
let(:inst1) { clazz.new([1]) }
|
6
|
+
let(:inst2) { clazz.new([1]) }
|
7
|
+
|
8
|
+
it { expect(inst1).to eq inst2 }
|
9
|
+
it { expect(inst1).to eql inst2 }
|
10
|
+
it { expect(inst1.hash).to eq inst2.hash }
|
11
|
+
|
12
|
+
describe 'immutability' do
|
13
|
+
it { expect { inst1.val1 = 2 }.to raise_error ::RuntimeError }
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'test when not equal' do
|
17
|
+
before { inst2.val1 << 3 }
|
18
|
+
|
19
|
+
it { expect(inst1).not_to eq inst2 }
|
20
|
+
it { expect(inst1.hash).to eq inst2.hash }
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'use hash for equality test' do
|
24
|
+
let(:inst1dup) { inst1.dup }
|
25
|
+
|
26
|
+
it { expect(inst1dup).to eq inst1 }
|
27
|
+
|
28
|
+
context do
|
29
|
+
before { inst1dup._cached_hash = 42 }
|
30
|
+
it { expect(inst1dup).not_to eq inst1 }
|
31
|
+
it { expect(inst1dup).not_to eq inst2 }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'ensure eql? behavior' do
|
36
|
+
let(:inst1) { clazz.new(1.0) }
|
37
|
+
let(:inst2) { clazz.new(1) }
|
38
|
+
|
39
|
+
it { expect(inst1).not_to eql inst2 }
|
40
|
+
it { expect(inst1).not_to eq inst2 }
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'no property' do
|
44
|
+
let(:clazz1) { described_class.new }
|
45
|
+
let(:clazz2) { described_class.new }
|
46
|
+
|
47
|
+
let(:clazz1_inst1) { clazz1.new }
|
48
|
+
let(:clazz1_inst2) { clazz1.new }
|
49
|
+
let(:clazz2_inst1) { clazz2.new }
|
50
|
+
|
51
|
+
it { expect(clazz1_inst1).to eq clazz1_inst2 }
|
52
|
+
it { expect(clazz1_inst1).not_to eq clazz2_inst1 }
|
53
|
+
end
|
54
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: babl-json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frederic Terrazzoni
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-07-
|
11
|
+
date: 2017-07-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -80,20 +80,6 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '3.0'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: values
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - "~>"
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '1.8'
|
90
|
-
type: :runtime
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - "~>"
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '1.8'
|
97
83
|
description: JSON templating on steroids
|
98
84
|
email:
|
99
85
|
- frederic.terrazzoni@gmail.com
|
@@ -164,7 +150,9 @@ files:
|
|
164
150
|
- lib/babl/template.rb
|
165
151
|
- lib/babl/utils/hash.rb
|
166
152
|
- lib/babl/utils/ref.rb
|
153
|
+
- lib/babl/utils/value.rb
|
167
154
|
- lib/babl/version.rb
|
155
|
+
- logo-babl.png
|
168
156
|
- spec/operators/array_spec.rb
|
169
157
|
- spec/operators/call_spec.rb
|
170
158
|
- spec/operators/continue_spec.rb
|
@@ -189,6 +177,7 @@ files:
|
|
189
177
|
- spec/spec_helper.rb
|
190
178
|
- spec/spec_helper/operator_testing.rb
|
191
179
|
- spec/spec_helper/schema_utils.rb
|
180
|
+
- spec/utils/value_spec.rb
|
192
181
|
homepage: https://github.com/getbannerman/babl
|
193
182
|
licenses:
|
194
183
|
- MIT
|
@@ -238,3 +227,4 @@ test_files:
|
|
238
227
|
- spec/spec_helper.rb
|
239
228
|
- spec/spec_helper/operator_testing.rb
|
240
229
|
- spec/spec_helper/schema_utils.rb
|
230
|
+
- spec/utils/value_spec.rb
|