babl-json 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/babl/nodes/terminal_value.rb +3 -2
- data/lib/babl/nodes/typed.rb +4 -1
- data/lib/babl/operators/static.rb +5 -3
- data/lib/babl/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f2f029a652582cac66cd36e290f5cda18388ed5
|
4
|
+
data.tar.gz: 44aabe9259b3b4c91147578e2a0c4528a45f3248
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d6199f5bd49ba5e467902e79ca254cf97dba01b21d92141ab451786c0fdba79a5701743f2c4f2766a9928924dd91bd8073c17f0d1542c83f3fbb047d852db31
|
7
|
+
data.tar.gz: 25c6e56a337e7eb69bd085a3454ef1183dbe489e372f9970be1ec1d8591b0b2df959d6790d69f3ac269c4735444ab5022233d635fe58d9ad6ea5aa922c035173
|
@@ -32,10 +32,11 @@ module Babl
|
|
32
32
|
|
33
33
|
def render_object(obj, stack = nil)
|
34
34
|
case obj
|
35
|
-
when ::String, ::
|
35
|
+
when ::String, ::Integer, ::NilClass, ::TrueClass, ::FalseClass then obj
|
36
|
+
when ::Numeric then obj.to_f
|
37
|
+
when ::Symbol then obj.to_s
|
36
38
|
when ::Hash then render_hash(obj, stack || [])
|
37
39
|
when ::Array then render_array(obj, stack || [])
|
38
|
-
when ::Symbol then obj.to_s
|
39
40
|
else raise TerminalValueError.new("Only primitives can be serialized: #{obj}", stack || [])
|
40
41
|
end
|
41
42
|
end
|
data/lib/babl/nodes/typed.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
require 'babl/utils'
|
3
3
|
require 'babl/schema'
|
4
|
+
require 'babl/nodes/terminal_value'
|
4
5
|
|
5
6
|
module Babl
|
6
7
|
module Nodes
|
@@ -20,7 +21,9 @@ module Babl
|
|
20
21
|
|
21
22
|
def render(ctx)
|
22
23
|
value = ctx.object
|
23
|
-
|
24
|
+
if schema.classes.any? { |clazz| clazz === value }
|
25
|
+
return ::Numeric === value ? TerminalValue.instance.render_object(value) : value
|
26
|
+
end
|
24
27
|
raise Errors::RenderingError, "Expected type '#{schema.type}': #{value}\n#{ctx.formatted_stack}"
|
25
28
|
end
|
26
29
|
|
@@ -8,10 +8,12 @@ module Babl
|
|
8
8
|
module DSL
|
9
9
|
# Create a static JSON value
|
10
10
|
def static(val)
|
11
|
-
|
11
|
+
sanitized_val = Nodes::TerminalValue.instance.render_object(val)
|
12
|
+
|
13
|
+
case sanitized_val
|
12
14
|
when ::String, ::Numeric, ::NilClass, ::TrueClass, ::FalseClass
|
13
|
-
construct_terminal { Nodes::Constant.new(
|
14
|
-
else call(
|
15
|
+
construct_terminal { Nodes::Constant.new(sanitized_val, Schema::Primitive.new(sanitized_val)) }
|
16
|
+
else call(sanitized_val)
|
15
17
|
end
|
16
18
|
rescue Errors::RenderingError => exception
|
17
19
|
raise Errors::InvalidTemplate, exception.message
|
data/lib/babl/version.rb
CHANGED