cartograph 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CHANGELOG.md +11 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +211 -0
- data/Rakefile +6 -0
- data/cartograph.gemspec +24 -0
- data/examples/collection_representation.rb +41 -0
- data/examples/domains.rb +54 -0
- data/examples/representation_for.rb +24 -0
- data/lib/cartograph.rb +24 -0
- data/lib/cartograph/artist.rb +34 -0
- data/lib/cartograph/dsl.rb +104 -0
- data/lib/cartograph/map.rb +91 -0
- data/lib/cartograph/property.rb +69 -0
- data/lib/cartograph/property_collection.rb +27 -0
- data/lib/cartograph/root_key.rb +19 -0
- data/lib/cartograph/sculptor.rb +40 -0
- data/lib/cartograph/version.rb +3 -0
- data/spec/lib/cartograph/artist_spec.rb +115 -0
- data/spec/lib/cartograph/dsl_spec.rb +257 -0
- data/spec/lib/cartograph/map_spec.rb +160 -0
- data/spec/lib/cartograph/property_collection_spec.rb +15 -0
- data/spec/lib/cartograph/property_spec.rb +235 -0
- data/spec/lib/cartograph/root_key_spec.rb +38 -0
- data/spec/lib/cartograph/sculptor_spec.rb +107 -0
- data/spec/spec_helper.rb +77 -0
- data/spec/support/dsl_contexts.rb +22 -0
- data/spec/support/dummy_comment.rb +2 -0
- data/spec/support/dummy_user.rb +2 -0
- metadata +132 -0
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Cartograph::RootKey do
|
4
|
+
describe '#initialize' do
|
5
|
+
it 'initializes with options' do
|
6
|
+
options = { singular: 'hello', plural: 'hellos', scopes: [:read] }
|
7
|
+
|
8
|
+
instance = Cartograph::RootKey.new(options)
|
9
|
+
expect(instance.options).to eq(options)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#scopes' do
|
14
|
+
it 'reads the scopes' do
|
15
|
+
instance = Cartograph::RootKey.new(scopes: [:read])
|
16
|
+
expect(instance.scopes).to eq([:read])
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'reads the scopes as an array always' do
|
20
|
+
instance = Cartograph::RootKey.new(scopes: :read)
|
21
|
+
expect(instance.scopes).to eq([:read])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#singular' do
|
26
|
+
it 'reads the singular key' do
|
27
|
+
instance = Cartograph::RootKey.new(singular: 'user')
|
28
|
+
expect(instance.singular).to eq('user')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#plural' do
|
33
|
+
it 'reads the plural key' do
|
34
|
+
instance = Cartograph::RootKey.new(plural: 'user')
|
35
|
+
expect(instance.plural).to eq('user')
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Cartograph::Sculptor do
|
4
|
+
describe '#initialize' do
|
5
|
+
it 'initializes with a hash and map' do
|
6
|
+
hash = {}
|
7
|
+
map = double('im a map')
|
8
|
+
|
9
|
+
sculptor = Cartograph::Sculptor.new(hash, map)
|
10
|
+
|
11
|
+
expect(sculptor.object).to be(hash)
|
12
|
+
expect(sculptor.map).to be(map)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#sculpted_object=' do
|
17
|
+
context 'objects that are not the mapping class' do
|
18
|
+
it 'raises an error' do
|
19
|
+
hash = {}
|
20
|
+
map = double(Cartograph::Map, mapping: Hash)
|
21
|
+
|
22
|
+
sculptor = Cartograph::Sculptor.new(hash, map)
|
23
|
+
|
24
|
+
expect {
|
25
|
+
sculptor.sculpted_object = ""
|
26
|
+
}.to raise_error(ArgumentError)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#sculpt' do
|
32
|
+
let(:map) { Cartograph::Map.new }
|
33
|
+
let(:object) { { 'id' => 343, 'name' => 'Guilty Spark', 'email_address' => 'guilty@bungie.net' } }
|
34
|
+
|
35
|
+
it 'returns nil if the object in nil' do
|
36
|
+
sculptor = Cartograph::Sculptor.new(nil, map)
|
37
|
+
expect(sculptor.sculpt).to be_nil
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'without a scope' do
|
41
|
+
before do
|
42
|
+
map.mapping DummyUser
|
43
|
+
map.property :id, scopes: [:read]
|
44
|
+
map.property :name, scopes: [:read, :create]
|
45
|
+
map.property :email, scopes: [:read, :create], key: 'email_address'
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'returns a coerced user' do
|
49
|
+
sculptor = Cartograph::Sculptor.new(object, map)
|
50
|
+
sculpted = sculptor.sculpt
|
51
|
+
|
52
|
+
expect(sculpted).to be_kind_of(DummyUser)
|
53
|
+
expect(sculpted.id).to eq(object['id'])
|
54
|
+
expect(sculpted.name).to eq(object['name'])
|
55
|
+
expect(sculpted.email).to eq(object['email_address'])
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'sculpts into a provided object' do
|
59
|
+
sculptor = Cartograph::Sculptor.new(object, map)
|
60
|
+
dummy = DummyUser.new
|
61
|
+
sculptor.sculpted_object = dummy
|
62
|
+
sculpted = sculptor.sculpt
|
63
|
+
|
64
|
+
expect(sculpted).to eq(dummy)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'with a scope' do
|
69
|
+
before do
|
70
|
+
map.mapping DummyUser
|
71
|
+
map.property :id, scopes: [:read]
|
72
|
+
map.property :name, scopes: [:create]
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'returns a coerced user' do
|
76
|
+
sculptor = Cartograph::Sculptor.new(object, map)
|
77
|
+
sculpted = sculptor.sculpt(:read)
|
78
|
+
|
79
|
+
expect(sculpted).to be_kind_of(DummyUser)
|
80
|
+
expect(sculpted.id).to eq(object['id'])
|
81
|
+
expect(sculpted.name).to be_nil
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'for nested properties' do
|
85
|
+
let(:object) { super().merge('comment' => { 'id' => 123, 'text' => 'hello' }) }
|
86
|
+
|
87
|
+
before do
|
88
|
+
map.property :comment, scopes: [:read] do
|
89
|
+
mapping DummyComment
|
90
|
+
|
91
|
+
property :id, scopes: [:read]
|
92
|
+
property :text, scopes: [:create]
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'returns the nested objects with scoped properties set' do
|
97
|
+
sculptor = Cartograph::Sculptor.new(object, map)
|
98
|
+
sculpted = sculptor.sculpt(:read)
|
99
|
+
|
100
|
+
expect(sculpted.comment).to be_kind_of(DummyComment)
|
101
|
+
expect(sculpted.comment.id).to eq(123)
|
102
|
+
expect(sculpted.comment.text).to be_nil
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'cartograph'
|
2
|
+
|
3
|
+
require 'pry'
|
4
|
+
|
5
|
+
Dir['./spec/support/**/*.rb'].each {|f| load f }
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
# The settings below are suggested to provide a good initial experience
|
9
|
+
# with RSpec, but feel free to customize to your heart's content.
|
10
|
+
|
11
|
+
# These two settings work together to allow you to limit a spec run
|
12
|
+
# to individual examples or groups you care about by tagging them with
|
13
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
14
|
+
# get run.
|
15
|
+
config.filter_run :focus
|
16
|
+
config.run_all_when_everything_filtered = true
|
17
|
+
|
18
|
+
config.after(:each) do
|
19
|
+
Cartograph.default_dumper = JSON
|
20
|
+
Cartograph.default_loader = JSON
|
21
|
+
Cartograph.default_cache = nil
|
22
|
+
Cartograph.default_cache_key = nil
|
23
|
+
end
|
24
|
+
|
25
|
+
=begin
|
26
|
+
|
27
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
28
|
+
# file, and it's useful to allow more verbose output when running an
|
29
|
+
# individual spec file.
|
30
|
+
if config.files_to_run.one?
|
31
|
+
# Use the documentation formatter for detailed output,
|
32
|
+
# unless a formatter has already been configured
|
33
|
+
# (e.g. via a command-line flag).
|
34
|
+
config.default_formatter = 'doc'
|
35
|
+
end
|
36
|
+
|
37
|
+
# Print the 10 slowest examples and example groups at the
|
38
|
+
# end of the spec run, to help surface which specs are running
|
39
|
+
# particularly slow.
|
40
|
+
config.profile_examples = 10
|
41
|
+
|
42
|
+
# Run specs in random order to surface order dependencies. If you find an
|
43
|
+
# order dependency and want to debug it, you can fix the order by providing
|
44
|
+
# the seed, which is printed after each run.
|
45
|
+
# --seed 1234
|
46
|
+
config.order = :random
|
47
|
+
|
48
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
49
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
50
|
+
# test failures related to randomization by passing the same `--seed` value
|
51
|
+
# as the one that triggered the failure.
|
52
|
+
Kernel.srand config.seed
|
53
|
+
|
54
|
+
# rspec-expectations config goes here. You can use an alternate
|
55
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
56
|
+
# assertions if you prefer.
|
57
|
+
config.expect_with :rspec do |expectations|
|
58
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
59
|
+
# For more details, see:
|
60
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
61
|
+
expectations.syntax = :expect
|
62
|
+
end
|
63
|
+
|
64
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
65
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
66
|
+
config.mock_with :rspec do |mocks|
|
67
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
68
|
+
# For more details, see:
|
69
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
70
|
+
mocks.syntax = :expect
|
71
|
+
|
72
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
73
|
+
# a real object. This is generally recommended.
|
74
|
+
mocks.verify_partial_doubles = true
|
75
|
+
end
|
76
|
+
=end
|
77
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
shared_context "DSL Objects" do
|
2
|
+
let(:object) { double('object', id: 1066, name: 'Bruce (the dude from Finding Nemo)') }
|
3
|
+
let(:mapped) do
|
4
|
+
Class.new do
|
5
|
+
include Cartograph::DSL
|
6
|
+
|
7
|
+
cartograph do
|
8
|
+
mapping DummyUser
|
9
|
+
|
10
|
+
property :id, scopes: [:read]
|
11
|
+
property :name, scopes: [:read, :create]
|
12
|
+
|
13
|
+
property :comments, plural: true do
|
14
|
+
mapping DummyComment
|
15
|
+
|
16
|
+
property :id, scopes: [:read]
|
17
|
+
property :text, scopes: [:read, :create]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cartograph
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Ross
|
8
|
+
- Kyrylo Silin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2017-09-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '12.0'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '12.0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rspec
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '3.6'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '3.6'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: pry
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
description: Cartograph makes it easy to generate and convert JSON. It's intention
|
57
|
+
is to be used for API clients.
|
58
|
+
email:
|
59
|
+
- silin@kyrylo.org
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- ".rspec"
|
66
|
+
- ".travis.yml"
|
67
|
+
- CHANGELOG.md
|
68
|
+
- Gemfile
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- cartograph.gemspec
|
73
|
+
- examples/collection_representation.rb
|
74
|
+
- examples/domains.rb
|
75
|
+
- examples/representation_for.rb
|
76
|
+
- lib/cartograph.rb
|
77
|
+
- lib/cartograph/artist.rb
|
78
|
+
- lib/cartograph/dsl.rb
|
79
|
+
- lib/cartograph/map.rb
|
80
|
+
- lib/cartograph/property.rb
|
81
|
+
- lib/cartograph/property_collection.rb
|
82
|
+
- lib/cartograph/root_key.rb
|
83
|
+
- lib/cartograph/sculptor.rb
|
84
|
+
- lib/cartograph/version.rb
|
85
|
+
- spec/lib/cartograph/artist_spec.rb
|
86
|
+
- spec/lib/cartograph/dsl_spec.rb
|
87
|
+
- spec/lib/cartograph/map_spec.rb
|
88
|
+
- spec/lib/cartograph/property_collection_spec.rb
|
89
|
+
- spec/lib/cartograph/property_spec.rb
|
90
|
+
- spec/lib/cartograph/root_key_spec.rb
|
91
|
+
- spec/lib/cartograph/sculptor_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
- spec/support/dsl_contexts.rb
|
94
|
+
- spec/support/dummy_comment.rb
|
95
|
+
- spec/support/dummy_user.rb
|
96
|
+
homepage: https://github.com/kyrylo/cartograph
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
metadata: {}
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 2.5.2
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: Cartograph makes it easy to generate and convert JSON. It's intention is
|
120
|
+
to be used for API clients.
|
121
|
+
test_files:
|
122
|
+
- spec/lib/cartograph/artist_spec.rb
|
123
|
+
- spec/lib/cartograph/dsl_spec.rb
|
124
|
+
- spec/lib/cartograph/map_spec.rb
|
125
|
+
- spec/lib/cartograph/property_collection_spec.rb
|
126
|
+
- spec/lib/cartograph/property_spec.rb
|
127
|
+
- spec/lib/cartograph/root_key_spec.rb
|
128
|
+
- spec/lib/cartograph/sculptor_spec.rb
|
129
|
+
- spec/spec_helper.rb
|
130
|
+
- spec/support/dsl_contexts.rb
|
131
|
+
- spec/support/dummy_comment.rb
|
132
|
+
- spec/support/dummy_user.rb
|