json-schema_builder 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3e166709b6daba83a54ba1774b1bbae0207b8480
4
- data.tar.gz: 0af74a28bd06737381ad31e8aae3ca5f94c692f9
3
+ metadata.gz: 4f5b78c7099bde185d4a86c992f62027591989dc
4
+ data.tar.gz: 62ab154f7d358f6192203cd46859d13bbdc92c9b
5
5
  SHA512:
6
- metadata.gz: 9750809f3276af4d79e4eae367efbaa2a4434e25d8c07d788ab9b778569f6bf3e84581432e7890f990e53245a87c354c3a24f05ff44cf78c4a7ffca377f8556a
7
- data.tar.gz: 79fbfcccad68fb29152e13886c169895ae0499ca5f6253f4cd45c6b13ec8ef28200b9eb966f69bf7e3c7c6de5d5a1574f60141cac5e374f8317ba2ebd6595496
6
+ metadata.gz: c12f9f064e511bf3b49ff204202be34464ea51050758f58d7ad841adf1f6f336ed9aae5f80b50f9b587273bd05878b1762e8dff12679803ff5f1901c333055f5
7
+ data.tar.gz: 4a410940b32b0b3b87560f3932b64e744ce792b016328a3614a9456ccd1152e7014b14371639485036045289d8cfb90da966b052aefaa6b7a8b650f2ff8eadfd
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json-schema_builder (0.0.3)
4
+ json-schema_builder (0.0.4)
5
5
  activesupport (~> 4.0, = 4.2.0.rc2)
6
6
  json-schema (~> 2.5)
7
7
 
@@ -16,6 +16,9 @@ module JSON
16
16
 
17
17
  included do |klass|
18
18
  extend JSON::SchemaBuilder::Configuration
19
+ class << self
20
+ attr_accessor :root_key
21
+ end
19
22
  end
20
23
 
21
24
  def self.default_options
@@ -27,5 +30,18 @@ module JSON
27
30
  instance_variable_set "@#{ key }", value
28
31
  end
29
32
  end
33
+
34
+ def root(key = nil, &block)
35
+ root_key = key || self.class.root_key.to_sym
36
+ object do
37
+ object root_key, required: true, &block
38
+ end
39
+ end
40
+
41
+ module ClassMethods
42
+ def root(key)
43
+ @root_key = key
44
+ end
45
+ end
30
46
  end
31
47
  end
@@ -0,0 +1,3 @@
1
+ require 'rspec'
2
+ require_relative 'rspec_helper/deep_open_struct'
3
+ require_relative 'rspec_helper/rspec_helper'
@@ -0,0 +1,38 @@
1
+ module JSON
2
+ module SchemaBuilder
3
+ module RSpecHelper
4
+ class DeepOpenStruct < OpenStruct
5
+ def initialize(hash = { })
6
+ @table = { }
7
+ hash.each_pair do |key, value|
8
+ key = key.to_sym
9
+ @table[key] = _transform value
10
+ new_ostruct_member key
11
+ end
12
+ end
13
+
14
+ def ==(other)
15
+ to_h == other.to_h
16
+ end
17
+ alias_method :eql?, :==
18
+
19
+ def inspect
20
+ to_h.inspect
21
+ end
22
+
23
+ private
24
+
25
+ def _transform(object)
26
+ case object
27
+ when Hash
28
+ DeepOpenStruct.new object
29
+ when Array
30
+ object.map{ |item| _transform item }
31
+ else
32
+ object
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,31 @@
1
+ module JSON
2
+ module SchemaBuilder
3
+ module RSpecHelper
4
+ extend ::RSpec::SharedContext
5
+ let(:schema_method){ raise 'undefined schema method' }
6
+ let(:schema_context){ { } }
7
+ let(:schema){ described_class.new(schema_context).send schema_method }
8
+ let(:json){ schema.as_json }
9
+ subject{ DeepOpenStruct.new json }
10
+
11
+ def self.included(klass)
12
+ super klass
13
+ klass.send :extend, ClassMethods
14
+ end
15
+
16
+ module ClassMethods
17
+ def with(key, &block)
18
+ describe ".#{ key }" do
19
+ eval "def subject; super.#{ key }; end"
20
+
21
+ it 'should return an entity' do
22
+ expect(schema).to be_a JSON::SchemaBuilder::Entity
23
+ end
24
+
25
+ instance_exec &block
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,5 +1,5 @@
1
1
  module JSON
2
2
  module SchemaBuilder
3
- VERSION = '0.0.3'
3
+ VERSION = '0.0.4'
4
4
  end
5
5
  end
@@ -1,43 +1,47 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe Examples::ContextDelegation, type: :integration do
4
- context 'with an admin' do
5
- it_behaves_like 'a builder' do
6
- let(:schema_context) do
7
- {
8
- user: Examples::ContextDelegation::User.new(role: :admin)
9
- }
10
- end
4
+ [:example, :alternate_example].each do |method_name|
5
+ context 'with an admin' do
6
+ it_behaves_like 'a builder' do
7
+ let(:schema_method){ method_name }
8
+ let(:schema_context) do
9
+ {
10
+ user: Examples::ContextDelegation::User.new(role: :admin)
11
+ }
12
+ end
11
13
 
12
- let(:expected_json) do
13
- {
14
- type: :object,
15
- additionalProperties: false,
16
- properties: {
17
- name: { type: :string },
18
- role: { type: :string }
14
+ let(:expected_json) do
15
+ {
16
+ type: :object,
17
+ additionalProperties: false,
18
+ properties: {
19
+ name: { type: :string },
20
+ role: { type: :string }
21
+ }
19
22
  }
20
- }
23
+ end
21
24
  end
22
25
  end
23
- end
24
26
 
25
- context 'with a user' do
26
- it_behaves_like 'a builder' do
27
- let(:schema_context) do
28
- {
29
- user: Examples::ContextDelegation::User.new(role: :user)
30
- }
31
- end
27
+ context 'with a user' do
28
+ it_behaves_like 'a builder' do
29
+ let(:schema_method){ method_name }
30
+ let(:schema_context) do
31
+ {
32
+ user: Examples::ContextDelegation::User.new(role: :user)
33
+ }
34
+ end
32
35
 
33
- let(:expected_json) do
34
- {
35
- type: :object,
36
- additionalProperties: false,
37
- properties: {
38
- name: { type: :string }
36
+ let(:expected_json) do
37
+ {
38
+ type: :object,
39
+ additionalProperties: false,
40
+ properties: {
41
+ name: { type: :string }
42
+ }
39
43
  }
40
- }
44
+ end
41
45
  end
42
46
  end
43
47
  end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Examples::Rooted, type: :integration do
4
+ context 'with a root key' do
5
+ it_behaves_like 'a builder' do
6
+ let(:expected_json) do
7
+ {
8
+ type: :object,
9
+ required: [:rooted],
10
+ properties: {
11
+ rooted: {
12
+ type: :object,
13
+ properties: {
14
+ name: { type: :string }
15
+ }
16
+ }
17
+ }
18
+ }
19
+ end
20
+ end
21
+ end
22
+
23
+ context 'with an alternate root key' do
24
+ it_behaves_like 'a builder' do
25
+ let(:schema_method){ :alternate_root }
26
+ let(:expected_json) do
27
+ {
28
+ type: :object,
29
+ required: [:other_root],
30
+ properties: {
31
+ other_root: {
32
+ type: :object,
33
+ properties: {
34
+ name: { type: :string }
35
+ }
36
+ }
37
+ }
38
+ }
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,30 +1,22 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe Examples::VerboseObjects, type: :integration do
4
- it_behaves_like 'a builder' do
5
- let(:expected_json) do
6
- {
7
- type: :object,
8
- properties: {
9
- name: {
10
- type: :string,
11
- minLength: 1
12
- },
13
- ids: {
14
- type: :array,
15
- minItems: 1,
16
- items: {
17
- type: :object,
18
- required: [:id],
19
- properties: {
20
- id: {
21
- type: :integer
22
- }
23
- }
24
- }
25
- }
26
- }
27
- }
4
+ include JSON::SchemaBuilder::RSpecHelper
5
+ let(:schema_method){ :example }
6
+ its(:type){ is_expected.to eql 'object' }
7
+
8
+ with :properties do
9
+ its(:name){ is_expected.to eql type: 'string', minLength: 1 }
10
+
11
+ with :ids do
12
+ its(:type){ is_expected.to eql 'array' }
13
+ its(:minItems){ is_expected.to eql 1 }
14
+
15
+ with :items do
16
+ its(:type){ is_expected.to eql 'object' }
17
+ its(:required){ is_expected.to eql ['id'] }
18
+ its('properties.id.type'){ is_expected.to eql 'integer' }
19
+ end
28
20
  end
29
21
  end
30
22
  end
@@ -1,6 +1,7 @@
1
1
  require 'pry'
2
2
  require 'simplecov'
3
3
  SimpleCov.configure do
4
+ add_filter '/lib/json/schema_builder/rspec_helper'
4
5
  add_filter '/spec'
5
6
  end
6
7
 
@@ -1,6 +1,7 @@
1
1
  module Examples
2
2
  class ContextDelegation
3
3
  include JSON::SchemaBuilder
4
+ attr_accessor :user
4
5
  delegate :admin?, to: :@user
5
6
 
6
7
  def example
@@ -10,6 +11,13 @@ module Examples
10
11
  end
11
12
  end
12
13
 
14
+ def alternate_example
15
+ object additional_properties: false do
16
+ string :name
17
+ string :role if user.admin?
18
+ end
19
+ end
20
+
13
21
  class User < OpenStruct
14
22
  def admin?
15
23
  role == :admin
@@ -0,0 +1,19 @@
1
+ module Examples
2
+ class Rooted
3
+ include JSON::SchemaBuilder
4
+
5
+ root :rooted
6
+
7
+ def example
8
+ root do
9
+ string :name
10
+ end
11
+ end
12
+
13
+ def alternate_root
14
+ root :other_root do
15
+ string :name
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,7 +1,8 @@
1
1
  module IntegrationHelper
2
2
  extend RSpec::SharedContext
3
3
  let(:schema_context){ { } }
4
- let(:schema){ described_class.new(schema_context).example }
4
+ let(:schema_method){ :example }
5
+ let(:schema){ described_class.new(schema_context).send schema_method }
5
6
  let(:json){ schema.as_json }
6
7
  let(:expected_json){ { } }
7
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Parrish
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-16 00:00:00.000000000 Z
11
+ date: 2014-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -156,6 +156,9 @@ files:
156
156
  - lib/json/schema_builder/number.rb
157
157
  - lib/json/schema_builder/numeric.rb
158
158
  - lib/json/schema_builder/object.rb
159
+ - lib/json/schema_builder/rspec.rb
160
+ - lib/json/schema_builder/rspec_helper/deep_open_struct.rb
161
+ - lib/json/schema_builder/rspec_helper/rspec_helper.rb
159
162
  - lib/json/schema_builder/schema.rb
160
163
  - lib/json/schema_builder/string.rb
161
164
  - lib/json/schema_builder/validation.rb
@@ -166,6 +169,7 @@ files:
166
169
  - spec/integration/mixed_arrays_spec.rb
167
170
  - spec/integration/mixed_objects_spec.rb
168
171
  - spec/integration/object_definitions_spec.rb
172
+ - spec/integration/rooted_spec.rb
169
173
  - spec/integration/schema_builder_spec.rb
170
174
  - spec/integration/terse_arrays_spec.rb
171
175
  - spec/integration/terse_objects_spec.rb
@@ -180,6 +184,7 @@ files:
180
184
  - spec/support/examples/mixed_arrays.rb
181
185
  - spec/support/examples/mixed_objects.rb
182
186
  - spec/support/examples/object_definitions.rb
187
+ - spec/support/examples/rooted.rb
183
188
  - spec/support/examples/schema_builder.rb
184
189
  - spec/support/examples/terse_arrays.rb
185
190
  - spec/support/examples/terse_objects.rb
@@ -229,6 +234,7 @@ test_files:
229
234
  - spec/integration/mixed_arrays_spec.rb
230
235
  - spec/integration/mixed_objects_spec.rb
231
236
  - spec/integration/object_definitions_spec.rb
237
+ - spec/integration/rooted_spec.rb
232
238
  - spec/integration/schema_builder_spec.rb
233
239
  - spec/integration/terse_arrays_spec.rb
234
240
  - spec/integration/terse_objects_spec.rb
@@ -243,6 +249,7 @@ test_files:
243
249
  - spec/support/examples/mixed_arrays.rb
244
250
  - spec/support/examples/mixed_objects.rb
245
251
  - spec/support/examples/object_definitions.rb
252
+ - spec/support/examples/rooted.rb
246
253
  - spec/support/examples/schema_builder.rb
247
254
  - spec/support/examples/terse_arrays.rb
248
255
  - spec/support/examples/terse_objects.rb