babl-json 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +3 -0
  3. data/.rubocop.yml +228 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +4 -0
  6. data/CHANGELOG.md +5 -0
  7. data/Gemfile +5 -0
  8. data/LICENSE +7 -0
  9. data/README.md +87 -0
  10. data/babl.gemspec +23 -0
  11. data/lib/babl.rb +41 -0
  12. data/lib/babl/builder/chain_builder.rb +85 -0
  13. data/lib/babl/builder/template_base.rb +37 -0
  14. data/lib/babl/operators/array.rb +42 -0
  15. data/lib/babl/operators/call.rb +25 -0
  16. data/lib/babl/operators/dep.rb +48 -0
  17. data/lib/babl/operators/each.rb +45 -0
  18. data/lib/babl/operators/enter.rb +22 -0
  19. data/lib/babl/operators/merge.rb +49 -0
  20. data/lib/babl/operators/nav.rb +55 -0
  21. data/lib/babl/operators/nullable.rb +16 -0
  22. data/lib/babl/operators/object.rb +55 -0
  23. data/lib/babl/operators/parent.rb +90 -0
  24. data/lib/babl/operators/partial.rb +46 -0
  25. data/lib/babl/operators/pin.rb +78 -0
  26. data/lib/babl/operators/source.rb +12 -0
  27. data/lib/babl/operators/static.rb +40 -0
  28. data/lib/babl/operators/switch.rb +71 -0
  29. data/lib/babl/operators/with.rb +51 -0
  30. data/lib/babl/railtie.rb +29 -0
  31. data/lib/babl/rendering/compiled_template.rb +28 -0
  32. data/lib/babl/rendering/context.rb +60 -0
  33. data/lib/babl/rendering/internal_value_node.rb +30 -0
  34. data/lib/babl/rendering/noop_preloader.rb +10 -0
  35. data/lib/babl/rendering/terminal_value_node.rb +54 -0
  36. data/lib/babl/template.rb +48 -0
  37. data/lib/babl/utils/hash.rb +11 -0
  38. data/lib/babl/version.rb +3 -0
  39. data/spec/construction_spec.rb +246 -0
  40. data/spec/navigation_spec.rb +133 -0
  41. data/spec/partial_spec.rb +53 -0
  42. data/spec/pinning_spec.rb +137 -0
  43. metadata +145 -0
@@ -0,0 +1,53 @@
1
+ require 'babl'
2
+
3
+ describe ::Babl::Operators::Partial do
4
+ before {
5
+ stub_const('TestLookupContext', Class.new {
6
+ attr_reader :code, :childs
7
+
8
+ def initialize(code = nil, **childs)
9
+ @code = code
10
+ @childs = childs
11
+ end
12
+
13
+ def find(name)
14
+ name = name.to_sym
15
+ return unless childs[name]
16
+ [name.to_s, childs[name].code, childs[name]]
17
+ end
18
+ })
19
+ }
20
+
21
+ let(:dsl) { ::Babl::Template.new }
22
+ let(:compiled) { template.compile }
23
+ let(:json) { Oj.load(compiled.json(object)) }
24
+ let(:object) { { some_property: 12 } }
25
+
26
+ let(:custom_lookup_context) {
27
+ TestLookupContext.new(
28
+ blabla: TestLookupContext.new(
29
+ "[partial('navi').partial('miche'), partial('muche')]",
30
+
31
+ miche: TestLookupContext.new(
32
+ "partial('blabla')",
33
+
34
+ blabla: TestLookupContext.new('call { 1 + self }')
35
+ ),
36
+
37
+ muche: TestLookupContext.new('23'),
38
+ navi: TestLookupContext.new(':some_property')
39
+ )
40
+ )
41
+ }
42
+ let(:ctx_dsl) { dsl.with_lookup_context(custom_lookup_context) }
43
+
44
+ context 'missing partial' do
45
+ let(:template) { ctx_dsl.partial('i_do_not_exist') }
46
+ it { expect { compiled }.to raise_error Babl::InvalidTemplateError }
47
+ end
48
+
49
+ context 'found partial' do
50
+ let(:template) { ctx_dsl.partial('blabla') }
51
+ it { expect(json).to eq [13, 23] }
52
+ end
53
+ end
@@ -0,0 +1,137 @@
1
+ require 'babl'
2
+
3
+ describe ::Babl::Template do
4
+ let(:dsl) { ::Babl::Template.new }
5
+ let(:compiled) { template.compile }
6
+ let(:json) { Oj.load(compiled.json(object)) }
7
+ let(:dependencies) { compiled.dependencies }
8
+ let(:documentation) { compiled.documentation }
9
+
10
+ describe '#pin' do
11
+ context 'simple pinning' do
12
+ let(:template) {
13
+ dsl.source {
14
+ nav(:a).pin { |a|
15
+ nav(:b).object(
16
+ x: _,
17
+ y: a.nav(:y)
18
+ )
19
+ }
20
+ }
21
+ }
22
+
23
+ let(:object) {
24
+ { a: { b: { x: 42 }, y: 13 } }
25
+ }
26
+
27
+ it { expect(json).to eq('x' => 42, 'y' => 13) }
28
+ it { expect(dependencies).to eq(a: { y: {}, b: { x: {} } }) }
29
+ it { expect(documentation).to eq(x: :__value__, y: :__value__) }
30
+ end
31
+
32
+ context 'when visiting parent from pin' do
33
+ let(:template) {
34
+ dsl.source {
35
+ nav(:a).pin { |p1|
36
+ object(x: _, y: p1.parent.pin { |p2| p2.nav(:a, :b) })
37
+ }
38
+ }
39
+ }
40
+
41
+ let(:object) { { a: { x: 12, b: 42 } } }
42
+
43
+ it { expect(json).to eq('x' => 12, 'y' => 42) }
44
+ it { expect(dependencies).to eq(a: { x: {}, b: {} }) }
45
+ it { expect(documentation).to eq(x: :__value__, y: :__value__) }
46
+ end
47
+
48
+ context 'when pinning is misused (out of context)' do
49
+ let(:template) {
50
+ dsl.source {
51
+ pinref = nil
52
+ object(
53
+ a: pin { |p| pinref = p },
54
+ b: pinref.nav(:lol)
55
+ )
56
+ }
57
+ }
58
+
59
+ it { expect { compiled }.to raise_error Babl::InvalidTemplateError }
60
+ end
61
+
62
+ context 'when pinning is mixed with a "with" context' do
63
+ let(:template) {
64
+ dsl.source {
65
+ pin { |root|
66
+ with(:a) { |a| a }.object(
67
+ x: _,
68
+ y: root.nav(:lol)
69
+ )
70
+ }
71
+ }
72
+ }
73
+
74
+ let(:object) { { a: { x: 34 }, lol: 1 } }
75
+
76
+ it { expect(json).to eq('x' => 34, 'y' => 1) }
77
+ it { expect(dependencies).to eq(a: {}, lol: {}) }
78
+ it { expect(documentation).to eq(x: :__value__, y: :__value__) }
79
+ end
80
+
81
+ context 'navigating pinning' do
82
+ let(:template) {
83
+ dsl.source {
84
+ pin(:timezone) { |timezone| object(applicant: _.object(:id, tz: timezone)) }
85
+ }
86
+ }
87
+
88
+ let(:object) { { applicant: { id: 1 }, timezone: 'LA' } }
89
+
90
+ it { expect(json).to eq('applicant' => { 'id' => 1, 'tz' => 'LA' }) }
91
+ it { expect(dependencies).to eq(timezone: {}, applicant: { id: {} }) }
92
+ it { expect(documentation).to eq(applicant: { id: :__value__, tz: :__value__ }) }
93
+ end
94
+
95
+ context 'pin used twice in same chain' do
96
+ let(:template) {
97
+ dsl.source {
98
+ nav(:a).pin { |a|
99
+ object(
100
+ h: nav(:h),
101
+ a: a.object(
102
+ x: _,
103
+ y: a.nav(:lol).parent.nav(:mdr)
104
+ )
105
+ )
106
+ }
107
+ }
108
+ }
109
+
110
+ let(:object) { { a: { h: 42, x: 13, lol: 11, mdr: 34 } } }
111
+
112
+ it { expect(json).to eq('h' => 42, 'a' => { 'x' => 13, 'y' => 34 }) }
113
+ it { expect(dependencies).to eq(a: { h: {}, x: {}, lol: {}, mdr: {} }) }
114
+ it { expect(documentation).to eq(h: :__value__, a: { x: :__value__, y: :__value__ }) }
115
+ end
116
+
117
+ context 'nested pinning' do
118
+ let(:template) {
119
+ dsl.source {
120
+ pin { |root|
121
+ nav(:a).pin { |a|
122
+ object(
123
+ a: a.nav(:x),
124
+ root: root.nav(:y)
125
+ )
126
+ }
127
+ }
128
+ }
129
+ }
130
+
131
+ let(:object) { { a: { x: 42 }, y: 13 } }
132
+
133
+ it { expect(json).to eq('a' => 42, 'root' => 13) }
134
+ it { expect(documentation).to eq(a: :__value__, root: :__value__) }
135
+ end
136
+ end
137
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: babl-json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Frederic Terrazzoni
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.48'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.48'
55
+ - !ruby/object:Gem::Dependency
56
+ name: oj
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: JSON templating on steroids
70
+ email:
71
+ - frederic.terrazzoni@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rubocop.yml"
78
+ - ".ruby-version"
79
+ - ".travis.yml"
80
+ - CHANGELOG.md
81
+ - Gemfile
82
+ - LICENSE
83
+ - README.md
84
+ - babl.gemspec
85
+ - lib/babl.rb
86
+ - lib/babl/builder/chain_builder.rb
87
+ - lib/babl/builder/template_base.rb
88
+ - lib/babl/operators/array.rb
89
+ - lib/babl/operators/call.rb
90
+ - lib/babl/operators/dep.rb
91
+ - lib/babl/operators/each.rb
92
+ - lib/babl/operators/enter.rb
93
+ - lib/babl/operators/merge.rb
94
+ - lib/babl/operators/nav.rb
95
+ - lib/babl/operators/nullable.rb
96
+ - lib/babl/operators/object.rb
97
+ - lib/babl/operators/parent.rb
98
+ - lib/babl/operators/partial.rb
99
+ - lib/babl/operators/pin.rb
100
+ - lib/babl/operators/source.rb
101
+ - lib/babl/operators/static.rb
102
+ - lib/babl/operators/switch.rb
103
+ - lib/babl/operators/with.rb
104
+ - lib/babl/railtie.rb
105
+ - lib/babl/rendering/compiled_template.rb
106
+ - lib/babl/rendering/context.rb
107
+ - lib/babl/rendering/internal_value_node.rb
108
+ - lib/babl/rendering/noop_preloader.rb
109
+ - lib/babl/rendering/terminal_value_node.rb
110
+ - lib/babl/template.rb
111
+ - lib/babl/utils/hash.rb
112
+ - lib/babl/version.rb
113
+ - spec/construction_spec.rb
114
+ - spec/navigation_spec.rb
115
+ - spec/partial_spec.rb
116
+ - spec/pinning_spec.rb
117
+ homepage: https://github.com/getbannerman/babl
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.6.10
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: JSON templating on steroids
141
+ test_files:
142
+ - spec/construction_spec.rb
143
+ - spec/navigation_spec.rb
144
+ - spec/partial_spec.rb
145
+ - spec/pinning_spec.rb