kartograph 0.0.7 → 0.0.8

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: ca1d38c7c5914edc0a9b78ffed7701e8b3cefff7
4
- data.tar.gz: 386c0e6f38402b271d00907596052e34327c1ca4
3
+ metadata.gz: 4b117d28ec0561cefabaf740af34ebfb3cf48a68
4
+ data.tar.gz: 8874d274b2b788ebbd2c0e79a748c29cdd226e25
5
5
  SHA512:
6
- metadata.gz: 254b47f2276dea83ef2e3de5ec260cc1ab3d6ed57a557382d725c1ee2d566f3e5f505ad4daacba0015e4112d63cee3b713e362a56115fb759f3b192516305eaa
7
- data.tar.gz: 4a307fb10db634870adaa9f3d9c291803ace606f299e27a46cb91d45466203d17a33bd6b7d614541e517059af624b1db210e3ad3e92230f6702fe11d7d62a7b5
6
+ metadata.gz: b36df369277c0088da9f2fa895ef0ed1640a3c369beab8fc8286480c5f47bc3b98a5fe5e62a19621fc60a5372519df07336a98ce5b1135c06c6c041254709e57
7
+ data.tar.gz: ec90464e0b3402ac70249f44d96d5a78f19899c681674cb911043bbbc6b4e607eabaa1db7b03b9d5b590330d6050fedd7698caf27fa4450ad8a66b58bdf8cf00
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
4
+ - 2.0.0
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  A Serialization / Deserialization library.
4
4
 
5
+ [![Build Status](https://travis-ci.org/digitaloceancloud/kartograph.svg?branch=master)](https://travis-ci.org/digitaloceancloud/kartograph)
6
+
5
7
  ## Installation
6
8
 
7
9
  Add this line to your application's Gemfile:
@@ -134,6 +136,30 @@ class CommentMapping
134
136
  end
135
137
  ```
136
138
 
139
+
140
+ ### Scope blocks
141
+
142
+ Sometimes adding scopes to all properties can be tedious, to avoid that, you can define properties within a scope block.
143
+
144
+ ```ruby
145
+ class UserMapping
146
+ include Kartograph::DSL
147
+
148
+ kartograph do
149
+ scoped :read do
150
+ property :name
151
+ property :id
152
+ property :email, key: 'email_address' # The JSON returned has the key of email_address, our property is called email however.
153
+ end
154
+
155
+ scoped :update, :create do
156
+ property :name
157
+ property :email, key: 'email_address'
158
+ end
159
+ end
160
+ end
161
+ ```
162
+
137
163
  Now when JSON includes comments for a user, it will know how to map the comments using the provided Kartograph definition.
138
164
 
139
165
  ## Contributing
data/Rakefile CHANGED
@@ -1,2 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
2
3
 
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/lib/kartograph.rb CHANGED
@@ -7,6 +7,7 @@ module Kartograph
7
7
  autoload :Property, 'kartograph/property'
8
8
  autoload :PropertyCollection, 'kartograph/property_collection'
9
9
  autoload :RootKey, 'kartograph/root_key'
10
+ autoload :ScopeProxy, 'kartograph/scope_proxy'
10
11
 
11
12
  autoload :Artist, 'kartograph/artist'
12
13
  autoload :Sculptor, 'kartograph/sculptor'
@@ -11,6 +11,11 @@ module Kartograph
11
11
  @properties ||= PropertyCollection.new
12
12
  end
13
13
 
14
+ def scoped(*scopes, &block)
15
+ proxy = ScopeProxy.new(self, scopes)
16
+ proxy.instance_eval(&block)
17
+ end
18
+
14
19
  def root_keys
15
20
  @root_keys ||= []
16
21
  end
@@ -25,6 +25,7 @@ module Kartograph
25
25
 
26
26
  def value_for(object, scope = nil)
27
27
  value = object.send(name)
28
+ return if value.nil?
28
29
  map ? artist_value(value, scope) : value
29
30
  end
30
31
 
@@ -0,0 +1,17 @@
1
+ module Kartograph
2
+ class ScopeProxy
3
+ attr_reader :map, :scopes
4
+
5
+ def initialize(map, scopes)
6
+ @map, @scopes = map, scopes
7
+ end
8
+
9
+ def property(*args, &block)
10
+ options = args.last.is_a?(Hash) ? args.pop : {}
11
+ options[:scopes] = scopes
12
+ args << options
13
+
14
+ map.property(*args, &block)
15
+ end
16
+ end
17
+ end
@@ -1,4 +1,4 @@
1
1
  module Kartograph
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
4
4
 
@@ -92,4 +92,17 @@ describe Kartograph::Map do
92
92
  expect(map1).to eq(map2)
93
93
  end
94
94
  end
95
+
96
+ describe '#scoped' do
97
+ it 'adds properties with the scopes defined on the block instantiation' do
98
+ map.scoped :read, :write do
99
+ property :name
100
+ property :something
101
+ end
102
+
103
+ expect(map.properties.size).to eq(2)
104
+ expect(map.properties.filter_by_scope(:read).map(&:name)).to eq([:name, :something])
105
+ expect(map.properties.filter_by_scope(:write).map(&:name)).to eq([:name, :something])
106
+ end
107
+ end
95
108
  end
@@ -109,6 +109,18 @@ describe Kartograph::Property do
109
109
  ])
110
110
  end
111
111
  end
112
+
113
+ context 'when the value for the root object is nil' do
114
+ it 'returns nil' do
115
+ top_level = Kartograph::Property.new(:sammy) do
116
+ property :cephalopod
117
+ end
118
+
119
+ root = double(sammy: nil)
120
+
121
+ expect(top_level.value_for(root)).to be_nil
122
+ end
123
+ end
112
124
  end
113
125
  end
114
126
 
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Kartograph::ScopeProxy do
4
+ describe '#initialize' do
5
+ it 'initializes with a map, scopes, and a block' do
6
+ map, scopes = double, [:read, :write]
7
+
8
+ instance = Kartograph::ScopeProxy.new(map, scopes)
9
+
10
+ expect(instance.map).to be(map)
11
+ expect(instance.scopes).to be(scopes)
12
+ end
13
+ end
14
+
15
+ describe '#property' do
16
+ let(:map) { Kartograph::Map.new }
17
+ subject(:proxy) { Kartograph::ScopeProxy.new(map, [:read, :write]) }
18
+
19
+ it 'adds a property to the properties with the correct scope' do
20
+ proxy.property :hello
21
+ proxy.property :world
22
+
23
+ read_properties = map.properties.filter_by_scope(:read)
24
+ expect(read_properties.map(&:name)).to eq([:hello, :world])
25
+
26
+ write_properties = map.properties.filter_by_scope(:write)
27
+ expect(write_properties.map(&:name)).to eq([:hello, :world])
28
+ end
29
+ end
30
+ end
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.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Ross
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-11 00:00:00.000000000 Z
11
+ date: 2014-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -77,6 +77,7 @@ extra_rdoc_files: []
77
77
  files:
78
78
  - .gitignore
79
79
  - .rspec
80
+ - .travis.yml
80
81
  - Gemfile
81
82
  - LICENSE.txt
82
83
  - README.md
@@ -91,6 +92,7 @@ files:
91
92
  - lib/kartograph/property.rb
92
93
  - lib/kartograph/property_collection.rb
93
94
  - lib/kartograph/root_key.rb
95
+ - lib/kartograph/scope_proxy.rb
94
96
  - lib/kartograph/sculptor.rb
95
97
  - lib/kartograph/version.rb
96
98
  - spec/lib/kartograph/artist_spec.rb
@@ -99,6 +101,7 @@ files:
99
101
  - spec/lib/kartograph/property_collection_spec.rb
100
102
  - spec/lib/kartograph/property_spec.rb
101
103
  - spec/lib/kartograph/root_key_spec.rb
104
+ - spec/lib/kartograph/scope_proxy_spec.rb
102
105
  - spec/lib/kartograph/sculptor_spec.rb
103
106
  - spec/spec_helper.rb
104
107
  - spec/support/dsl_contexts.rb
@@ -136,6 +139,7 @@ test_files:
136
139
  - spec/lib/kartograph/property_collection_spec.rb
137
140
  - spec/lib/kartograph/property_spec.rb
138
141
  - spec/lib/kartograph/root_key_spec.rb
142
+ - spec/lib/kartograph/scope_proxy_spec.rb
139
143
  - spec/lib/kartograph/sculptor_spec.rb
140
144
  - spec/spec_helper.rb
141
145
  - spec/support/dsl_contexts.rb