kartograph 0.2.2 → 0.2.3

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: 058669bff932d817f5f55ba0db31eae00a6176c8
4
- data.tar.gz: 13ce3561911df10ec3bcbf972e929cf4171b7a42
3
+ metadata.gz: 34cf2d2347f433eb341a3ff291db0897542b44fa
4
+ data.tar.gz: 587af332800ff4eaec9fb7a685e74379557d6091
5
5
  SHA512:
6
- metadata.gz: ea8f25bb8ab823fe9c2206cab7afb3f2626f1ad233d7fcdcfd6eb9781d5e7e521fd65d5e2d7b056297f129bc863f0ad3fdc054a06410aabd18d434038bd0b422
7
- data.tar.gz: 547d95b3446781a52e56b3e75e8ff56dfa932a4aad52effdaa76fdf4d9f290f65aa966c8ebe5b35c8281dbf3fb5db53eeaf3c2c592b9f18809fcbcd34f2c6409
6
+ metadata.gz: b6d58d31c6519e3ef49a301429370294b95c4b9c2cbf7b26b2a64943fa17f802ac98f7f8e1cd8ab3bdd16f786105fd0c4140d416ccb7b2bc35d25e05d351f37d
7
+ data.tar.gz: d3a4ae339b1fa2f2815efc101e1db7a60b6edfd75bafa3b0afe2f6d3a13b9735ae445a5b5eaccae2ed47d95a0d0460dfb9265abf52b582c9536a6a417343e62e
data/README.md CHANGED
@@ -36,6 +36,16 @@ user = User.new(name: 'Bobby Tables')
36
36
  json_for_create = UserMapping.representation_for(:create, user)
37
37
  ```
38
38
 
39
+ ### Rendering Objects or Collections as Hashes
40
+
41
+ ```ruby
42
+ user = User.new(name: 'PB Jelly')
43
+ users = [user]
44
+
45
+ hash = UserMapping.hash_for(:read, user)
46
+ hash_collection = UserMapping.hash_collection_for(:read, user)
47
+ ```
48
+
39
49
  ### Rendering Collections as JSON
40
50
 
41
51
  ```ruby
@@ -15,18 +15,35 @@ module Kartograph
15
15
  @kartograph_map
16
16
  end
17
17
 
18
- def representation_for(scope, object, dumper = Kartograph.default_dumper)
19
- drawed = Artist.new(object, @kartograph_map).draw(scope)
20
-
21
- dumper.dump(prepend_root_key(scope, :singular, drawed))
18
+ # Returns a hash representation of the object based on the mapping
19
+ #
20
+ # @param scope [Symbol] the scope of the mapping
21
+ # @param object the object to be mapped
22
+ # @return [Hash, Array]
23
+ def hash_for(scope, object)
24
+ drawn_object = Artist.new(object, @kartograph_map).draw(scope)
25
+ prepend_root_key(scope, :singular, drawn_object)
22
26
  end
23
27
 
24
- def represent_collection_for(scope, objects, dumper = Kartograph.default_dumper)
25
- drawed_objects = objects.map do |object|
28
+ # Returns a hash representation of the collection of objects based on the mapping
29
+ #
30
+ # @param scope [Symbol] the scope of the mapping
31
+ # @params objects [Array] the array of objects to be mapped
32
+ # @return [Hash, Array]
33
+ def hash_collection_for(scope, objects)
34
+ drawn_objects = objects.map do |object|
26
35
  Artist.new(object, @kartograph_map).draw(scope)
27
36
  end
28
37
 
29
- dumper.dump(prepend_root_key(scope, :plural, drawed_objects))
38
+ prepend_root_key(scope, :plural, drawn_objects)
39
+ end
40
+
41
+ def representation_for(scope, object, dumper = Kartograph.default_dumper)
42
+ dumper.dump(hash_for(scope, object))
43
+ end
44
+
45
+ def represent_collection_for(scope, objects, dumper = Kartograph.default_dumper)
46
+ dumper.dump(hash_collection_for(scope, objects))
30
47
  end
31
48
 
32
49
  def extract_single(content, scope, loader = Kartograph.default_loader)
@@ -40,6 +57,19 @@ module Kartograph
40
57
  Sculptor.new(loaded, @kartograph_map).sculpt(scope)
41
58
  end
42
59
 
60
+ def extract_into_object(object, content, scope, loader = Kartograph.default_loader)
61
+ loaded = loader.load(content)
62
+
63
+ retrieve_root_key(scope, :singular) do |root_key|
64
+ # Reassign loaded if a root key exists
65
+ loaded = loaded[root_key]
66
+ end
67
+
68
+ sculptor = Sculptor.new(loaded, @kartograph_map)
69
+ sculptor.sculpted_object = object
70
+ sculptor.sculpt(scope)
71
+ end
72
+
43
73
  def extract_collection(content, scope, loader = Kartograph.default_loader)
44
74
  loaded = loader.load(content)
45
75
 
@@ -71,4 +101,4 @@ module Kartograph
71
101
 
72
102
  end
73
103
  end
74
- end
104
+ end
@@ -20,7 +20,7 @@ module Kartograph
20
20
  end
21
21
 
22
22
  def key
23
- options[:key] || name
23
+ (options[:key] || name).to_s
24
24
  end
25
25
 
26
26
  def value_for(object, scope = nil)
@@ -31,7 +31,7 @@ module Kartograph
31
31
 
32
32
  def value_from(object, scope = nil)
33
33
  return if object.nil?
34
- value = object.has_key?(key) ? object[key] : object[key.to_s]
34
+ value = object.has_key?(key) ? object[key] : object[key.to_sym]
35
35
  map ? sculpt_value(value, scope) : value
36
36
  end
37
37
 
@@ -11,14 +11,25 @@ module Kartograph
11
11
  map.properties
12
12
  end
13
13
 
14
+ def sculpted_object
15
+ # Fallback initialization of the object we're extracting into
16
+ @sculpted_object ||= map.mapping.new
17
+ end
18
+
19
+ # Set this to pass in an object to extract into. Must
20
+ # @param object must be of the same class as the map#mapping
21
+ def sculpted_object=(object)
22
+ raise ArgumentError unless object.is_a?(map.mapping)
23
+
24
+ @sculpted_object = object
25
+ end
26
+
14
27
  def sculpt(scope = nil)
15
28
  return nil if @object.nil?
16
29
 
17
- # Initializing the object we're coercing so we can set attributes on it
18
- coerced = map.mapping.new
19
30
  scoped_properties = scope ? properties.filter_by_scope(scope) : properties
20
31
 
21
- scoped_properties.each_with_object(coerced) do |property, mutable|
32
+ scoped_properties.each_with_object(sculpted_object) do |property, mutable|
22
33
  setter_method = "#{property.name}="
23
34
  value = property.value_from(object, scope)
24
35
  mutable.send(setter_method, value)
@@ -1,4 +1,4 @@
1
1
  module Kartograph
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
4
4
 
@@ -24,7 +24,7 @@ describe Kartograph::Artist do
24
24
  artist = Kartograph::Artist.new(object, map)
25
25
  masterpiece = artist.draw
26
26
 
27
- expect(masterpiece).to include(hello: 'world')
27
+ expect(masterpiece).to include('hello' => 'world')
28
28
  end
29
29
 
30
30
  it 'raises for a property that the object does not have' do
@@ -43,7 +43,7 @@ describe Kartograph::Artist do
43
43
  artist = Kartograph::Artist.new(object, map)
44
44
  masterpiece = artist.draw
45
45
 
46
- expect(masterpiece).to include(hola: 'world')
46
+ expect(masterpiece).to include('hola' => 'world')
47
47
  end
48
48
  end
49
49
 
@@ -56,7 +56,7 @@ describe Kartograph::Artist do
56
56
  artist = Kartograph::Artist.new(object, map)
57
57
  masterpiece = artist.draw(:read)
58
58
 
59
- expect(masterpiece).to eq(hello: 'world')
59
+ expect(masterpiece).to eq('hello' => 'world')
60
60
  end
61
61
 
62
62
  context 'on nested properties' do
@@ -74,7 +74,7 @@ describe Kartograph::Artist do
74
74
  artist = Kartograph::Artist.new(object, map)
75
75
  masterpiece = artist.draw(:read)
76
76
 
77
- expect(masterpiece).to eq(child: { foo: child.foo })
77
+ expect(masterpiece).to eq('child' => { 'foo' => child.foo })
78
78
  end
79
79
  end
80
80
  end
@@ -111,4 +111,4 @@ describe Kartograph::Artist do
111
111
  end
112
112
  end
113
113
  end
114
- end
114
+ end
@@ -20,6 +20,76 @@ describe Kartograph::DSL do
20
20
  end
21
21
  end
22
22
 
23
+ describe '.hash_for' do
24
+ include_context 'DSL Objects'
25
+
26
+ it 'returns the hash representation for an object' do
27
+ hash = mapped.hash_for(:create, object)
28
+ expect(hash).to eq({ 'name' => object.name })
29
+ end
30
+
31
+ context 'with a root key for the scope' do
32
+ it 'returns the hash with the root key' do
33
+ mapped.kartograph do
34
+ root_key singular: 'user', scopes: [:create]
35
+ end
36
+ hash = mapped.hash_for(:create, object)
37
+
38
+ expect(hash).to eq(
39
+ {
40
+ 'user' => {
41
+ 'name' => object.name
42
+ }
43
+ }
44
+ )
45
+ end
46
+ end
47
+ end
48
+
49
+ describe '.hash_collection_for' do
50
+ include_context 'DSL Objects'
51
+
52
+ let(:users) { Array.new(3, object) }
53
+
54
+ subject(:parsed) do
55
+ json = mapped.represent_collection_for(:read, users)
56
+ JSON.parse(json)
57
+ end
58
+
59
+ it 'returns the objects as a collection of hashes' do
60
+ collection = mapped.hash_collection_for(:read, users)
61
+
62
+ expect(collection).to be_an(Array)
63
+ expect(collection.size).to be(3)
64
+
65
+ expect(collection[0]['id']).to eq(users[0].id)
66
+ expect(collection[0]['name']).to eq(users[0].name)
67
+ expect(collection[1]['id']).to eq(users[1].id)
68
+ expect(collection[1]['name']).to eq(users[1].name)
69
+ end
70
+
71
+ context 'with a root key' do
72
+ it "includes the root key" do
73
+ root_key_name = "the_root_key"
74
+
75
+ mapped.kartograph do
76
+ root_key plural: root_key_name, scopes: [:read]
77
+ end
78
+
79
+ collection = mapped.hash_collection_for(:read, users)
80
+
81
+ expect(collection).to be_an(Hash)
82
+ expect(collection.keys.first).to eq(root_key_name)
83
+
84
+ collection_array = collection[root_key_name]
85
+ expect(collection_array[0]['id']).to eq(users[0].id)
86
+ expect(collection_array[0]['name']).to eq(users[0].name)
87
+ expect(collection_array[1]['id']).to eq(users[1].id)
88
+ expect(collection_array[1]['name']).to eq(users[1].name)
89
+ end
90
+ end
91
+ end
92
+
23
93
  describe '.representation_for' do
24
94
  include_context 'DSL Objects'
25
95
 
@@ -122,6 +192,21 @@ describe Kartograph::DSL do
122
192
  end
123
193
  end
124
194
 
195
+ describe '.extract_into_object' do
196
+ include_context 'DSL Objects'
197
+ let(:json) do
198
+ { id: 1337, name: 'Paul the octopus' }
199
+ end
200
+
201
+ it 'returns a populated object from a JSON representation' do
202
+ object = DummyUser.new
203
+ mapped.extract_into_object(object, json.to_json, :read)
204
+
205
+ expect(object.id).to eq(1337)
206
+ expect(object.name).to eq('Paul the octopus')
207
+ end
208
+ end
209
+
125
210
  describe '.extract_collection' do
126
211
  include_context 'DSL Objects'
127
212
  let(:json) do
@@ -169,4 +254,4 @@ describe Kartograph::DSL do
169
254
  end
170
255
  end
171
256
  end
172
- end
257
+ end
@@ -9,7 +9,7 @@ describe Kartograph::Property do
9
9
  property = Kartograph::Property.new(name, options)
10
10
  expect(property.name).to eq(name)
11
11
  expect(property.options).to eq(options)
12
- expect(property.key).to eq(:hey)
12
+ expect(property.key).to eq('hey')
13
13
  end
14
14
 
15
15
  context 'with a key' do
@@ -89,7 +89,7 @@ describe Kartograph::Property do
89
89
  child = double('child', cephalopod: 'I will ink you')
90
90
  root = double('root', sammy: child)
91
91
 
92
- expect(top_level.value_for(root)).to eq(cephalopod: child.cephalopod)
92
+ expect(top_level.value_for(root)).to eq('cephalopod' => child.cephalopod)
93
93
  end
94
94
 
95
95
  context 'when it is plural' do
@@ -104,8 +104,8 @@ describe Kartograph::Property do
104
104
  root = double('root', sammy: [child1, child2])
105
105
 
106
106
  expect(top_level.value_for(root)).to eq([
107
- { cephalopod: child1.cephalopod },
108
- { cephalopod: child2.cephalopod }
107
+ { 'cephalopod' => child1.cephalopod },
108
+ { 'cephalopod' => child2.cephalopod }
109
109
  ])
110
110
  end
111
111
  end
@@ -13,6 +13,40 @@ describe Kartograph::Sculptor do
13
13
  end
14
14
  end
15
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(Kartograph::Map, mapping: Hash)
21
+
22
+ sculptor = Kartograph::Sculptor.new(hash, map)
23
+
24
+ expect {
25
+ sculptor.sculpted_object = ""
26
+ }.to raise_error(ArgumentError)
27
+ end
28
+ end
29
+
30
+ it 'sets the sculpted_object' do
31
+ hash = {}
32
+ map = double(Kartograph::Map, mapping: Hash)
33
+
34
+ sculptor = Kartograph::Sculptor.new(hash, map)
35
+ sculptor.sculpted_object = hash
36
+ expect(sculptor.sculpted_object).to be hash
37
+ end
38
+ end
39
+
40
+ describe '#sculpted_object' do
41
+ it 'initializes the mapping class' do
42
+ hash = {}
43
+ map = double(Kartograph::Map, mapping: Hash)
44
+
45
+ sculptor = Kartograph::Sculptor.new(hash, map)
46
+ expect(sculptor.sculpted_object).to be_kind_of(Hash)
47
+ end
48
+ end
49
+
16
50
  describe '#sculpt' do
17
51
  let(:map) { Kartograph::Map.new }
18
52
  let(:object) { { 'id' => 343, 'name' => 'Guilty Spark', 'email_address' => 'guilty@bungie.net' } }
@@ -39,6 +73,15 @@ describe Kartograph::Sculptor do
39
73
  expect(sculpted.name).to eq(object['name'])
40
74
  expect(sculpted.email).to eq(object['email_address'])
41
75
  end
76
+
77
+ it 'sculpts into a provided object' do
78
+ sculptor = Kartograph::Sculptor.new(object, map)
79
+ dummy = DummyUser.new
80
+ sculptor.sculpted_object = dummy
81
+ sculpted = sculptor.sculpt
82
+
83
+ expect(sculpted).to be dummy
84
+ end
42
85
  end
43
86
 
44
87
  context 'with a scope' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kartograph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Ross
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-10 00:00:00.000000000 Z
11
+ date: 2015-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -127,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
127
  version: '0'
128
128
  requirements: []
129
129
  rubyforge_project:
130
- rubygems_version: 2.2.2
130
+ rubygems_version: 2.4.5.1
131
131
  signing_key:
132
132
  specification_version: 4
133
133
  summary: Kartograph makes it easy to generate and convert JSON. It's intention is