babl-json 0.3.1 → 0.3.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: 4f2f029a652582cac66cd36e290f5cda18388ed5
4
- data.tar.gz: 44aabe9259b3b4c91147578e2a0c4528a45f3248
3
+ metadata.gz: 1244f145154a35f410ff91f14de9dbacd4f679c9
4
+ data.tar.gz: fdc4821e46e72c54c248b33c406850421ec548e4
5
5
  SHA512:
6
- metadata.gz: 0d6199f5bd49ba5e467902e79ca254cf97dba01b21d92141ab451786c0fdba79a5701743f2c4f2766a9928924dd91bd8073c17f0d1542c83f3fbb047d852db31
7
- data.tar.gz: 25c6e56a337e7eb69bd085a3454ef1183dbe489e372f9970be1ec1d8591b0b2df959d6790d69f3ac269c4735444ab5022233d635fe58d9ad6ea5aa922c035173
6
+ metadata.gz: 7f1256e9d5a21ba6da758a890eba6e8063b94e2a3d9e26d45c55f262ebc9db95eacbef29f7a920ec67d55626e0a23d3c0f76bcd312d41d3f4de729fc742bd9b7
7
+ data.tar.gz: 47b8265e9078140b733cd81bd77baa1dcf40936362998dd8a85b7ad7a3b891a8ae1849f82be60c638fc6ccc3eb5a03d09b4eed137242575a453240d21e77c93b
@@ -65,12 +65,11 @@ module Babl
65
65
  obj1 = constant_to_object(obj1) if Constant === obj1
66
66
  obj2 = constant_to_object(obj2) if Constant === obj2
67
67
 
68
- if Object === obj1 && Object === obj2
69
- new_nodes = nodes.dup
70
- new_nodes[idx] = Object.new(obj1.nodes.merge(obj2.nodes))
71
- new_nodes[idx + 1] = nil
72
- return Merge.new(new_nodes.compact).optimize
73
- end
68
+ next unless Object === obj1 && Object === obj2
69
+ new_nodes = nodes.dup
70
+ new_nodes[idx] = Object.new(obj1.nodes.merge(obj2.nodes))
71
+ new_nodes[idx + 1] = nil
72
+ return Merge.new(new_nodes.compact).optimize
74
73
  end
75
74
  nil
76
75
  end
@@ -87,7 +86,7 @@ module Babl
87
86
  AS_OBJECT_MAPPING = {
88
87
  Schema::Anything.instance => Schema::Object::EMPTY_WITH_ADDITIONAL,
89
88
  Schema::Primitive::NULL => Schema::Object::EMPTY
90
- }
89
+ }.freeze
91
90
 
92
91
  # Merge two documentations together
93
92
  def merge_doc(doc1, doc2)
@@ -112,7 +111,17 @@ module Babl
112
111
  properties = (
113
112
  doc1.property_set.map { |property| doc2.additional ? anything_property(property) : property } +
114
113
  doc2.property_set.to_a
115
- ).each_with_object({}) { |property, acc| acc[property.name] = property }.values
114
+ ).each_with_object({}) { |property, acc|
115
+ if property.required
116
+ acc[property.name] = property
117
+ else
118
+ acc[property.name] = Schema::Object::Property.new(
119
+ property.name,
120
+ Schema::AnyOf.canonicalized([property.value, acc[property.name]&.value].compact),
121
+ acc[property.name]&.required || false
122
+ )
123
+ end
124
+ }.values
116
125
 
117
126
  Schema::Object.new(properties, additional)
118
127
  end
@@ -3,6 +3,7 @@ require 'babl/schema'
3
3
  require 'babl/errors'
4
4
  require 'babl/utils'
5
5
  require 'babl/nodes/constant'
6
+ require 'set'
6
7
 
7
8
  module Babl
8
9
  module Nodes
@@ -36,11 +37,22 @@ module Babl
36
37
  optimize_falsy_conditions ||
37
38
  optimize_truthy_conditions ||
38
39
  optimize_always_same_outputs ||
40
+ optimize_same_conditions ||
39
41
  self
40
42
  end
41
43
 
42
44
  private
43
45
 
46
+ def optimize_same_conditions
47
+ conds = Set.new
48
+ new_nodes = nodes.map { |cond, val|
49
+ next if conds.include?(cond)
50
+ conds << cond
51
+ [cond, val]
52
+ }.compact
53
+ new_nodes.size == nodes.size ? nil : Switch.new(new_nodes).optimize
54
+ end
55
+
44
56
  def optimize_always_same_outputs
45
57
  return unless nodes.map(&:first).any? { |node| Constant === node && node.value }
46
58
  return unless nodes.map(&:last).uniq.size == 1
@@ -6,7 +6,7 @@ require 'singleton'
6
6
 
7
7
  module Babl
8
8
  module Nodes
9
- # A TerminalValue node is always implicitely added to the end of the
9
+ # A TerminalValue node is always implicitly added to the end of the
10
10
  # chain during compilation. It basically ensures that the output contains only
11
11
  # primitives, arrays and hashes.
12
12
  class TerminalValue
@@ -71,8 +71,10 @@ module Babl
71
71
  case key
72
72
  when ::Symbol then key
73
73
  when ::String then key.to_sym
74
+ # rubocop:disable Lint/BooleanSymbol
74
75
  when ::TrueClass then :true
75
76
  when ::FalseClass then :false
77
+ # rubocop:enable Lint/BooleanSymbol
76
78
  when ::Numeric, ::NilClass then :"#{key}"
77
79
  else raise TerminalValueError.new("Invalid key for JSON object: #{key}", stack)
78
80
  end
@@ -11,7 +11,7 @@ module Babl
11
11
  def enter
12
12
  construct_node(key: nil, continue: nil) { |node, context|
13
13
  key = context[:key]
14
- raise Errors::InvalidTemplate, "No key to enter into" unless key
14
+ raise Errors::InvalidTemplate, 'No key to enter into' unless key
15
15
  Nodes::Nav.new(key, node)
16
16
  }
17
17
  end
@@ -8,7 +8,7 @@ module Babl
8
8
  module DSL
9
9
  # Create a JSON object node with static structure
10
10
  def object(*attrs, **nested)
11
- (attrs.map(&:to_sym) + nested.keys).group_by(&:itself).values.each do |keys|
11
+ (attrs.map(&:to_sym) + nested.keys).group_by(&:itself).each_value do |keys|
12
12
  raise Errors::InvalidTemplate, "Duplicate key in object(): #{keys.first}" if keys.size > 1
13
13
  end
14
14
 
@@ -8,7 +8,7 @@ module Babl
8
8
  # Load a partial template given its name
9
9
  # A 'lookup_context' must be defined
10
10
  def partial(partial_name)
11
- raise Errors::InvalidTemplate, "Cannot use partial without lookup context" unless lookup_context
11
+ raise Errors::InvalidTemplate, 'Cannot use partial without lookup context' unless lookup_context
12
12
 
13
13
  path, source, partial_lookup_context = lookup_context.find(partial_name)
14
14
  raise Errors::InvalidTemplate, "Cannot find partial '#{partial_name}'" unless path
data/lib/babl/railtie.rb CHANGED
@@ -42,7 +42,7 @@ module Babl
42
42
  end
43
43
 
44
44
  class Railtie < Rails::Railtie
45
- initializer "babl.initialize" do
45
+ initializer 'babl.initialize' do
46
46
  ActiveSupport.on_load(:action_view) do
47
47
  ::ActionView::Template.register_template_handler(:babl, Babl::ActionView::TemplateHandler)
48
48
  end
@@ -110,25 +110,19 @@ module Babl
110
110
  obj1props = obj1.property_set.map { |p| [p.name, p] }.to_h
111
111
  obj2props = obj2.property_set.map { |p| [p.name, p] }.to_h
112
112
 
113
- # Do not merge properties unless a keyset is almost a subset of the other
114
- next unless (obj1props.keys.to_set - obj2props.keys).size <= 1 ||
115
- (obj2props.keys.to_set - obj1props.keys).size <= 1
116
-
117
113
  # Try to detect a discrimitive property (inspired from Typescript's discriminative union),
118
- # We will abort the merging process unless all the other properties are exactly the same.
119
- discriminator = obj1props.find { |name, p1|
114
+ # We will abort the merging process if there is one
115
+ next if obj1props.any? { |name, p1|
120
116
  p2 = obj2props[name]
121
117
  next name if p2 && Primitive === p2.value &&
122
118
  Primitive === p1.value &&
123
119
  p1.value.value != p2.value.value
124
- }&.first
120
+ }
125
121
 
126
122
  new_properties = (obj1props.keys + obj2props.keys).uniq.map { |name|
127
123
  p1 = obj1props[name]
128
124
  p2 = obj2props[name]
129
125
 
130
- break if discriminator && discriminator != name && p1 != p2
131
-
132
126
  Object::Property.new(
133
127
  name,
134
128
  AnyOf.canonicalized([p1&.value, p2&.value].compact),
@@ -136,7 +130,6 @@ module Babl
136
130
  )
137
131
  }
138
132
 
139
- next unless new_properties
140
133
  new_obj = Object.new(new_properties, obj1.additional || obj2.additional)
141
134
  return AnyOf.canonicalized(choice_set - [obj1, obj2] + [new_obj])
142
135
  }
data/lib/babl/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Babl
3
- VERSION = '0.3.1'
3
+ VERSION = '0.3.2'
4
4
  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.3.1
4
+ version: 0.3.2
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-09-04 00:00:00.000000000 Z
11
+ date: 2017-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.48'
47
+ version: '0.50'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0.48'
54
+ version: '0.50'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: json-schema
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rabl
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.13'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.13'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: multi_json
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -199,7 +213,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
199
213
  version: '0'
200
214
  requirements: []
201
215
  rubyforge_project:
202
- rubygems_version: 2.6.11
216
+ rubygems_version: 2.6.13
203
217
  signing_key:
204
218
  specification_version: 4
205
219
  summary: JSON templating on steroids