json_factory 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4c49c73f5c727e2f83caad602c203bd3a4003961
4
+ data.tar.gz: 1b589f2f5ac1cb970f65cf741d6b5b7fa6013b16
5
+ SHA512:
6
+ metadata.gz: 04a4f6a100f963652dd97bfc74bdfab4c7e8ddd9c414422cf43767a44f9557178a93344e94958f64c7d6a356d362f4801fb4e32bcd25cb25dc272f4eb943ddcd
7
+ data.tar.gz: 77002b8c955a39e6cd19635ec0e124f745bcef1cf546976b980d464af860fc1e32e04df61a0d193a86007bac76e006317ac37d6224481dc1f214c788a8c18947
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea
@@ -0,0 +1,33 @@
1
+ # Use this file to configure the Overcommit hooks you wish to use. This will
2
+ # extend the default configuration defined in:
3
+ # https://github.com/brigade/overcommit/blob/master/config/default.yml
4
+ #
5
+ # At the topmost level of this YAML file is a key representing type of hook
6
+ # being run (e.g. pre-commit, commit-msg, etc.). Within each type you can
7
+ # customize each hook, such as whether to only run it on certain files (via
8
+ # `include`), whether to only display output if it fails (via `quiet`), etc.
9
+ #
10
+ # For a complete list of hooks, see:
11
+ # https://github.com/brigade/overcommit/tree/master/lib/overcommit/hook
12
+ #
13
+ # For a complete list of options that you can use to customize hooks, see:
14
+ # https://github.com/brigade/overcommit#configuration
15
+ #
16
+ # Uncomment the following lines to make the configuration take effect.
17
+
18
+ PreCommit:
19
+ RuboCop:
20
+ enabled: true
21
+ on_warn: fail # Treat all warnings as failures
22
+ #
23
+ # TrailingWhitespace:
24
+ # enabled: true
25
+ # exclude:
26
+ # - '**/db/structure.sql' # Ignore trailing whitespace in generated files
27
+ #
28
+ #PostCheckout:
29
+ # ALL: # Special hook name that customizes all hooks of this type
30
+ # quiet: true # Change all post-checkout hooks to only display output on failure
31
+ #
32
+ # IndexTags:
33
+ # enabled: true # Generate a tags file with `ctags` each time HEAD changes
@@ -0,0 +1,32 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+
4
+ Style/Documentation:
5
+ Enabled: false
6
+
7
+ Style/MethodMissing:
8
+ Exclude:
9
+ - lib/json_factory/json_builder.rb
10
+
11
+ Style/ClassVars:
12
+ Exclude:
13
+ - lib/json_factory/json_builder.rb
14
+
15
+ Metrics/LineLength:
16
+ Max: 140
17
+ Exclude:
18
+ - spec/**/*
19
+
20
+ Metrics/MethodLength:
21
+ Max: 20
22
+
23
+ Metrics/BlockLength:
24
+ Exclude:
25
+ - spec/**/*
26
+ - examples/**/*
27
+
28
+ Lint/ShadowingOuterLocalVariable:
29
+ Exclude:
30
+ - spec/**/*
31
+
32
+
@@ -0,0 +1 @@
1
+ 2.4.1
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.4.1
4
+ services:
5
+ - redis-server
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at alex.klaiber@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ group :development do
8
+ gem 'rubocop', require: false
9
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Alexander Klaiber
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,181 @@
1
+ [![Build Status](https://travis-ci.org/aklaiber/json_factory.svg?branch=master)](https://travis-ci.org/aklaiber/json_factory)
2
+
3
+ # JsonFactory
4
+
5
+ JsonFactory is a Easy DSL to create JSON structures with the development focus on performance.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'json_factory'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install json_factory
22
+
23
+ ## Usage
24
+
25
+ | DSL Method | Description |
26
+ | ------------- |:-------------------------------- |
27
+ | json.object! | Create a json object |
28
+ | json.array! | Create a json array |
29
+ | json.member! | Create key value pair |
30
+ | json.null! | Set object to "null" |
31
+ | json.partial! | Load sub jfactory file |
32
+ | json.cache! | Read and write from cache stores |
33
+
34
+ ##### Top level object JSON structure
35
+
36
+ ```ruby
37
+ factory = <<-RUBY
38
+ json.object! do |json|
39
+ json.object!(:data) do |json|
40
+ json.member!(:id, object.id)
41
+ json.member!(:name, object.name)
42
+
43
+ json.array!(object.test_objects, :test_array) do |json, test_object|
44
+ json.member!(:id, test_object.id)
45
+ json.member!(:name, test_object.name)
46
+ end
47
+ end
48
+ end
49
+ RUBY
50
+
51
+ # test data
52
+ test_object_1 = OpenStruct.new(id: '001', name: 'TestObject2')
53
+ test_object_2 = OpenStruct.new(id: '002', name: 'TestObject3')
54
+ test_object = OpenStruct.new(id: '1', name: 'TestObject1', test_objects: [test_object_1, test_object_2])
55
+
56
+ # create context object
57
+ context = JSONFactory::Context.new(object: test_object)
58
+
59
+ puts JSONFactory::JSONBuilder.new(factory, context).build
60
+ ```
61
+
62
+ ```json
63
+ {
64
+ "data": {
65
+ "id": "1",
66
+ "name": "TestObject1",
67
+ "test_array": [
68
+ { "id": "001", "name": "TestObject2" },
69
+ { "id": "002", "name": "TestObject3" }
70
+ ]
71
+ }
72
+ }
73
+ ```
74
+
75
+ ##### Top level array JSON structure
76
+
77
+ ```ruby
78
+ factory = <<-RUBY
79
+ json.array! objects do |json, test_object|
80
+ json.member!(:id, test_object.id)
81
+ json.member!(:name, test_object.name)
82
+ end
83
+ RUBY
84
+
85
+ # test data
86
+ test_object_1 = OpenStruct.new(id: '001', name: 'TestObject2')
87
+ test_object_2 = OpenStruct.new(id: '002', name: 'TestObject3')
88
+
89
+ # create context object
90
+ context = JSONFactory::Context.new(objects: [test_object_1, test_object_2])
91
+
92
+ puts JSONFactory::JSONBuilder.new(factory, context).build
93
+ ```
94
+
95
+ ```json
96
+ [
97
+ { "id": "001", "name": "TestObject2" },
98
+ { "id": "002", "name": "TestObject3" }
99
+ ]
100
+ ```
101
+
102
+ ##### Load jfactory files
103
+
104
+ ```ruby
105
+ # tmp/test.jfactory
106
+ json.member!(:id, test_object.id)
107
+ json.member!(:name, test_object.name)
108
+ ```
109
+
110
+ ```ruby
111
+ # test data
112
+ test_object = OpenStruct.new(id: '1', name: 'TestObject1')
113
+
114
+ # create context object
115
+ context = JSONFactory::Context.new(object: test_object)
116
+
117
+ puts JSONFactory::JSONBuilder.load_factory_file('tmp/test.jfactory', context).build # => { "id": 1, name: "TestObject1" }
118
+ ```
119
+
120
+ ##### Load partials
121
+
122
+ ```ruby
123
+ # tmp/_test_partial.jfactory
124
+ json.member!(:id, test_object.id)
125
+ json.member!(:name, test_object.name)
126
+ ```
127
+
128
+ ```ruby
129
+ # tmp/test.jfactory
130
+ json.object! do |json|
131
+ json.partial!('tmp/test_partial', test_object: object)
132
+ end
133
+ ```
134
+
135
+ ```ruby
136
+ # test data
137
+ test_object = OpenStruct.new(id: '1', name: 'TestObject1')
138
+
139
+ # create context object
140
+ context = JSONFactory::Context.new(object: test_object)
141
+
142
+ puts JSONFactory::JSONBuilder.load_factory_file('tmp/test.jfactory', context).build # => { "id": 1, name: "TestObject1" }
143
+ ```
144
+
145
+ ##### Use cache stores
146
+
147
+ ```ruby
148
+ factory = <<-RUBY
149
+ json.object! do |json|
150
+ json.object!(:data) do |json|
151
+ json.cache! 'test-cache-key' do |json|
152
+ json.member!(:id, object.id)
153
+ json.member!(:name, object.name)
154
+ end
155
+ end
156
+ end
157
+ RUBY
158
+
159
+ # test data
160
+ test_object = OpenStruct.new(id: '1', name: 'TestObject1')
161
+
162
+ # create context object
163
+ context = JSONFactory::Context.new(object: test_object)
164
+
165
+ builder = JSONFactory::JSONBuilder.new(factory, context)
166
+ builder.cache.store = ActiveSupport::Cache::MemoryStore.new
167
+ puts builder.build # => { "data": { "id": "1", "name": "TestObject1" } }
168
+ ```
169
+
170
+ ## Development
171
+
172
+ To install this gem onto your local machine, run `bundle exec rake install`.
173
+
174
+ ## Contributing
175
+
176
+ Bug reports and pull requests are welcome on GitHub at https://github.com/aklaiber/json_factory. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
177
+
178
+ ## License
179
+
180
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
181
+
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'json_factory'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jbuilder'
4
+ require 'benchmark'
5
+ require 'forgery'
6
+
7
+ require_relative '../lib/json_factory'
8
+
9
+ objects = []
10
+ sub_array = []
11
+
12
+ 10.times do
13
+ sub_array << OpenStruct.new(
14
+ id: rand(1_000_000_000),
15
+ name: 'TestObject1',
16
+ description: 'Test1',
17
+ test_array: sub_array
18
+ )
19
+ end
20
+
21
+ 1000.times do |i|
22
+ objects << OpenStruct.new(
23
+ id: i,
24
+ name: 'TestObject1',
25
+ description: 'Test1',
26
+ test_array: sub_array
27
+ )
28
+ end
29
+
30
+ # CURRENT RUNTIME WITH 1000 and 10 = 0.133410
31
+ Benchmark.bmbm(15) do |x|
32
+ x.report(:json_factory) do
33
+ builder = JSONFactory::JSONBuilder.load_factory_file('fixtures/test.jfactory')
34
+ builder.build(JSONFactory::Context.new(objects: objects))
35
+ end
36
+ end
@@ -0,0 +1 @@
1
+ json.member!(:id, context.id)
@@ -0,0 +1,8 @@
1
+ json.array! objects do |json, test_object|
2
+ json.member!(:id, test_object.id)
3
+ json.member!(:name, test_object.name)
4
+ json.member!(:description, test_object.description)
5
+ json.array!(test_object.test_array, :test_array) do |json, test_sub_object|
6
+ json.partial! 'fixtures/test_partial', id: test_sub_object.id
7
+ end
8
+ end
@@ -0,0 +1,38 @@
1
+ require_relative '../lib/json_factory'
2
+
3
+ # test data
4
+ test_object_1 = OpenStruct.new(id: '001', name: 'TestObject2')
5
+ test_object_2 = OpenStruct.new(id: '002', name: 'TestObject3')
6
+ test_object = OpenStruct.new(id: '1', name: 'TestObject1', description: 'Test2', test_objects: [test_object_1, test_object_2])
7
+
8
+ factory = <<-RUBY
9
+ json.object! do |json|
10
+ json.object!(:data) do |json|
11
+ json.member!(:id, object.id)
12
+ json.member!(:name, object.name)
13
+
14
+ json.array!(object.test_objects, :test_array) do |json, test_object|
15
+ json.member!(:id, test_object.id)
16
+ json.member!(:name, test_object.name)
17
+ end
18
+ end
19
+ end
20
+ RUBY
21
+
22
+ # create context object
23
+ context = JSONFactory::Context.new(object: test_object)
24
+
25
+ puts JSONFactory::JSONBuilder.new(factory, context).build
26
+
27
+
28
+
29
+ factory = <<-RUBY
30
+ json.array! objects do |json, test_object|
31
+ json.member!(:id, test_object.id)
32
+ json.member!(:name, test_object.name)
33
+ end
34
+ RUBY
35
+ # create context object
36
+ context = JSONFactory::Context.new(objects: [test_object_1, test_object_2])
37
+
38
+ puts JSONFactory::JSONBuilder.new(factory, context).build
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'json_factory/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'json_factory'
9
+ spec.version = JSONFactory::VERSION
10
+ spec.authors = ['Alexander Klaiber']
11
+ spec.email = ['alex.klaiber@gmail.com']
12
+
13
+ spec.summary = 'JsonFactory is a Easy DSL to create JSON structures with the development focus on performance.'
14
+ spec.description = 'JsonFactory is a Easy DSL to create JSON structures with the development focus on performance.'
15
+ spec.homepage = 'https://github.com/aklaiber/json_factory'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'activesupport', '>= 5.1.0'
22
+ spec.add_runtime_dependency 'oj'
23
+ spec.add_runtime_dependency 'redis-activesupport', '>= 5.0.0'
24
+
25
+ spec.add_development_dependency 'bundler'
26
+ spec.add_development_dependency 'colorize'
27
+ spec.add_development_dependency 'forgery'
28
+ spec.add_development_dependency 'jbuilder'
29
+ spec.add_development_dependency 'json-schema'
30
+ spec.add_development_dependency 'rake'
31
+ spec.add_development_dependency 'rspec'
32
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'oj'
4
+
5
+ require 'active_support'
6
+ require 'active_support/cache/redis_store'
7
+
8
+ require_relative 'json_factory/version'
9
+ require_relative 'json_factory/context'
10
+ require_relative 'json_factory/json_builder'
11
+ require_relative 'json_factory/json_object'
12
+
13
+ module JSONFactory
14
+ autoload :Cache, 'json_factory/cache.rb'
15
+ autoload :CacheError, 'json_factory/cache_error.rb'
16
+ autoload :CacheStoreProxy, 'json_factory/cache_store_proxy.rb'
17
+ autoload :BaseStoreProxy, 'json_factory/cache_store_proxy/base_store_proxy.rb'
18
+ autoload :RedisStoreProxy, 'json_factory/cache_store_proxy/redis_store_proxy.rb'
19
+ autoload :MemoryStoreProxy, 'json_factory/cache_store_proxy/memory_store_proxy.rb'
20
+ autoload :FileStoreProxy, 'json_factory/cache_store_proxy/file_store_proxy.rb'
21
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONFactory
4
+ class Cache
5
+ include Singleton
6
+
7
+ attr_accessor :prefix
8
+
9
+ def initialize
10
+ @prefix = 'json_factory'
11
+
12
+ self.store = ::Rails.cache if defined?(::Rails.cache)
13
+ end
14
+
15
+ attr_reader :store
16
+ alias store_proxy store
17
+
18
+ def store=(store)
19
+ @store = CacheStoreProxy.build(store)
20
+ end
21
+
22
+ delegate :read, to: :store
23
+ delegate :write, to: :store
24
+ delegate :delete, to: :store
25
+ end
26
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONFactory
4
+ class CacheError < StandardError
5
+ end
6
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONFactory
4
+ module CacheStoreProxy
5
+ PROXIES = [RedisStoreProxy, MemoryStoreProxy, FileStoreProxy].freeze
6
+
7
+ def self.build(store)
8
+ store_proxy = PROXIES.find { |proxy| proxy.handle?(store) }
9
+ fail CacheError, "#{store.class} is a not supported cache" if store_proxy.nil?
10
+ store_proxy.new(store)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONFactory
4
+ module CacheStoreProxy
5
+ class BaseStoreProxy
6
+ attr_reader :store
7
+
8
+ def self.handle?(store)
9
+ defined?(self::STORE_CLASS) && store.is_a?(self::STORE_CLASS)
10
+ end
11
+
12
+ def initialize(store, prefix = 'json_factory')
13
+ @store = store
14
+ @prefix = prefix
15
+ end
16
+
17
+ def read(key, options = nil)
18
+ store.read("#{@prefix}:#{key}", options)
19
+ end
20
+
21
+ def write(key, value, options = nil)
22
+ store.write("#{@prefix}:#{key}", value, options)
23
+ end
24
+
25
+ def delete(key, options = nil)
26
+ store.delete("#{@prefix}:#{key}", options)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONFactory
4
+ module CacheStoreProxy
5
+ class FileStoreProxy < BaseStoreProxy
6
+ STORE_CLASS = ActiveSupport::Cache::FileStore.freeze
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONFactory
4
+ module CacheStoreProxy
5
+ class MemoryStoreProxy < BaseStoreProxy
6
+ STORE_CLASS = ActiveSupport::Cache::MemoryStore.freeze
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONFactory
4
+ module CacheStoreProxy
5
+ class RedisStoreProxy < BaseStoreProxy
6
+ STORE_CLASS = ActiveSupport::Cache::RedisStore.freeze
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONFactory
4
+ class Context
5
+ def initialize(data = nil)
6
+ @data = data
7
+ end
8
+
9
+ def add(key, value)
10
+ @data[key] = value
11
+ end
12
+
13
+ def method_missing(method_name, *arguments, &block)
14
+ return @data[method_name] if @data.is_a?(Hash) && @data.key?(method_name)
15
+ super
16
+ end
17
+
18
+ def respond_to_missing?(method_name, include_private = false)
19
+ (@data.is_a?(Hash) && @data.key?(method_name)) || super
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONFactory
4
+ class JSONBuilder
5
+ attr_accessor :output
6
+ attr_reader :stream, :factory, :cache
7
+
8
+ def self.load_factory_file(path, context = nil)
9
+ raise "file format is invalid. #{path}" unless File.extname(path).eql?('.jfactory')
10
+ raise "jfactory file #{path} not found" unless File.exist?(path)
11
+ new(File.open(path).read, context)
12
+ end
13
+
14
+ def initialize(factory, context = nil)
15
+ @factory = factory
16
+ @cache = Cache.instance
17
+ @context = context
18
+ end
19
+
20
+ def build
21
+ @output = StringIO.new
22
+ @stream = Oj::StreamWriter.new(@output, indent: 0)
23
+
24
+ JSONObject.new(self, @context).schema! { |json|
25
+ json.instance_eval(@factory)
26
+ }
27
+
28
+ @output.string.delete("\n").gsub('null}', 'null') if @output
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,86 @@
1
+ module JSONFactory
2
+ class JSONObject
3
+ attr_reader :json_builder
4
+ attr_accessor :context
5
+
6
+ @@partials = {}
7
+
8
+ def initialize(json_builder, context = nil)
9
+ @json_builder = json_builder
10
+ @context = context
11
+ end
12
+
13
+ def open_object
14
+ json_builder.stream.push_object
15
+ end
16
+
17
+ def close_object
18
+ json_builder.stream.pop
19
+ end
20
+
21
+ def schema!(object = nil)
22
+ yield self, object
23
+ self
24
+ end
25
+
26
+ def object!(key = nil, &block)
27
+ json_object = JSONObject.new(json_builder)
28
+
29
+ json_builder.stream.push_key(key.to_s) if key
30
+
31
+ json_object.open_object
32
+ json_object.schema!(&block)
33
+ json_object.close_object
34
+ end
35
+
36
+ def array!(array, key = nil, &block)
37
+ json_builder.stream.push_array(key ? key.to_s : nil)
38
+ array.each do |object|
39
+ builder = JSONObject.new(json_builder)
40
+ builder.open_object
41
+ builder.schema!(object, &block)
42
+ builder.close_object
43
+ end
44
+ json_builder.stream.pop
45
+ end
46
+
47
+ def member!(key, value = nil)
48
+ json_builder.stream.push_value(value, key.to_s)
49
+ end
50
+
51
+ def null!
52
+ json_builder.output.seek(json_builder.output.pos - 1)
53
+ json_builder.output.puts('null')
54
+ end
55
+
56
+ def cache!(cache_key, &block)
57
+ json = json_builder.cache.read(cache_key) if cache_key && json_builder.cache
58
+ return json_builder.output.puts(json) unless json.blank?
59
+
60
+ start_cache_pos = json_builder.output.pos
61
+ self.schema!(&block)
62
+ end_cache_pos = json_builder.output.pos
63
+
64
+ json_builder.cache.write(cache_key, json_builder.output.string[start_cache_pos..end_cache_pos])
65
+ end
66
+
67
+ def partial!(path, data = {})
68
+ path = Pathname.new(path)
69
+
70
+ if path.extname.eql?('.jfactory')
71
+ @@partials[path] ||= File.open(path).read
72
+ else
73
+ @@partials[path] ||= File.open("#{path.dirname}/_#{path.basename}.jfactory").read
74
+ end
75
+
76
+ JSONObject.new(json_builder, JSONFactory::Context.new(data)).schema! do |json|
77
+ json.instance_eval(@@partials[path])
78
+ end
79
+ end
80
+
81
+ def method_missing(method_name, *arguments, &block)
82
+ return context.send(method_name) if context.respond_to?(method_name)
83
+ super
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONFactory
4
+ VERSION = '0.3.0'
5
+ end
metadata ADDED
@@ -0,0 +1,215 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_factory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Klaiber
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: oj
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: redis-activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 5.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 5.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: colorize
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: forgery
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: jbuilder
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: json-schema
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rake
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: JsonFactory is a Easy DSL to create JSON structures with the development
154
+ focus on performance.
155
+ email:
156
+ - alex.klaiber@gmail.com
157
+ executables: []
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - ".gitignore"
162
+ - ".overcommit.yml"
163
+ - ".rubocop.yml"
164
+ - ".ruby-version"
165
+ - ".travis.yml"
166
+ - CODE_OF_CONDUCT.md
167
+ - Gemfile
168
+ - LICENSE.txt
169
+ - README.md
170
+ - Rakefile
171
+ - bin/console
172
+ - bin/setup
173
+ - examples/benchmark.rb
174
+ - examples/fixtures/_test_partial.jfactory
175
+ - examples/fixtures/test.jfactory
176
+ - examples/readme_examples.rb
177
+ - json_factory.gemspec
178
+ - lib/json_factory.rb
179
+ - lib/json_factory/cache.rb
180
+ - lib/json_factory/cache_error.rb
181
+ - lib/json_factory/cache_store_proxy.rb
182
+ - lib/json_factory/cache_store_proxy/base_store_proxy.rb
183
+ - lib/json_factory/cache_store_proxy/file_store_proxy.rb
184
+ - lib/json_factory/cache_store_proxy/memory_store_proxy.rb
185
+ - lib/json_factory/cache_store_proxy/redis_store_proxy.rb
186
+ - lib/json_factory/context.rb
187
+ - lib/json_factory/json_builder.rb
188
+ - lib/json_factory/json_object.rb
189
+ - lib/json_factory/version.rb
190
+ homepage: https://github.com/aklaiber/json_factory
191
+ licenses:
192
+ - MIT
193
+ metadata: {}
194
+ post_install_message:
195
+ rdoc_options: []
196
+ require_paths:
197
+ - lib
198
+ required_ruby_version: !ruby/object:Gem::Requirement
199
+ requirements:
200
+ - - ">="
201
+ - !ruby/object:Gem::Version
202
+ version: '0'
203
+ required_rubygems_version: !ruby/object:Gem::Requirement
204
+ requirements:
205
+ - - ">="
206
+ - !ruby/object:Gem::Version
207
+ version: '0'
208
+ requirements: []
209
+ rubyforge_project:
210
+ rubygems_version: 2.6.11
211
+ signing_key:
212
+ specification_version: 4
213
+ summary: JsonFactory is a Easy DSL to create JSON structures with the development
214
+ focus on performance.
215
+ test_files: []