babl-json 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/lib/babl.rb +1 -0
  3. data/lib/babl/builder.rb +1 -0
  4. data/lib/babl/builder/chain_builder.rb +1 -0
  5. data/lib/babl/builder/template_base.rb +1 -0
  6. data/lib/babl/errors.rb +1 -0
  7. data/lib/babl/nodes.rb +1 -0
  8. data/lib/babl/nodes/create_pin.rb +1 -0
  9. data/lib/babl/nodes/dep.rb +1 -0
  10. data/lib/babl/nodes/each.rb +1 -0
  11. data/lib/babl/nodes/fixed_array.rb +1 -0
  12. data/lib/babl/nodes/goto_pin.rb +1 -0
  13. data/lib/babl/nodes/internal_value.rb +1 -0
  14. data/lib/babl/nodes/merge.rb +5 -4
  15. data/lib/babl/nodes/nav.rb +7 -6
  16. data/lib/babl/nodes/object.rb +1 -0
  17. data/lib/babl/nodes/parent.rb +1 -0
  18. data/lib/babl/nodes/static.rb +7 -1
  19. data/lib/babl/nodes/switch.rb +1 -0
  20. data/lib/babl/nodes/terminal_value.rb +1 -0
  21. data/lib/babl/nodes/typed.rb +1 -0
  22. data/lib/babl/nodes/with.rb +6 -2
  23. data/lib/babl/operators.rb +1 -0
  24. data/lib/babl/operators/array.rb +1 -0
  25. data/lib/babl/operators/call.rb +1 -0
  26. data/lib/babl/operators/continue.rb +1 -0
  27. data/lib/babl/operators/default.rb +1 -0
  28. data/lib/babl/operators/dep.rb +1 -0
  29. data/lib/babl/operators/each.rb +1 -0
  30. data/lib/babl/operators/enter.rb +1 -0
  31. data/lib/babl/operators/extends.rb +1 -0
  32. data/lib/babl/operators/merge.rb +1 -0
  33. data/lib/babl/operators/nav.rb +1 -0
  34. data/lib/babl/operators/null.rb +1 -0
  35. data/lib/babl/operators/nullable.rb +4 -1
  36. data/lib/babl/operators/object.rb +1 -0
  37. data/lib/babl/operators/parent.rb +1 -0
  38. data/lib/babl/operators/partial.rb +1 -0
  39. data/lib/babl/operators/pin.rb +1 -0
  40. data/lib/babl/operators/source.rb +1 -0
  41. data/lib/babl/operators/static.rb +1 -0
  42. data/lib/babl/operators/switch.rb +1 -0
  43. data/lib/babl/operators/typed.rb +1 -0
  44. data/lib/babl/operators/with.rb +1 -0
  45. data/lib/babl/railtie.rb +1 -0
  46. data/lib/babl/rendering.rb +1 -0
  47. data/lib/babl/rendering/compiled_template.rb +1 -0
  48. data/lib/babl/rendering/context.rb +1 -10
  49. data/lib/babl/rendering/noop_preloader.rb +1 -0
  50. data/lib/babl/schema.rb +1 -0
  51. data/lib/babl/schema/any_of.rb +15 -17
  52. data/lib/babl/schema/anything.rb +1 -0
  53. data/lib/babl/schema/dyn_array.rb +1 -0
  54. data/lib/babl/schema/fixed_array.rb +1 -0
  55. data/lib/babl/schema/object.rb +6 -8
  56. data/lib/babl/schema/static.rb +6 -6
  57. data/lib/babl/schema/typed.rb +6 -5
  58. data/lib/babl/template.rb +1 -0
  59. data/lib/babl/utils.rb +1 -0
  60. data/lib/babl/utils/hash.rb +1 -0
  61. data/lib/babl/utils/ref.rb +1 -0
  62. data/lib/babl/utils/value.rb +1 -0
  63. data/lib/babl/version.rb +2 -1
  64. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b3d925354aed5c77de3cf7003b305b6de01b71f
4
- data.tar.gz: 24ffd681b9f421b971ac5340b1896608cb803313
3
+ metadata.gz: 2ba3e05ba09552694ee2869f3105feb91ec3459c
4
+ data.tar.gz: 1c5c24a986f264dc33db0d48a5f0885621c56793
5
5
  SHA512:
6
- metadata.gz: a935bea9bf8a01a924719526f7d26d1c5bdb016c6e64b3be08ae1040f6126b4aac8cc7dc032c38b7f2f2d1fbfc8ed9a53a091c6f7634b4c4c7b1cd894d7a7f6e
7
- data.tar.gz: 1fc88f586cd05f30dbcf2a766611b6bb2208006722b14ab53beceeffcacce5b1cb00397cfb3dab048497cd7f41a944810ab1e229f24c0b254bb2ab124355fabe
6
+ metadata.gz: 4014ee582da9da899725759eb4b3f0242929c8870f7326de59a5e65416e17b92469dab7cbb7c960ab4d783f696e6aa5d0840cbde14ec0fc7f951de0012e915fd
7
+ data.tar.gz: e3d4615978a94bd19c48f7089fa0722613b10dc44411f32d9f27cd8dbdf27c18f6dd62f59a9a0880fc22fb74991afbfcb111bb099e090d03f4f2c4523366dd63
data/lib/babl.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/railtie' if defined?(Rails)
2
3
  require 'babl/template'
3
4
  require 'babl/version'
data/lib/babl/builder.rb CHANGED
@@ -1,2 +1,3 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/builder/chain_builder'
2
3
  require 'babl/builder/template_base'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/nodes'
2
3
  require 'babl/errors'
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/nodes'
2
3
  require 'babl/builder'
3
4
  require 'babl/rendering'
data/lib/babl/errors.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Babl
2
3
  module Errors
3
4
  class Base < StandardError; end
data/lib/babl/nodes.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/nodes/create_pin'
2
3
  require 'babl/nodes/dep'
3
4
  require 'babl/nodes/each'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/utils'
2
3
 
3
4
  module Babl
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/utils'
2
3
 
3
4
  module Babl
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/schema'
2
3
  require 'babl/errors'
3
4
  require 'babl/utils'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/schema'
2
3
  require 'babl/utils'
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/utils'
2
3
 
3
4
  module Babl
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/errors'
2
3
  require 'babl/utils'
3
4
  require 'singleton'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/errors'
2
3
  require 'babl/utils'
3
4
  require 'babl/schema'
@@ -59,9 +60,9 @@ module Babl
59
60
 
60
61
  case
61
62
  when Schema::AnyOf === doc1
62
- Schema::AnyOf.canonicalized(doc1.choices.map { |c| merge_doc(c, doc2) })
63
+ Schema::AnyOf.canonicalized(doc1.choice_set.map { |c| merge_doc(c, doc2) })
63
64
  when Schema::AnyOf === doc2
64
- Schema::AnyOf.canonicalized(doc2.choices.map { |c| merge_doc(doc1, c) })
65
+ Schema::AnyOf.canonicalized(doc2.choice_set.map { |c| merge_doc(doc1, c) })
65
66
  when Schema::Object === doc1 && Schema::Object === doc2
66
67
  merge_object(doc1, doc2)
67
68
  else raise Errors::InvalidTemplate, 'Only objects can be merged'
@@ -73,8 +74,8 @@ module Babl
73
74
  additional = doc1.additional || doc2.additional
74
75
 
75
76
  properties = (
76
- doc1.properties.map { |property| doc2.additional ? anything_property(property) : property } +
77
- doc2.properties
77
+ doc1.property_set.map { |property| doc2.additional ? anything_property(property) : property } +
78
+ doc2.property_set.to_a
78
79
  ).each_with_object({}) { |property, acc| acc[property.name] = property }.values
79
80
 
80
81
  Schema::Object.new(properties, additional)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/utils'
2
3
 
3
4
  module Babl
@@ -16,17 +17,17 @@ module Babl
16
17
  end
17
18
 
18
19
  def render(ctx)
19
- node.render(ctx.move_forward_block(through) { navigate(ctx.object) })
20
+ value = begin
21
+ ::Hash === ctx.object ? ctx.object.fetch(through) : ctx.object.send(through)
22
+ rescue StandardError => e
23
+ raise Errors::RenderingError, "#{e.message}\n" + ctx.formatted_stack(through), e.backtrace
24
+ end
25
+ node.render(ctx.move_forward(value, through))
20
26
  end
21
27
 
22
28
  private
23
29
 
24
30
  def navigate(object)
25
- if ::Hash === object
26
- object.fetch(through)
27
- else
28
- object.send(through)
29
- end
30
31
  end
31
32
  end
32
33
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/schema'
2
3
  require 'babl/utils'
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/errors'
2
3
  require 'babl/utils'
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/schema'
2
3
  require 'babl/utils'
3
4
 
@@ -5,7 +6,12 @@ module Babl
5
6
  module Nodes
6
7
  class Static < Utils::Value.new(:value)
7
8
  def schema
8
- Schema::Static.new(value)
9
+ case value
10
+ when ::NilClass then Schema::Static::NULL
11
+ when ::TrueClass then Schema::Static::TRUE
12
+ when ::FalseClass then Schema::Static::FALSE
13
+ else Schema::Static.new(value)
14
+ end
9
15
  end
10
16
 
11
17
  def render(_ctx)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/schema'
2
3
  require 'babl/errors'
3
4
  require 'babl/utils'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/schema'
2
3
  require 'babl/errors'
3
4
  require 'babl/utils'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/utils'
2
3
  require 'babl/schema'
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/utils'
2
3
 
3
4
  module Babl
@@ -19,9 +20,12 @@ module Babl
19
20
 
20
21
  def render(ctx)
21
22
  values = nodes.map { |n| n.render(ctx) }
22
- node.render(ctx.move_forward_block(:__block__) do
23
+ value = begin
23
24
  block.arity.zero? ? ctx.object.instance_exec(&block) : block.call(*values)
24
- end)
25
+ rescue StandardError => e
26
+ raise Errors::RenderingError, "#{e.message}\n" + ctx.formatted_stack(:__block__), e.backtrace
27
+ end
28
+ node.render(ctx.move_forward(value, :__block__))
25
29
  end
26
30
  end
27
31
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/operators/array'
2
3
  require 'babl/operators/call'
3
4
  require 'babl/operators/continue'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/nodes'
2
3
 
3
4
  module Babl
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/errors'
2
3
 
3
4
  module Babl
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/errors'
2
3
 
3
4
  module Babl
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Babl
2
3
  module Operators
3
4
  module Default
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/nodes'
2
3
 
3
4
  module Babl
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/nodes'
2
3
 
3
4
  module Babl
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/errors'
2
3
  require 'babl/nodes'
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Babl
2
3
  module Operators
3
4
  module Extends
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/nodes'
2
3
  require 'babl/utils'
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/nodes'
2
3
 
3
4
  module Babl
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Babl
2
3
  module Operators
3
4
  module Null
@@ -1,13 +1,16 @@
1
+ # frozen_string_literal: true
1
2
  module Babl
2
3
  module Operators
3
4
  module Nullable
4
5
  module DSL
6
+ IS_NIL = ->(val) { ::NilClass === val }
7
+
5
8
  # Nullify the current construction if
6
9
  # the current element is Nil.
7
10
  def nullable
8
11
  source {
9
12
  switch(
10
- nav(&:nil?) => nil,
13
+ nav(&IS_NIL) => nil,
11
14
  default => continue
12
15
  )
13
16
  }
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/nodes'
2
3
  require 'babl/errors'
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/nodes'
2
3
 
3
4
  module Babl
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/errors'
2
3
 
3
4
  module Babl
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/nodes'
2
3
  require 'babl/utils'
3
4
  require 'babl/errors'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Babl
2
3
  module Operators
3
4
  module Source
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/nodes'
2
3
  require 'babl/errors'
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/nodes'
2
3
  require 'babl/utils'
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/nodes'
2
3
 
3
4
  module Babl
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/nodes'
2
3
 
3
4
  module Babl
data/lib/babl/railtie.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Babl
2
3
  module ActionView
3
4
  module Template
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/rendering/compiled_template'
2
3
  require 'babl/rendering/noop_preloader'
3
4
  require 'babl/rendering/context'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'oj'
2
3
  require 'babl/rendering'
3
4
  require 'babl/utils'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/errors'
2
3
  require 'babl/utils'
3
4
 
@@ -43,16 +44,6 @@ module Babl
43
44
  Context.new(object, key, parent, (pins || Utils::Hash::EMPTY).merge(ref => self))
44
45
  end
45
46
 
46
- # Wrapper around #move_forward navigating into the return value of
47
- # the block. However, if an error occurs, it is wrapped in a
48
- # RenderingError and the navigation stack trace is added
49
- # to the error message.
50
- def move_forward_block(key)
51
- move_forward(yield, key)
52
- rescue StandardError => e
53
- raise Errors::RenderingError, "#{e.message}\n" + formatted_stack(key), e.backtrace
54
- end
55
-
56
47
  def formatted_stack(*additional_stack_items)
57
48
  stack_trace = ([:__root__] + stack + additional_stack_items).join('.')
58
49
  "BABL @ #{stack_trace}"
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Babl
2
3
  module Rendering
3
4
  # BABL uses this preloader by default. It does nothing.
data/lib/babl/schema.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/schema/any_of'
2
3
  require 'babl/schema/anything'
3
4
  require 'babl/schema/dyn_array'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/utils'
2
3
  require 'babl/schema'
3
4
  require 'set'
@@ -5,16 +6,13 @@ require 'set'
5
6
  module Babl
6
7
  module Schema
7
8
  class AnyOf < Utils::Value.new(:choice_set)
8
- attr_reader :choices
9
-
10
9
  def initialize(choices)
11
- flattened_choices = choices.flat_map { |doc| AnyOf === doc ? doc.choices : [doc] }.uniq
12
- @choices = flattened_choices
13
- super(flattened_choices.to_set)
10
+ flattened_choices = choices.flat_map { |doc| AnyOf === doc ? doc.choice_set.to_a : [doc] }.uniq
11
+ super(flattened_choices.to_set.freeze)
14
12
  end
15
13
 
16
14
  def json
17
- { anyOf: choices.map(&:json) }
15
+ { anyOf: choice_set.map(&:json) }
18
16
  end
19
17
 
20
18
  # Perform simple transformations in order to reduce the size of the generated
@@ -39,7 +37,7 @@ module Babl
39
37
 
40
38
  # We can completely get rid of the AnyOf element of there is only one possible schema.
41
39
  def simplify_single
42
- choices.size == 1 ? choices.first : nil
40
+ choice_set.size == 1 ? choice_set.first : nil
43
41
  end
44
42
 
45
43
  # AnyOf[anything, string, number...] always collapse to anything.
@@ -58,9 +56,9 @@ module Babl
58
56
  # AnyOf[number, 2, 3.1] is just number
59
57
  # AnyOf[integer, 2, 1] is just integer
60
58
  def simplify_typed_and_static
61
- choices.each do |typed|
59
+ choice_set.each do |typed|
62
60
  next unless Typed === typed
63
- instances = choices.select { |instance|
61
+ instances = choice_set.select { |instance|
64
62
  Static === instance && typed.classes.any? { |clazz| clazz === instance.value }
65
63
  }
66
64
  next if instances.empty?
@@ -84,10 +82,10 @@ module Babl
84
82
  # If the static array is an instance of another dyn array, then the fixed array can be
85
83
  # removed.
86
84
  def simplify_dyn_and_fixed_array
87
- fixed_arrays = choices.select { |s| FixedArray === s && s.items.uniq.size == 1 }
85
+ fixed_arrays = choice_set.select { |s| FixedArray === s && s.items.uniq.size == 1 }
88
86
  return if fixed_arrays.empty?
89
87
 
90
- choices.each do |dyn|
88
+ choice_set.each do |dyn|
91
89
  next unless DynArray === dyn
92
90
  fixed_arrays.each do |fixed|
93
91
  new_dyn = DynArray.new(dyn.item)
@@ -102,15 +100,15 @@ module Babl
102
100
  # of the generated schema. On top of that, when the JSON-Schema is translated into Typescript, it produces
103
101
  # a much more workable type definition (union of anonymous object types is not practical to use)
104
102
  def simplify_merge_objects
105
- choices.each_with_index { |obj1, index1|
103
+ choice_set.each_with_index { |obj1, index1|
106
104
  next unless Object === obj1
107
105
 
108
- choices.each_with_index { |obj2, index2|
106
+ choice_set.each_with_index { |obj2, index2|
109
107
  break if index2 >= index1
110
108
  next unless Object === obj2
111
109
 
112
- obj1props = obj1.properties.map { |p| [p.name, p] }.to_h
113
- obj2props = obj2.properties.map { |p| [p.name, p] }.to_h
110
+ obj1props = obj1.property_set.map { |p| [p.name, p] }.to_h
111
+ obj2props = obj2.property_set.map { |p| [p.name, p] }.to_h
114
112
 
115
113
  # Do not merge properties unless a keyset is almost a subset of the other
116
114
  next unless (obj1props.keys.to_set - obj2props.keys).size <= 1 ||
@@ -147,9 +145,9 @@ module Babl
147
145
 
148
146
  # Push down the AnyOf to the item if all outputs are of type DynArray
149
147
  def simplify_push_down_dyn_array
150
- choices.each_with_index { |arr1, index1|
148
+ choice_set.each_with_index { |arr1, index1|
151
149
  next unless DynArray === arr1
152
- choices.each_with_index { |arr2, index2|
150
+ choice_set.each_with_index { |arr2, index2|
153
151
  break if index2 >= index1
154
152
  next unless DynArray === arr2
155
153
  new_arr = DynArray.new(AnyOf.canonicalized([arr1.item, arr2.item]))
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/utils'
2
3
  require 'singleton'
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/utils'
2
3
 
3
4
  module Babl
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/utils'
2
3
 
3
4
  module Babl
@@ -1,14 +1,12 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/utils'
2
3
  require 'set'
3
4
 
4
5
  module Babl
5
6
  module Schema
6
7
  class Object < Utils::Value.new(:property_set, :additional)
7
- attr_reader :properties
8
-
9
8
  def initialize(properties, additional)
10
- @properties = properties
11
- super(properties.to_set, additional)
9
+ super(properties.to_set.freeze, additional)
12
10
  end
13
11
 
14
12
  EMPTY = new([], false)
@@ -22,12 +20,12 @@ module Babl
22
20
 
23
21
  def json
24
22
  { type: 'object' }.tap { |out|
25
- next if properties.empty?
26
- out[:properties] = properties.map { |property| [property.name, property.value.json] }.to_h
23
+ next if property_set.empty?
24
+ out[:properties] = property_set.map { |property| [property.name, property.value.json] }.to_h
27
25
  out[:additionalProperties] = additional
28
- required_properties = properties.select(&:required)
26
+ required_properties = property_set.select(&:required)
29
27
  next if required_properties.empty?
30
- out[:required] = properties.select(&:required).map(&:name).map(&:to_s)
28
+ out[:required] = property_set.select(&:required).map(&:name).map(&:to_s)
31
29
  }
32
30
  end
33
31
  end
@@ -1,16 +1,16 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/utils'
2
3
 
3
4
  module Babl
4
5
  module Schema
5
- class Static < Utils::Value.new(:value)
6
+ class Static < Utils::Value.new(:value, :json)
7
+ def initialize(value)
8
+ super(value, ::NilClass === value ? { type: 'null' } : { enum: [value] })
9
+ end
10
+
6
11
  NULL = new(nil)
7
12
  TRUE = new(true)
8
13
  FALSE = new(false)
9
-
10
- def json
11
- return { type: 'null' } if value.nil?
12
- { enum: [value] }
13
- end
14
14
  end
15
15
  end
16
16
  end
@@ -1,16 +1,17 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/utils'
2
3
 
3
4
  module Babl
4
5
  module Schema
5
- class Typed < Utils::Value.new(:type, :classes)
6
+ class Typed < Utils::Value.new(:type, :classes, :json)
7
+ def initialize(type, classes)
8
+ super(type.freeze, classes.freeze, { type: type }.freeze)
9
+ end
10
+
6
11
  INTEGER = new('integer', [::Integer])
7
12
  BOOLEAN = new('boolean', [::TrueClass, ::FalseClass])
8
13
  NUMBER = new('number', [::Numeric])
9
14
  STRING = new('string', [::String])
10
-
11
- def json
12
- { type: type }
13
- end
14
15
  end
15
16
  end
16
17
  end
data/lib/babl/template.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/builder'
2
3
  require 'babl/operators'
3
4
 
data/lib/babl/utils.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/utils/hash'
2
3
  require 'babl/utils/ref'
3
4
  require 'babl/utils/value'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Babl
2
3
  module Utils
3
4
  class Hash
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Babl
2
3
  module Utils
3
4
  class Ref
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'babl/utils'
2
3
 
3
4
  module Babl
data/lib/babl/version.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Babl
2
- VERSION = '0.2.5'
3
+ VERSION = '0.2.6'
3
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.2.5
4
+ version: 0.2.6
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-08-21 00:00:00.000000000 Z
11
+ date: 2017-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry