babl-json 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/lib/babl.rb +2 -2
  3. data/lib/babl/builder.rb +2 -0
  4. data/lib/babl/builder/chain_builder.rb +1 -2
  5. data/lib/babl/builder/template_base.rb +3 -4
  6. data/lib/babl/nodes.rb +15 -0
  7. data/lib/babl/nodes/create_pin.rb +1 -2
  8. data/lib/babl/nodes/dep.rb +1 -2
  9. data/lib/babl/nodes/each.rb +2 -2
  10. data/lib/babl/nodes/fixed_array.rb +2 -3
  11. data/lib/babl/nodes/goto_pin.rb +1 -2
  12. data/lib/babl/nodes/internal_value.rb +1 -1
  13. data/lib/babl/nodes/merge.rb +14 -35
  14. data/lib/babl/nodes/nav.rb +1 -1
  15. data/lib/babl/nodes/object.rb +5 -4
  16. data/lib/babl/nodes/parent.rb +1 -2
  17. data/lib/babl/nodes/static.rb +2 -3
  18. data/lib/babl/nodes/switch.rb +2 -3
  19. data/lib/babl/nodes/terminal_value.rb +17 -12
  20. data/lib/babl/nodes/typed.rb +2 -2
  21. data/lib/babl/nodes/with.rb +1 -2
  22. data/lib/babl/operators.rb +21 -0
  23. data/lib/babl/operators/array.rb +1 -2
  24. data/lib/babl/operators/dep.rb +1 -1
  25. data/lib/babl/operators/each.rb +1 -1
  26. data/lib/babl/operators/enter.rb +1 -0
  27. data/lib/babl/operators/merge.rb +1 -2
  28. data/lib/babl/operators/nav.rb +1 -1
  29. data/lib/babl/operators/object.rb +1 -2
  30. data/lib/babl/operators/parent.rb +1 -1
  31. data/lib/babl/operators/partial.rb +1 -1
  32. data/lib/babl/operators/pin.rb +2 -3
  33. data/lib/babl/operators/static.rb +3 -3
  34. data/lib/babl/operators/switch.rb +1 -3
  35. data/lib/babl/operators/typed.rb +1 -1
  36. data/lib/babl/operators/with.rb +1 -2
  37. data/lib/babl/rendering.rb +3 -0
  38. data/lib/babl/rendering/compiled_template.rb +2 -2
  39. data/lib/babl/schema.rb +7 -0
  40. data/lib/babl/schema/any_of.rb +43 -38
  41. data/lib/babl/schema/dyn_array.rb +1 -1
  42. data/lib/babl/schema/fixed_array.rb +1 -1
  43. data/lib/babl/schema/object.rb +1 -1
  44. data/lib/babl/schema/static.rb +1 -1
  45. data/lib/babl/schema/typed.rb +1 -1
  46. data/lib/babl/template.rb +2 -23
  47. data/lib/babl/utils.rb +3 -0
  48. data/lib/babl/version.rb +1 -1
  49. metadata +9 -63
  50. data/.gitignore +0 -3
  51. data/.rubocop.yml +0 -201
  52. data/.ruby-version +0 -1
  53. data/.travis.yml +0 -4
  54. data/CHANGELOG.md +0 -36
  55. data/Gemfile +0 -5
  56. data/LICENSE +0 -7
  57. data/README.md +0 -89
  58. data/babl.gemspec +0 -24
  59. data/logo-babl.png +0 -0
  60. data/spec/operators/array_spec.rb +0 -37
  61. data/spec/operators/call_spec.rb +0 -64
  62. data/spec/operators/continue_spec.rb +0 -25
  63. data/spec/operators/default_spec.rb +0 -15
  64. data/spec/operators/dep_spec.rb +0 -15
  65. data/spec/operators/each_spec.rb +0 -42
  66. data/spec/operators/enter_spec.rb +0 -28
  67. data/spec/operators/extends_spec.rb +0 -30
  68. data/spec/operators/merge_spec.rb +0 -124
  69. data/spec/operators/nav_spec.rb +0 -71
  70. data/spec/operators/null_spec.rb +0 -15
  71. data/spec/operators/nullable_spec.rb +0 -29
  72. data/spec/operators/object_spec.rb +0 -30
  73. data/spec/operators/parent_spec.rb +0 -50
  74. data/spec/operators/partial_spec.rb +0 -36
  75. data/spec/operators/pin_spec.rb +0 -172
  76. data/spec/operators/source_spec.rb +0 -33
  77. data/spec/operators/static_spec.rb +0 -27
  78. data/spec/operators/switch_spec.rb +0 -151
  79. data/spec/operators/typed_spec.rb +0 -84
  80. data/spec/operators/with_spec.rb +0 -31
  81. data/spec/spec_helper.rb +0 -2
  82. data/spec/spec_helper/operator_testing.rb +0 -46
  83. data/spec/spec_helper/schema_utils.rb +0 -53
  84. data/spec/utils/value_spec.rb +0 -54
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de6e2596717bacec917c4ccb9b293676ba32f7b5
4
- data.tar.gz: 2756b7fd36d5fdc9c661d0a1341f1353d6200260
3
+ metadata.gz: 89d572b099ed6dd5b4f7a8d330d62b82f2c614a9
4
+ data.tar.gz: a5a54bfc4ea3d28cba05a5285f401faa5708546c
5
5
  SHA512:
6
- metadata.gz: 78ebc4868c41208e176f32637aa781c292ec6025612b988edb88a20ed9a640e250d942864789be496687651c19208df26335e5d2c9959307900c96a304886ea8
7
- data.tar.gz: 6f1fb869d2345fabc61d52eb4065b5d2202e641afdd1d344b7218c6cdd70f1de4b3c3b919019b1fb8328e176c116c5614ae8aeff25b99b2d3305148f4b10a0b7
6
+ metadata.gz: 80a16645c833210146dd440920895f9d0a53bbea5a06b213ec5c50d396dcf059f3378bbb5da6b23771a035e99abe833c55125a731f92b7d22664216e1883105e
7
+ data.tar.gz: 14926b91b5690491bd9ae7d69123950167bfe823bda592ad6ac72e9dccf009dee817ad74f6176cd7bdee85f343bee10928059a804b65ea4701c547d241683c89
@@ -1,8 +1,8 @@
1
1
  require 'babl/railtie' if defined?(Rails)
2
2
  require 'babl/template'
3
3
  require 'babl/version'
4
- require 'babl/rendering/noop_preloader'
5
- require 'babl/operators/partial'
4
+ require 'babl/rendering'
5
+ require 'babl/operators'
6
6
 
7
7
  module Babl
8
8
  class Config
@@ -0,0 +1,2 @@
1
+ require 'babl/builder/chain_builder'
2
+ require 'babl/builder/template_base'
@@ -1,5 +1,4 @@
1
- require 'babl/nodes/internal_value'
2
- require 'babl/nodes/terminal_value'
1
+ require 'babl/nodes'
3
2
  require 'babl/errors'
4
3
 
5
4
  module Babl
@@ -1,7 +1,6 @@
1
- require 'babl/nodes/terminal_value'
2
- require 'babl/builder/chain_builder'
3
- require 'babl/rendering/compiled_template'
4
- require 'babl/rendering/noop_preloader'
1
+ require 'babl/nodes'
2
+ require 'babl/builder'
3
+ require 'babl/rendering'
5
4
 
6
5
  module Babl
7
6
  module Builder
@@ -0,0 +1,15 @@
1
+ require 'babl/nodes/create_pin'
2
+ require 'babl/nodes/dep'
3
+ require 'babl/nodes/each'
4
+ require 'babl/nodes/fixed_array'
5
+ require 'babl/nodes/goto_pin'
6
+ require 'babl/nodes/internal_value'
7
+ require 'babl/nodes/merge'
8
+ require 'babl/nodes/nav'
9
+ require 'babl/nodes/object'
10
+ require 'babl/nodes/parent'
11
+ require 'babl/nodes/static'
12
+ require 'babl/nodes/switch'
13
+ require 'babl/nodes/terminal_value'
14
+ require 'babl/nodes/typed'
15
+ require 'babl/nodes/with'
@@ -1,5 +1,4 @@
1
- require 'babl/utils/hash'
2
- require 'babl/utils/value'
1
+ require 'babl/utils'
3
2
 
4
3
  module Babl
5
4
  module Nodes
@@ -1,5 +1,4 @@
1
- require 'babl/utils/hash'
2
- require 'babl/utils/value'
1
+ require 'babl/utils'
3
2
 
4
3
  module Babl
5
4
  module Nodes
@@ -1,6 +1,6 @@
1
- require 'babl/schema/dyn_array'
1
+ require 'babl/schema'
2
2
  require 'babl/errors'
3
- require 'babl/utils/value'
3
+ require 'babl/utils'
4
4
 
5
5
  module Babl
6
6
  module Nodes
@@ -1,6 +1,5 @@
1
- require 'babl/utils/hash'
2
- require 'babl/schema/fixed_array'
3
- require 'babl/utils/value'
1
+ require 'babl/schema'
2
+ require 'babl/utils'
4
3
 
5
4
  module Babl
6
5
  module Nodes
@@ -1,5 +1,4 @@
1
- require 'babl/utils/hash'
2
- require 'babl/utils/value'
1
+ require 'babl/utils'
3
2
 
4
3
  module Babl
5
4
  module Nodes
@@ -1,5 +1,5 @@
1
- require 'singleton'
2
1
  require 'babl/errors'
2
+ require 'singleton'
3
3
 
4
4
  module Babl
5
5
  module Nodes
@@ -1,6 +1,6 @@
1
- require 'babl/utils/hash'
2
1
  require 'babl/errors'
3
- require 'babl/utils/value'
2
+ require 'babl/utils'
3
+ require 'babl/schema'
4
4
 
5
5
  module Babl
6
6
  module Nodes
@@ -28,12 +28,19 @@ module Babl
28
28
 
29
29
  # Merge two documentations together
30
30
  def merge_doc(doc1, doc2)
31
- doc1 = Schema::Object::EMPTY if Schema::Static::NULL == doc1
32
- doc2 = Schema::Object::EMPTY if Schema::Static::NULL == doc2
33
-
34
31
  case
35
- when Schema::AnyOf === doc1 || Schema::AnyOf === doc2
36
- merge_extended(doc1, doc2)
32
+ when Schema::Static::NULL == doc1
33
+ merge_doc(Schema::Object::EMPTY, doc2)
34
+ when Schema::Static::NULL == doc2
35
+ merge_doc(doc1, Schema::Object::EMPTY)
36
+ when Schema::AnyOf === doc1
37
+ Schema::AnyOf.canonicalized(doc1.choices.map { |c|
38
+ merge_doc(c, doc2)
39
+ })
40
+ when Schema::AnyOf === doc2
41
+ Schema::AnyOf.canonicalized(doc2.choices.map { |c|
42
+ merge_doc(doc1, c)
43
+ })
37
44
  when Schema::Object === doc1 && Schema::Object === doc2
38
45
  merge_object(doc1, doc2)
39
46
  when Schema::Object === doc1 && Schema::Anything === doc2
@@ -45,34 +52,6 @@ module Babl
45
52
  end
46
53
  end
47
54
 
48
- # Merge two documention when Schema::AnyOf is involved either
49
- # on left, right or both sides.
50
- def merge_extended(doc1, doc2)
51
- # Ensure doc1 & doc2 are both Schema::AnyOf
52
- choices1 = Schema::AnyOf === doc1 ? doc1.choices : [doc1]
53
- choices2 = Schema::AnyOf === doc2 ? doc2.choices : [doc2]
54
-
55
- # Generate all possible combinations
56
- all_docs = choices1.product(choices2)
57
- .map { |choice1, choice2| merge_doc(choice1, choice2) }
58
-
59
- # Analyze each property accross all combination to
60
- # generate a Schema::Object::Property filled with
61
- # accurate information.
62
- final_properties = all_docs.flat_map(&:properties)
63
- .group_by(&:name)
64
- .map do |name, properties|
65
- Schema::Object::Property.new(
66
- name,
67
- Schema::AnyOf.canonicalized(properties.map(&:value)),
68
- properties.size == all_docs.size && properties.all?(&:required)
69
- )
70
- end
71
-
72
- # Generate the final Schema::Object
73
- Schema::Object.new(final_properties, all_docs.any?(&:additional))
74
- end
75
-
76
55
  # Merge two Schema::Object
77
56
  def merge_object(doc1, doc2)
78
57
  additional = doc1.additional || doc2.additional
@@ -1,4 +1,4 @@
1
- require 'babl/utils/value'
1
+ require 'babl/utils'
2
2
 
3
3
  module Babl
4
4
  module Nodes
@@ -1,6 +1,5 @@
1
- require 'babl/utils/hash'
2
- require 'babl/schema/object'
3
- require 'babl/utils/value'
1
+ require 'babl/schema'
2
+ require 'babl/utils'
4
3
 
5
4
  module Babl
6
5
  module Nodes
@@ -19,7 +18,9 @@ module Babl
19
18
  end
20
19
 
21
20
  def render(ctx)
22
- nodes.map { |k, v| [k, v.render(ctx)] }.to_h
21
+ out = {}
22
+ nodes.each { |k, v| out[k] = v.render(ctx) }
23
+ out
23
24
  end
24
25
  end
25
26
  end
@@ -1,6 +1,5 @@
1
- require 'babl/utils/ref'
2
1
  require 'babl/errors'
3
- require 'babl/utils/value'
2
+ require 'babl/utils'
4
3
 
5
4
  module Babl
6
5
  module Nodes
@@ -1,6 +1,5 @@
1
- require 'babl/nodes/terminal_value'
2
- require 'babl/schema/static'
3
- require 'babl/utils/value'
1
+ require 'babl/schema'
2
+ require 'babl/utils'
4
3
 
5
4
  module Babl
6
5
  module Nodes
@@ -1,7 +1,6 @@
1
- require 'babl/utils/hash'
2
- require 'babl/schema/any_of'
1
+ require 'babl/schema'
3
2
  require 'babl/errors'
4
- require 'babl/utils/value'
3
+ require 'babl/utils'
5
4
 
6
5
  module Babl
7
6
  module Nodes
@@ -1,6 +1,6 @@
1
- require 'singleton'
2
- require 'babl/schema/anything'
1
+ require 'babl/schema'
3
2
  require 'babl/errors'
3
+ require 'singleton'
4
4
 
5
5
  module Babl
6
6
  module Nodes
@@ -28,12 +28,12 @@ module Babl
28
28
  raise Errors::RenderingError, "#{e.message}\n" + ctx.formatted_stack(e.babl_stack), e.backtrace
29
29
  end
30
30
 
31
- def render_object(obj, stack = [])
31
+ def render_object(obj, stack = nil)
32
32
  case obj
33
- when String, Numeric, NilClass, TrueClass, FalseClass then obj
34
- when Hash then render_hash(obj, stack)
35
- when Array then render_array(obj, stack)
36
- else raise TerminalValueError.new("Only primitives can be serialized: #{obj}", stack)
33
+ when ::String, ::Numeric, ::NilClass, ::TrueClass, ::FalseClass then obj
34
+ when ::Hash then render_hash(obj, stack || [])
35
+ when ::Array then render_array(obj, stack || [])
36
+ else raise TerminalValueError.new("Only primitives can be serialized: #{obj}", stack || [])
37
37
  end
38
38
  end
39
39
 
@@ -49,18 +49,23 @@ module Babl
49
49
  end
50
50
 
51
51
  def render_hash(hash, stack)
52
- hash.map { |k, v|
52
+ out = {}
53
+ hash.each { |k, v|
53
54
  key = render_key(k, stack)
54
55
  stack.push key
55
- out = [key, render_object(v, stack)]
56
+ out[key] = render_object(v, stack)
56
57
  stack.pop
57
- out
58
- }.to_h
58
+ }
59
+ out
59
60
  end
60
61
 
61
62
  def render_key(key, stack)
62
63
  case key
63
- when Symbol, String, Numeric, NilClass, TrueClass, FalseClass then :"#{key}"
64
+ when ::Symbol then key
65
+ when ::String then key.to_sym
66
+ when ::TrueClass then :true
67
+ when ::FalseClass then :false
68
+ when ::Numeric, ::NilClass then :"#{key}"
64
69
  else raise TerminalValueError.new("Invalid key for JSON object: #{key}", stack)
65
70
  end
66
71
  end
@@ -1,5 +1,5 @@
1
- require 'babl/utils/value'
2
- require 'babl/schema/typed'
1
+ require 'babl/utils'
2
+ require 'babl/schema'
3
3
 
4
4
  module Babl
5
5
  module Nodes
@@ -1,5 +1,4 @@
1
- require 'babl/utils/hash'
2
- require 'babl/utils/value'
1
+ require 'babl/utils'
3
2
 
4
3
  module Babl
5
4
  module Nodes
@@ -0,0 +1,21 @@
1
+ require 'babl/operators/array'
2
+ require 'babl/operators/call'
3
+ require 'babl/operators/continue'
4
+ require 'babl/operators/default'
5
+ require 'babl/operators/dep'
6
+ require 'babl/operators/each'
7
+ require 'babl/operators/enter'
8
+ require 'babl/operators/extends'
9
+ require 'babl/operators/merge'
10
+ require 'babl/operators/nav'
11
+ require 'babl/operators/null'
12
+ require 'babl/operators/nullable'
13
+ require 'babl/operators/object'
14
+ require 'babl/operators/parent'
15
+ require 'babl/operators/partial'
16
+ require 'babl/operators/pin'
17
+ require 'babl/operators/source'
18
+ require 'babl/operators/static'
19
+ require 'babl/operators/switch'
20
+ require 'babl/operators/typed'
21
+ require 'babl/operators/with'
@@ -1,5 +1,4 @@
1
- require 'babl/nodes/fixed_array'
2
- require 'babl/nodes/terminal_value'
1
+ require 'babl/nodes'
3
2
 
4
3
  module Babl
5
4
  module Operators
@@ -1,4 +1,4 @@
1
- require 'babl/nodes/dep'
1
+ require 'babl/nodes'
2
2
 
3
3
  module Babl
4
4
  module Operators
@@ -1,4 +1,4 @@
1
- require 'babl/nodes/each'
1
+ require 'babl/nodes'
2
2
 
3
3
  module Babl
4
4
  module Operators
@@ -1,4 +1,5 @@
1
1
  require 'babl/errors'
2
+ require 'babl/nodes'
2
3
 
3
4
  module Babl
4
5
  module Operators
@@ -1,5 +1,4 @@
1
- require 'babl/nodes/merge'
2
- require 'babl/nodes/terminal_value'
1
+ require 'babl/nodes'
3
2
 
4
3
  module Babl
5
4
  module Operators
@@ -1,4 +1,4 @@
1
- require 'babl/nodes/nav'
1
+ require 'babl/nodes'
2
2
 
3
3
  module Babl
4
4
  module Operators
@@ -1,5 +1,4 @@
1
- require 'babl/nodes/object'
2
- require 'babl/nodes/terminal_value'
1
+ require 'babl/nodes'
3
2
  require 'babl/errors'
4
3
 
5
4
  module Babl
@@ -1,4 +1,4 @@
1
- require 'babl/nodes/parent'
1
+ require 'babl/nodes'
2
2
 
3
3
  module Babl
4
4
  module Operators
@@ -31,7 +31,7 @@ module Babl
31
31
 
32
32
  def initialize(search_path)
33
33
  @search_path = search_path
34
- raise 'Invalid search path' unless search_path
34
+ raise Errors::InvalidTemplate, 'Invalid search path' unless search_path
35
35
  end
36
36
 
37
37
  def find(partial_name)
@@ -1,6 +1,5 @@
1
- require 'babl/nodes/create_pin'
2
- require 'babl/nodes/goto_pin'
3
- require 'babl/utils/ref'
1
+ require 'babl/nodes'
2
+ require 'babl/utils'
4
3
  require 'babl/errors'
5
4
 
6
5
  module Babl
@@ -1,5 +1,4 @@
1
- require 'babl/nodes/static'
2
- require 'babl/nodes/terminal_value'
1
+ require 'babl/nodes'
3
2
  require 'babl/errors'
4
3
 
5
4
  module Babl
@@ -9,7 +8,8 @@ module Babl
9
8
  # Create a static JSON value
10
9
  def static(val)
11
10
  case val
12
- when String, Numeric, NilClass, TrueClass, FalseClass then construct_terminal { Nodes::Static.new(val) }
11
+ when ::String, ::Numeric, ::NilClass, ::TrueClass, ::FalseClass
12
+ construct_terminal { Nodes::Static.new(val) }
13
13
  else call(Nodes::TerminalValue.instance.render_object(val))
14
14
  end
15
15
  rescue Errors::RenderingError => exception
@@ -1,6 +1,4 @@
1
- require 'babl/nodes/switch'
2
- require 'babl/nodes/internal_value'
3
- require 'babl/nodes/terminal_value'
1
+ require 'babl/nodes'
4
2
 
5
3
  module Babl
6
4
  module Operators
@@ -1,4 +1,4 @@
1
- require 'babl/nodes/typed'
1
+ require 'babl/nodes'
2
2
 
3
3
  module Babl
4
4
  module Operators