jsonrb 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +31 -0
- data/LICENSE +75 -0
- data/README.md +183 -0
- data/jsonrb.gemspec +38 -0
- data/lib/jsonrb.rb +12 -0
- data/lib/jsonrb/document.rb +97 -0
- data/lib/jsonrb/error.rb +6 -0
- data/lib/jsonrb/helpers.rb +19 -0
- data/lib/jsonrb/rake_task.rb +44 -0
- data/lib/jsonrb/version.rb +8 -0
- metadata +139 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e81155f65254eac24b7fc4ee33e8e175be1f5361a4c1b6329d2ad46d4dae3f05
|
4
|
+
data.tar.gz: 0307d64a8bc784ef9fff2df589002f922177812af8a678fb44851257ada0500e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c8cac0c9274aa62978653abf7f267825a6432fc9d96ae57dd2c4d89d3f40dcd7bb9d58f57539235487711c71179c3fd5d3ca5558395696146cec98d937e630b2
|
7
|
+
data.tar.gz: 1b37ca2d8753b336670922509738d16231289709a681639d2e79361a1cfb37ea245061c50f8b4a03c86f13c107d53d091ccca6ab4fb649ce43d879cd4996d76b
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
jsonrb (0.1.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
debase (0.2.2)
|
10
|
+
debase-ruby_core_source (>= 0.10.2)
|
11
|
+
debase-ruby_core_source (0.10.4)
|
12
|
+
minitest (5.11.3)
|
13
|
+
rake (10.5.0)
|
14
|
+
ruby-debug-ide (0.6.1)
|
15
|
+
rake (>= 0.8.1)
|
16
|
+
yard (0.9.16)
|
17
|
+
|
18
|
+
PLATFORMS
|
19
|
+
ruby
|
20
|
+
|
21
|
+
DEPENDENCIES
|
22
|
+
bundler (~> 1.17)
|
23
|
+
debase (~> 0.2)
|
24
|
+
jsonrb!
|
25
|
+
minitest (~> 5.11)
|
26
|
+
rake (~> 10.0)
|
27
|
+
ruby-debug-ide (~> 0.6)
|
28
|
+
yard (~> 0.9)
|
29
|
+
|
30
|
+
BUNDLED WITH
|
31
|
+
1.17.0
|
data/LICENSE
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
9
|
+
|
10
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
11
|
+
|
12
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
13
|
+
|
14
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
15
|
+
|
16
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
17
|
+
|
18
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
19
|
+
|
20
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
21
|
+
|
22
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
23
|
+
|
24
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
25
|
+
|
26
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
27
|
+
|
28
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
29
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
30
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
31
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
32
|
+
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
33
|
+
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
34
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
35
|
+
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
36
|
+
|
37
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
38
|
+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
39
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
40
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
41
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
42
|
+
END OF TERMS AND CONDITIONS
|
43
|
+
|
44
|
+
APPENDIX: How to apply the Apache License to your work.
|
45
|
+
|
46
|
+
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
47
|
+
|
48
|
+
Copyright 2019 Hi5 Development, LLC
|
49
|
+
|
50
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
51
|
+
you may not use this file except in compliance with the License.
|
52
|
+
You may obtain a copy of the License at
|
53
|
+
|
54
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
55
|
+
|
56
|
+
Unless required by applicable law or agreed to in writing, software
|
57
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
58
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
59
|
+
See the License for the specific language governing permissions and
|
60
|
+
limitations under the License.
|
61
|
+
|
62
|
+
Standard License Header
|
63
|
+
Copyright 2019 Hi5 Development, LLC
|
64
|
+
|
65
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
66
|
+
you may not use this file except in compliance with the License.
|
67
|
+
You may obtain a copy of the License at
|
68
|
+
|
69
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
70
|
+
|
71
|
+
Unless required by applicable law or agreed to in writing, software
|
72
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
73
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
74
|
+
See the License for the specific language governing permissions and
|
75
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
# JSONRB
|
2
|
+
|
3
|
+
Easily create complex JSON documents using Ruby templates.
|
4
|
+
|
5
|
+
I wrote this gem when working with the
|
6
|
+
[AWS Serverless Application Model](https://github.com/awslabs/serverless-application-model)
|
7
|
+
after the template I was working with grew to hundreds lines of code. I needed a way to easily split the template up
|
8
|
+
to smaller, easier to manage templates, and compile them into one only when it needed to be uploaded during deployment.
|
9
|
+
|
10
|
+
With this gem, you can create a folder that contains Ruby files that will be compiled into a single JSON document. It
|
11
|
+
includes a helper for building a Rake task to make it easy to build from the command line.
|
12
|
+
|
13
|
+
For example, let's say you're building an AWS SAM template with several resources. You could put each resource in a
|
14
|
+
separate file, and reference them in the main template, like this:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
{
|
18
|
+
AWSTemplateFormatVersion: '2010-09-09',
|
19
|
+
Transform: 'AWS::Serverless-2016-10-31',
|
20
|
+
|
21
|
+
Resources: merge(
|
22
|
+
render(:resources, :lambda_function),
|
23
|
+
render(:resources, :api_gateway),
|
24
|
+
render(:resources, :permissions),
|
25
|
+
render(:resources, :dynamodb_table)
|
26
|
+
)
|
27
|
+
}
|
28
|
+
```
|
29
|
+
|
30
|
+
The template contains Ruby code with the last expression as the output that gets added to the JSON document when it's
|
31
|
+
rendered.
|
32
|
+
|
33
|
+
Then you can set up a Rake task for building the JSON by adding this to your Rakefile:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require 'jsonrb/rake_task'
|
37
|
+
|
38
|
+
JSONRB::RakeTask.new(:build) do |t|
|
39
|
+
t.output_file = 'template.json'
|
40
|
+
t.template_name = 'aws_template'
|
41
|
+
t.template_path = 'config/templates'
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
Then you just have to run `rake build` to create the template.
|
46
|
+
|
47
|
+
## Installation
|
48
|
+
|
49
|
+
Add this line to your application's Gemfile:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
gem 'jsonrb'
|
53
|
+
```
|
54
|
+
|
55
|
+
And then execute:
|
56
|
+
|
57
|
+
$ bundle
|
58
|
+
|
59
|
+
Or install it yourself as:
|
60
|
+
|
61
|
+
$ gem install jsonrb
|
62
|
+
|
63
|
+
## Usage
|
64
|
+
|
65
|
+
Create a directory for your templates anywhere in your project.
|
66
|
+
|
67
|
+
Templates should be named using the dual file extension `.json.rb`. This is the default extension used when you don't
|
68
|
+
provide one to the `render` method inside your templates. You can change it if you need to when you set up the Rake
|
69
|
+
task, or in your code if you're building the documents manually.
|
70
|
+
|
71
|
+
The last expression in your template is what gets rendered to the document when you call `render`.
|
72
|
+
|
73
|
+
You can split Hashes into multiple templates using the `merge` helper method. In the above example, each resource
|
74
|
+
template outputs a Hash, like this:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
# File: resources/lambda_function.json.rb
|
78
|
+
{
|
79
|
+
LambdaFunction: {
|
80
|
+
Type: 'AWS::Serverless::Function',
|
81
|
+
# ...
|
82
|
+
}
|
83
|
+
}
|
84
|
+
```
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
# File: resources/api_gateway.json.rb
|
88
|
+
{
|
89
|
+
APIGateway: {
|
90
|
+
Type: 'AWS::Serverless::Api',
|
91
|
+
# ...
|
92
|
+
}
|
93
|
+
}
|
94
|
+
```
|
95
|
+
|
96
|
+
Which get combined together into a single Hash like this:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
{
|
100
|
+
LambdaFunction: {
|
101
|
+
Type: 'AWS::Serverless::Function',
|
102
|
+
# ...
|
103
|
+
},
|
104
|
+
APIGateway: {
|
105
|
+
Type: 'AWS::Serverless::Api',
|
106
|
+
# ...
|
107
|
+
}
|
108
|
+
}
|
109
|
+
```
|
110
|
+
|
111
|
+
### Using with Rake
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
require 'jsonrb/rake_task'
|
115
|
+
|
116
|
+
# The name of the task is the only argument the initializer requires. You can
|
117
|
+
# also pass a Hash if you prefer instead of using a block to configure the
|
118
|
+
# task.
|
119
|
+
JSONRB::RakeTask.new(:task_name) do |t|
|
120
|
+
## Required configuration.
|
121
|
+
|
122
|
+
# Full path to the JSON output file.
|
123
|
+
t.output_file = File.expand_path('template.json', __dir__)
|
124
|
+
|
125
|
+
# Which template to render when calling the task.
|
126
|
+
t.template_name = 'aws_template'
|
127
|
+
|
128
|
+
# Full path to the directory with the .json.rb templates.
|
129
|
+
t.template_path = File.expand_path('config/templates', __dir__)
|
130
|
+
|
131
|
+
## Optional configuration - values below are the defaults.
|
132
|
+
|
133
|
+
# When +true+ the JSON output will contain line feeds and indentation.
|
134
|
+
t.pretty = false
|
135
|
+
|
136
|
+
# Description of the Rake task - useful when creating multiple tasks.
|
137
|
+
t.description = 'Builds the JSON template'
|
138
|
+
|
139
|
+
# The file extension used when one is not provided to the #render method.
|
140
|
+
t.default_file_ext = '.json.rb'
|
141
|
+
|
142
|
+
## Advanced configuration.
|
143
|
+
|
144
|
+
# You can also add a module with custom helper methods. This only works with
|
145
|
+
# block configuration.
|
146
|
+
t.extend(CustomHelpers)
|
147
|
+
end
|
148
|
+
```
|
149
|
+
|
150
|
+
### Using without Rake
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
require 'jsonrb'
|
154
|
+
|
155
|
+
# Full path to the directory with the templates.
|
156
|
+
template_path = File.expand_path('config/templates', __dir__)
|
157
|
+
|
158
|
+
# Full path to save the JSON file.
|
159
|
+
output_file = File.expand_path('template.json', __dir__)
|
160
|
+
|
161
|
+
# The template path is the only required argument. You can also provide
|
162
|
+
# one of these optional arguments:
|
163
|
+
#
|
164
|
+
# default_file_ext - Defaults to '.json.rb'
|
165
|
+
# pretty - Defaults to false.
|
166
|
+
document = JSONRB::Document.new(template_path)
|
167
|
+
|
168
|
+
# Render the entry-point template.
|
169
|
+
document.render('template_name')
|
170
|
+
|
171
|
+
# Create the JSON file.
|
172
|
+
document.save(output_file)
|
173
|
+
```
|
174
|
+
|
175
|
+
## Development
|
176
|
+
|
177
|
+
This project is configured to use [Docker Compose](https://docs.docker.com/compose/) for development. It is set up for
|
178
|
+
[Alpine Linux](https://alpinelinux.org/), which uses [Almquist Shell](https://en.wikipedia.org/wiki/Almquist_shell)
|
179
|
+
instead of Bash, so if you need to open a shell run `docker-compose run ruby ash`.
|
180
|
+
|
181
|
+
## Contributing
|
182
|
+
|
183
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hi5dev/jsonrb.
|
data/jsonrb.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
ROOT_PATH = File.expand_path(__dir__)
|
4
|
+
|
5
|
+
# Add the lib folder to the Ruby load path.
|
6
|
+
($LOAD_PATH << File.join(ROOT_PATH, 'lib')).uniq!
|
7
|
+
|
8
|
+
require 'jsonrb/version'
|
9
|
+
|
10
|
+
Gem::Specification.new do |spec|
|
11
|
+
spec.name = 'jsonrb'
|
12
|
+
spec.version = JSONRB::VERSION
|
13
|
+
spec.authors = ['Travis Haynes']
|
14
|
+
spec.email = ['travis@hi5dev.com']
|
15
|
+
spec.licenses = ['Apache-2.0']
|
16
|
+
|
17
|
+
spec.summary = 'Build JSON from multiple Ruby sources that output a Hash or Array.'
|
18
|
+
spec.homepage = 'https://github.com/hi5dev/jsonrb'
|
19
|
+
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.files = Dir[File.join(ROOT_PATH, 'lib', '**', '*.rb')].map do |file|
|
23
|
+
file[ROOT_PATH.length + 1..-1]
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files << 'Gemfile'
|
27
|
+
spec.files << 'Gemfile.lock'
|
28
|
+
spec.files << 'jsonrb.gemspec'
|
29
|
+
spec.files << 'LICENSE'
|
30
|
+
spec.files << 'README.md'
|
31
|
+
|
32
|
+
spec.add_development_dependency 'bundler', '~> 1.17'
|
33
|
+
spec.add_development_dependency 'debase', '~> 0.2'
|
34
|
+
spec.add_development_dependency 'minitest', '~> 5.11'
|
35
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
36
|
+
spec.add_development_dependency 'ruby-debug-ide', '~> 0.6'
|
37
|
+
spec.add_development_dependency 'yard', '~> 0.9'
|
38
|
+
end
|
data/lib/jsonrb.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'pathname'
|
5
|
+
|
6
|
+
module JSONRB
|
7
|
+
autoload :Document, 'jsonrb/document'
|
8
|
+
autoload :Error, 'jsonrb/error'
|
9
|
+
autoload :Helpers, 'jsonrb/helpers'
|
10
|
+
autoload :RakeTask, 'jsonrb/rake_task'
|
11
|
+
autoload :VERSION, 'jsonrb/version'
|
12
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JSONRB
|
4
|
+
# Creates a new JSON document from Ruby files in a given path.
|
5
|
+
#
|
6
|
+
# @!attribute default_file_ext
|
7
|
+
# The default file extension to use when none is provided to the #render. Set to +nil+, +false+, or an empty String
|
8
|
+
# to disable automatically adding file extensions.
|
9
|
+
# @return [String]
|
10
|
+
#
|
11
|
+
# @!attribute output
|
12
|
+
# The Ruby object that will be serialized to JSON. This is updated when you call #render. It is not initialized by
|
13
|
+
# default, and should only be modified if you can't accomplish what you need to using the #render method. For
|
14
|
+
# example, if you need to see what's in the document so far before you add anything else to it.
|
15
|
+
# @return [Object]
|
16
|
+
#
|
17
|
+
# @!attribute path
|
18
|
+
# The full path to the template files.
|
19
|
+
# @return [Pathname]
|
20
|
+
#
|
21
|
+
# @!attribute pretty
|
22
|
+
# When this is +true+, the generated JSON document will contain line feeds and indentation.
|
23
|
+
# @return [true | false]
|
24
|
+
class Document
|
25
|
+
include JSONRB::Helpers
|
26
|
+
|
27
|
+
attr_accessor :default_file_ext, :pretty
|
28
|
+
|
29
|
+
attr_reader :output, :path
|
30
|
+
|
31
|
+
# @param [String] path Full path to the templates.
|
32
|
+
# @param [String] default_file_ext The default file extension to use when none is provided in the template names.
|
33
|
+
# @param [true | false] pretty When +true+ the document will be formatted to be easy to read.
|
34
|
+
# @return [JSONRB::Document]
|
35
|
+
def initialize(path, default_file_ext: '.json.rb', pretty: false)
|
36
|
+
@default_file_ext = default_file_ext
|
37
|
+
@output = {}
|
38
|
+
@path = Pathname.new(path)
|
39
|
+
@pretty = pretty
|
40
|
+
end
|
41
|
+
|
42
|
+
# Gets the full path for the template with the given name.
|
43
|
+
#
|
44
|
+
# @example
|
45
|
+
# document = JSONRB::Document.new('/usr/src/app/templates')
|
46
|
+
# document.full_path(:resources, :a) # => '/usr/src/app/templates/resources/a.json.rb'
|
47
|
+
#
|
48
|
+
# @param [*String | *Symbol] name Name of the template with or without an extension.
|
49
|
+
# @return [String] Full path to the template.
|
50
|
+
def full_path(*name)
|
51
|
+
# @type [Array<String>] Pathname#join only allows Strings
|
52
|
+
rel_path = name.map(&:to_s)
|
53
|
+
|
54
|
+
# @type [Pathname] Expand the relative path based on the template path provided to the initializer.
|
55
|
+
fp = path.join(*rel_path).to_s
|
56
|
+
|
57
|
+
# No more processing required if the user provided the file extension in the name.
|
58
|
+
return fp unless File.extname(fp).empty?
|
59
|
+
|
60
|
+
# No more processing required when a file extension was not provided, and the default file extension is disabled.
|
61
|
+
return fp if !default_file_ext || default_file_ext.empty?
|
62
|
+
|
63
|
+
# Only add a period if the file doesn't end with a period, or the extension doesn't begin with one.
|
64
|
+
sep = default_file_ext[0] == '.' || fp[-1] == '.' ? '' : '.'
|
65
|
+
|
66
|
+
# Add the default file extension.
|
67
|
+
[fp, default_file_ext].join(sep)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Renders the template partial at the given path.
|
71
|
+
#
|
72
|
+
# @param [*String] partial_path Path relative to the path given when initializing the Document.
|
73
|
+
# @return [Array | Hash]
|
74
|
+
def render(*partial_path)
|
75
|
+
template_path = full_path(*partial_path)
|
76
|
+
|
77
|
+
code = File.read(template_path)
|
78
|
+
|
79
|
+
@output = eval(code, binding, template_path.to_s)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Saves the document.
|
83
|
+
#
|
84
|
+
# @param [Pathname | String] full_path
|
85
|
+
# @return [String]
|
86
|
+
def save(full_path)
|
87
|
+
File.write(full_path, to_json)
|
88
|
+
end
|
89
|
+
|
90
|
+
# Creates the JSON for the rendered templates.
|
91
|
+
#
|
92
|
+
# @return [String]
|
93
|
+
def to_json
|
94
|
+
pretty ? JSON.pretty_generate(output) : output.to_json
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/lib/jsonrb/error.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JSONRB
|
4
|
+
module Helpers
|
5
|
+
# Merges multiple hashes together.
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# one = { one: 1 }
|
9
|
+
# two = { two: 2 }
|
10
|
+
# three = { three: 3 }
|
11
|
+
# merge(one, two, three) # => { one: 1, two: 2, three: 3 }
|
12
|
+
#
|
13
|
+
# @param [*Hash] hashes All of the hashes to merge.
|
14
|
+
# @return [Hash] Merged hashes.
|
15
|
+
def merge(*hashes)
|
16
|
+
hashes.inject({}, &:merge!)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rake'
|
4
|
+
require 'rake/tasklib'
|
5
|
+
|
6
|
+
module JSONRB
|
7
|
+
class RakeTask
|
8
|
+
attr_reader :name
|
9
|
+
attr_accessor :default_file_ext, :description, :output_file, :pretty, :template_name, :template_path
|
10
|
+
|
11
|
+
DESCRIPTION = 'Builds JSON template'
|
12
|
+
|
13
|
+
# @yieldparam [JSONRB::RakeTask] config
|
14
|
+
def initialize(name, options = {})
|
15
|
+
@name = name
|
16
|
+
|
17
|
+
@default_file_ext = options.fetch(:default_file_ext, '.json.rb')
|
18
|
+
@description = options.fetch(:description, DESCRIPTION)
|
19
|
+
@output_file = options.fetch(:output_file, nil)
|
20
|
+
@pretty = options.fetch(:pretty, false)
|
21
|
+
@template_name = options.fetch(:template_name, nil)
|
22
|
+
@template_path = options.fetch(:template_path, nil)
|
23
|
+
|
24
|
+
yield(self) if block_given?
|
25
|
+
|
26
|
+
raise ArgumentError, "invalid template_path: #{template_path.inspect}" unless File.directory?(template_path)
|
27
|
+
raise ArgumentError, 'template_name cannot be blank' if template_name.nil? || template_name.empty?
|
28
|
+
raise ArgumentError, 'output_file cannot be blank' if output_file.nil? || output_file.empty?
|
29
|
+
|
30
|
+
define
|
31
|
+
end
|
32
|
+
|
33
|
+
def define
|
34
|
+
desc(description)
|
35
|
+
task(name, &method(:execute))
|
36
|
+
end
|
37
|
+
|
38
|
+
def execute
|
39
|
+
document = JSONRB::Document.new(template_path, default_file_ext: default_file_ext, pretty: pretty)
|
40
|
+
document.render(*Array(template_name))
|
41
|
+
document.save(output_file)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsonrb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Travis Haynes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: debase
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.2'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.11'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.11'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: ruby-debug-ide
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.6'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.6'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: yard
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.9'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.9'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- travis@hi5dev.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- Gemfile
|
105
|
+
- Gemfile.lock
|
106
|
+
- LICENSE
|
107
|
+
- README.md
|
108
|
+
- jsonrb.gemspec
|
109
|
+
- lib/jsonrb.rb
|
110
|
+
- lib/jsonrb/document.rb
|
111
|
+
- lib/jsonrb/error.rb
|
112
|
+
- lib/jsonrb/helpers.rb
|
113
|
+
- lib/jsonrb/rake_task.rb
|
114
|
+
- lib/jsonrb/version.rb
|
115
|
+
homepage: https://github.com/hi5dev/jsonrb
|
116
|
+
licenses:
|
117
|
+
- Apache-2.0
|
118
|
+
metadata: {}
|
119
|
+
post_install_message:
|
120
|
+
rdoc_options: []
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
requirements: []
|
134
|
+
rubyforge_project:
|
135
|
+
rubygems_version: 2.7.8
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: Build JSON from multiple Ruby sources that output a Hash or Array.
|
139
|
+
test_files: []
|