json_factory 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +131 -71
- data/lib/json_factory.rb +0 -1
- data/lib/json_factory/configuration.rb +4 -0
- data/lib/json_factory/dsl.rb +3 -0
- data/lib/json_factory/dsl/object_array.rb +1 -1
- data/lib/json_factory/dsl/object_if.rb +20 -0
- data/lib/json_factory/json_builder.rb +17 -3
- data/lib/json_factory/version.rb +1 -1
- metadata +3 -3
- data/examples/readme_examples.rb +0 -39
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 454931a891f470c353038999c80a2ec56bcb6123
|
4
|
+
data.tar.gz: 5a56f375ce2338492bc71e6994cbed1dca3dce20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d83cf336f0a24cac3e33b7593b3c80b29531ae4edbca150fa59d7113c99b2e5beadeae88fa294ddadc86cb3fa8ed84b26bbe9039c68f2e5c4d611817d12c14f
|
7
|
+
data.tar.gz: 1bcf40a03c5647c892ab7738d0c420f244a978b76d9fb8a784c04022f382b362880713df43610d9e6b75faac22b8b957ac558844d5e78ff5972dbf2ce1b2c99c
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# JsonFactory
|
4
4
|
|
5
|
-
JsonFactory is a Easy DSL to create JSON
|
5
|
+
JsonFactory is a Easy DSL to create JSON with focus on performance and flexibility.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -22,108 +22,123 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
| DSL Method
|
26
|
-
|
|
27
|
-
| value
|
28
|
-
| object
|
29
|
-
| member
|
30
|
-
| array
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
25
|
+
| DSL Method | Description |
|
26
|
+
| ------------------------------------ |:------------------------------------------------------------------- |
|
27
|
+
| [value](#value-method) | Generates a JSON value. |
|
28
|
+
| [object](#object-method) | Generates a JSON object. |
|
29
|
+
| [member](#member-method) | Adds a key-value pair to a JSON object. |
|
30
|
+
| [array](#array-method) | Generates a JSON array. |
|
31
|
+
| [object_array](#object_array-method) | Generates a JSON array. |
|
32
|
+
| [element](#element-method) | Adds a value to a JSON array. |
|
33
|
+
| [partial](#partial-method) | Loads the given partial and evaluates it using the local variables. |
|
34
|
+
| [cache](#cache-method) | Caches the given content under the specified key. |
|
35
|
+
| [object_if](#object_if-method) | Generates a JSON object if condition is true. |
|
34
36
|
|
35
|
-
#####
|
37
|
+
##### value method
|
36
38
|
|
37
39
|
```ruby
|
38
40
|
factory = <<-RUBY
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
41
|
+
value nil
|
42
|
+
RUBY
|
43
|
+
|
44
|
+
puts JSONFactory.build(factory) # => null
|
45
|
+
```
|
46
|
+
|
47
|
+
##### object method
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
factory = <<-RUBY
|
51
|
+
object do
|
52
|
+
member :data do
|
53
|
+
object do
|
54
|
+
member :id, object.id
|
50
55
|
end
|
51
56
|
end
|
52
57
|
end
|
53
58
|
RUBY
|
54
59
|
|
55
60
|
# test data
|
56
|
-
|
57
|
-
test_object_2 = OpenStruct.new(id: '002', name: 'TestObject3')
|
58
|
-
test_object = OpenStruct.new(id: '1', name: 'TestObject1', test_objects: [test_object_1, test_object_2])
|
61
|
+
test_object = OpenStruct.new(id: 1)
|
59
62
|
|
60
|
-
puts JSONFactory.build(factory, object: test_object)
|
63
|
+
puts JSONFactory.build(factory, object: test_object) # => {"data":{"id":1}}
|
61
64
|
```
|
62
65
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
}
|
66
|
+
##### member method
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
factory = <<-RUBY
|
70
|
+
object do
|
71
|
+
member :foo, 'bar'
|
72
|
+
end
|
73
|
+
RUBY
|
74
|
+
|
75
|
+
puts JSONFactory.build(factory) # => {"foo":"bar"}
|
74
76
|
```
|
75
77
|
|
76
|
-
#####
|
78
|
+
##### array method
|
77
79
|
|
78
80
|
```ruby
|
79
81
|
factory = <<-RUBY
|
80
|
-
|
81
|
-
|
82
|
-
|
82
|
+
array
|
83
|
+
objects.each do |test_object|
|
84
|
+
element :id, test_object.id
|
85
|
+
end
|
83
86
|
end
|
84
87
|
RUBY
|
85
88
|
|
86
89
|
# test data
|
87
|
-
test_object_1 = OpenStruct.new(id:
|
88
|
-
test_object_2 = OpenStruct.new(id:
|
90
|
+
test_object_1 = OpenStruct.new(id: 1)
|
91
|
+
test_object_2 = OpenStruct.new(id: 2)
|
89
92
|
|
90
|
-
puts JSONFactory.build(factory, objects: [test_object_1, test_object_2])
|
93
|
+
puts JSONFactory.build(factory, objects: [test_object_1, test_object_2]) # => [{"id": 1},{"id":2}]
|
91
94
|
```
|
92
95
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
96
|
+
##### object_array method
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
factory = <<-RUBY
|
100
|
+
object_array objects do |test_object|
|
101
|
+
member :id, test_object.id
|
102
|
+
end
|
103
|
+
RUBY
|
104
|
+
|
105
|
+
# test data
|
106
|
+
test_object_1 = OpenStruct.new(id: 1)
|
107
|
+
test_object_2 = OpenStruct.new(id: 2)
|
108
|
+
|
109
|
+
puts JSONFactory.build(factory, objects: [test_object_1, test_object_2]) # => [{"id":1},{"id":2}]
|
98
110
|
```
|
99
111
|
|
100
|
-
#####
|
112
|
+
##### element method
|
101
113
|
|
102
114
|
```ruby
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
115
|
+
factory = <<-RUBY
|
116
|
+
array
|
117
|
+
objects.each do |test_object|
|
118
|
+
element :id, test_object.id
|
119
|
+
end
|
120
|
+
end
|
121
|
+
RUBY
|
107
122
|
|
108
|
-
|
109
|
-
|
110
|
-
|
123
|
+
# test data
|
124
|
+
test_object_1 = OpenStruct.new(id: 1)
|
125
|
+
test_object_2 = OpenStruct.new(id: 2)
|
111
126
|
|
112
|
-
puts JSONFactory.build(
|
113
|
-
```
|
127
|
+
puts JSONFactory.build(factory, objects: [test_object_1, test_object_2]) # => [{"id": 1},{"id":2}]
|
128
|
+
```
|
114
129
|
|
115
|
-
#####
|
130
|
+
##### partial method
|
116
131
|
|
117
132
|
```ruby
|
118
133
|
# tmp/_test_partial.jfactory
|
119
|
-
|
120
|
-
|
134
|
+
member :id, test_object.id
|
135
|
+
member :name, test_object.name
|
121
136
|
```
|
122
137
|
|
123
138
|
```ruby
|
124
139
|
# tmp/test.jfactory
|
125
|
-
|
126
|
-
|
140
|
+
object do
|
141
|
+
partial 'tmp/test_partial', test_object: object
|
127
142
|
end
|
128
143
|
```
|
129
144
|
|
@@ -134,16 +149,16 @@ test_object = OpenStruct.new(id: '1', name: 'TestObject1')
|
|
134
149
|
puts JSONFactory.build('tmp/test.jfactory', object: test_object).build # => { "id": 1, name: "TestObject1" }
|
135
150
|
```
|
136
151
|
|
137
|
-
#####
|
152
|
+
##### cache method
|
138
153
|
|
139
154
|
```ruby
|
140
155
|
factory = <<-RUBY
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
156
|
+
object do
|
157
|
+
member :data do
|
158
|
+
object do
|
159
|
+
cache 'test-cache-key' do
|
160
|
+
member :id, object.id
|
161
|
+
member :name, object.name
|
147
162
|
end
|
148
163
|
end
|
149
164
|
end
|
@@ -153,11 +168,56 @@ RUBY
|
|
153
168
|
# test data
|
154
169
|
test_object = OpenStruct.new(id: '1', name: 'TestObject1')
|
155
170
|
|
171
|
+
# set cache store
|
156
172
|
JSONFactory::Cache.instance.store = ActiveSupport::Cache::MemoryStore.new
|
157
173
|
|
158
174
|
puts JSONFactory.build(factory, object: test_object) # => { "data": { "id": "1", "name": "TestObject1" } }
|
159
175
|
```
|
160
176
|
|
177
|
+
##### object_if method
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
factory = <<-RUBY
|
181
|
+
object do
|
182
|
+
member :data do
|
183
|
+
object_if true do
|
184
|
+
member :foo, 'bar'
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
RUBY
|
189
|
+
|
190
|
+
puts JSONFactory.build(factory) # => { "data": { "foo": "bar" } }
|
191
|
+
```
|
192
|
+
```ruby
|
193
|
+
factory = <<-RUBY
|
194
|
+
object do
|
195
|
+
member :data do
|
196
|
+
object_if false do
|
197
|
+
member :foo, 'bar'
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
RUBY
|
202
|
+
|
203
|
+
puts JSONFactory.build(factory) # => { "data": null }
|
204
|
+
```
|
205
|
+
|
206
|
+
##### Load jfactory files
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
# tmp/test.jfactory
|
210
|
+
member :id, test_object.id
|
211
|
+
member :name, test_object.name
|
212
|
+
```
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
# test data
|
216
|
+
test_object = OpenStruct.new(id: '1', name: 'TestObject1')
|
217
|
+
|
218
|
+
puts JSONFactory.build('tmp/test.jfactory', object: test_object).build # => { "id": 1, name: "TestObject1" }
|
219
|
+
```
|
220
|
+
|
161
221
|
## Development
|
162
222
|
|
163
223
|
To install this gem onto your local machine, run `bundle exec rake install`.
|
data/lib/json_factory.rb
CHANGED
@@ -11,7 +11,6 @@ require_relative 'json_factory/errors'
|
|
11
11
|
require_relative 'json_factory/state'
|
12
12
|
require_relative 'json_factory/converter'
|
13
13
|
require_relative 'json_factory/dsl'
|
14
|
-
require_relative 'json_factory/dsl/object_array'
|
15
14
|
require_relative 'json_factory/template_store'
|
16
15
|
require_relative 'json_factory/json_builder'
|
17
16
|
require_relative 'json_factory/builder'
|
data/lib/json_factory/dsl.rb
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JSONFactory
|
4
|
+
class DSL
|
5
|
+
# Helper method to generate an object.
|
6
|
+
#
|
7
|
+
# json.object_if(true) do |id|
|
8
|
+
# json.member :foo, 'bar'
|
9
|
+
# end
|
10
|
+
# # generates: {"foo":"bar"}
|
11
|
+
#
|
12
|
+
# json.object_if(false) do |id|
|
13
|
+
# json.member :foo, 'bar'
|
14
|
+
# end
|
15
|
+
# # generates: null
|
16
|
+
def object_if(value, &block)
|
17
|
+
value ? object(&block) : value(nil)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -84,11 +84,12 @@ module JSONFactory
|
|
84
84
|
end
|
85
85
|
|
86
86
|
def evaluate(string, local_variables, filename)
|
87
|
-
|
87
|
+
dsl = DSL.new(self)
|
88
|
+
binding = jfactory(dsl)
|
88
89
|
local_variables.each_pair do |key, value|
|
89
90
|
binding.local_variable_set(key, value)
|
90
91
|
end
|
91
|
-
binding.local_variable_set(BUILDER_VARIABLE_NAME,
|
92
|
+
binding.local_variable_set(BUILDER_VARIABLE_NAME, dsl)
|
92
93
|
eval(string, binding, filename.to_s) # rubocop:disable Security/Eval
|
93
94
|
end
|
94
95
|
|
@@ -145,7 +146,7 @@ end
|
|
145
146
|
|
146
147
|
JSONFactory::JSONBuilder.class_eval do
|
147
148
|
# Returns an empty evaluation context, similar to Ruby's main object.
|
148
|
-
def jfactory
|
149
|
+
def jfactory(__dsl__)
|
149
150
|
Object.allocate.instance_eval do
|
150
151
|
class << self
|
151
152
|
JSONFactory.configure.helpers.each { |mod| include mod }
|
@@ -155,6 +156,19 @@ JSONFactory::JSONBuilder.class_eval do
|
|
155
156
|
end
|
156
157
|
alias inspect to_s
|
157
158
|
end
|
159
|
+
|
160
|
+
define_singleton_method(:__dsl__) do
|
161
|
+
__dsl__
|
162
|
+
end
|
163
|
+
|
164
|
+
def method_missing(method_name, *args, &block)
|
165
|
+
if __dsl__.respond_to?(method_name)
|
166
|
+
__dsl__.send(method_name, *args, &block)
|
167
|
+
else
|
168
|
+
super
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
158
172
|
return binding
|
159
173
|
end
|
160
174
|
end
|
data/lib/json_factory/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_factory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Klaiber
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -171,7 +171,6 @@ files:
|
|
171
171
|
- examples/benchmark.rb
|
172
172
|
- examples/fixtures/_test_partial.jfactory
|
173
173
|
- examples/fixtures/test.jfactory
|
174
|
-
- examples/readme_examples.rb
|
175
174
|
- json_factory.gemspec
|
176
175
|
- lib/json_factory.rb
|
177
176
|
- lib/json_factory/builder.rb
|
@@ -180,6 +179,7 @@ files:
|
|
180
179
|
- lib/json_factory/converter.rb
|
181
180
|
- lib/json_factory/dsl.rb
|
182
181
|
- lib/json_factory/dsl/object_array.rb
|
182
|
+
- lib/json_factory/dsl/object_if.rb
|
183
183
|
- lib/json_factory/errors.rb
|
184
184
|
- lib/json_factory/json_builder.rb
|
185
185
|
- lib/json_factory/railtie.rb
|
data/examples/readme_examples.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../lib/json_factory'
|
4
|
-
|
5
|
-
# test data
|
6
|
-
test_object1 = OpenStruct.new(id: '001', name: 'TestObject2')
|
7
|
-
test_object2 = OpenStruct.new(id: '002', name: 'TestObject3')
|
8
|
-
test_object = OpenStruct.new(id: '1', name: 'TestObject1', description: 'Test2', test_objects: [test_object1, test_object2])
|
9
|
-
|
10
|
-
factory = <<-RUBY
|
11
|
-
object! do
|
12
|
-
object!(:data) do
|
13
|
-
member!(:id, object.id)
|
14
|
-
member!(:name, object.name)
|
15
|
-
member!(:test_array) do
|
16
|
-
array!(object.test_objects) do |test_object|
|
17
|
-
member!(:id, test_object.id)
|
18
|
-
member!(:name, test_object.name)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
RUBY
|
24
|
-
|
25
|
-
# create context object
|
26
|
-
context = JSONFactory::Context.new(object: test_object)
|
27
|
-
|
28
|
-
puts JSONFactory::JSONBuilder.new(factory, context).build
|
29
|
-
|
30
|
-
factory = <<-RUBY
|
31
|
-
array! objects do |test_object|
|
32
|
-
member!(:id, test_object.id)
|
33
|
-
member!(:name, test_object.name)
|
34
|
-
end
|
35
|
-
RUBY
|
36
|
-
# create context object
|
37
|
-
context = JSONFactory::Context.new(objects: [test_object1, test_object2])
|
38
|
-
|
39
|
-
puts JSONFactory::JSONBuilder.new(factory, context).build
|