schemad 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -2
- data/lib/schemad/extensions.rb +1 -1
- data/lib/schemad/normalizer.rb +23 -0
- data/lib/schemad/version.rb +1 -1
- data/spec/normalizer_spec.rb +22 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a89ab9d861274a3049d986b4eda13df1bb57235c
|
4
|
+
data.tar.gz: e42c0dc667171bfe6310cbe7d53f48b5215830ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7f01994442b648edf178dd18f059a9f1d95194f7c1cb61697a555620ace6bdcd8a7eb8e944f583f7e56da5d0ec197e25f5911408d24d06eee320879d3305943
|
7
|
+
data.tar.gz: a9a164dda791b2f616463af32e02491bfdadac91eb050cab555d45aca38ee1258f4ee03a124b980c27e89724b0720e72ffa0846aadfc8f99bf6565c25802397d
|
data/README.md
CHANGED
@@ -140,14 +140,17 @@ In step the Normalizers. For this example ee'd create two separate normalizers f
|
|
140
140
|
```ruby
|
141
141
|
class GitHubNormalizer < Schemad::Normalizer
|
142
142
|
normalize :id, key: :sha
|
143
|
-
normalize :url, key: :url
|
144
143
|
normalize :committer, key: "committer/name"
|
145
144
|
normalize :created_date, key: "committer/date"
|
146
145
|
normalize :comment, key: :message
|
146
|
+
|
147
|
+
include_fields :url
|
147
148
|
end
|
148
149
|
```
|
149
150
|
|
150
|
-
We could obviously also include additional data if we wanted. Notice you can use either symbols or strings as keys. If you want to do a deep traversal (more than one level), you will need to use strings with a "/" delimited path.
|
151
|
+
We could obviously also include additional data if we wanted. Notice you can use either symbols or strings as keys. If you want to do a deep traversal (more than one level), you will need to use strings with a "/" delimited path.
|
152
|
+
|
153
|
+
Also notice that if you want fields to be included 'as is', using the same key as the dataset provided, you can use the `include_fields` method. Normalizers _only return_ data that you specify.
|
151
154
|
|
152
155
|
Now for BitBucket:
|
153
156
|
|
data/lib/schemad/extensions.rb
CHANGED
data/lib/schemad/normalizer.rb
CHANGED
@@ -48,6 +48,18 @@ module Schemad
|
|
48
48
|
normalized
|
49
49
|
end
|
50
50
|
|
51
|
+
def reverse(data)
|
52
|
+
normalized = {}
|
53
|
+
|
54
|
+
allowed_attributes.each do |key|
|
55
|
+
from_key = normalizers[key]
|
56
|
+
|
57
|
+
normalized.deep_merge! nested_hash_from_path(key, data[from_key])
|
58
|
+
end
|
59
|
+
|
60
|
+
normalized
|
61
|
+
end
|
62
|
+
|
51
63
|
def normalizers
|
52
64
|
self.class.instance_variable_get(:@normalizers)
|
53
65
|
end
|
@@ -57,6 +69,7 @@ module Schemad
|
|
57
69
|
end
|
58
70
|
|
59
71
|
private
|
72
|
+
|
60
73
|
def path_steps(key)
|
61
74
|
key.to_s.split(DELIMITER)
|
62
75
|
end
|
@@ -74,6 +87,16 @@ module Schemad
|
|
74
87
|
end
|
75
88
|
end
|
76
89
|
|
90
|
+
def nested_hash_from_path(path, value)
|
91
|
+
build_hash path_steps(path), value, {}
|
92
|
+
end
|
93
|
+
|
94
|
+
def build_hash(steps, value, accum)
|
95
|
+
step = steps.shift
|
96
|
+
accum[step] = steps.empty? ? value : build_hash(steps, value, {})
|
97
|
+
accum
|
98
|
+
end
|
99
|
+
|
77
100
|
def search_data(steps, data)
|
78
101
|
step = steps.shift
|
79
102
|
return data unless step
|
data/lib/schemad/version.rb
CHANGED
data/spec/normalizer_spec.rb
CHANGED
@@ -34,6 +34,21 @@ describe Schemad::Normalizer do
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
+
context "reverse normalization" do
|
38
|
+
Given(:normalized) { normalizer.normalize(data) }
|
39
|
+
Given(:expected) { { "Middle Earth" => "COORDINATES", "roads" => 50 } }
|
40
|
+
|
41
|
+
context "for given keys" do
|
42
|
+
When(:reversed) { normalizer.reverse(normalized) }
|
43
|
+
Then { reversed.should == expected }
|
44
|
+
end
|
45
|
+
|
46
|
+
context "ignores extra keys" do
|
47
|
+
When(:reversed) { normalizer.reverse(normalized.merge({ billy: "joel" })) }
|
48
|
+
Then { reversed.should == expected }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
37
52
|
context "additional fields" do
|
38
53
|
Given { normalizer.class.include_fields :beasts, :cool }
|
39
54
|
When(:normalized) { normalizer.normalize(data) }
|
@@ -106,5 +121,12 @@ describe Schemad::Normalizer do
|
|
106
121
|
Then { results[:display_name].should == "Joseph Walton" }
|
107
122
|
end
|
108
123
|
|
124
|
+
context "can reverse into nested key" do
|
125
|
+
Given(:normalizer) { BucketNormalizer.new }
|
126
|
+
Given(:normalized) { normalizer.normalize(data) }
|
127
|
+
When(:reversed) { normalizer.reverse(normalized) }
|
128
|
+
Then { reversed['author']['user']['username'].should == "jwalton" }
|
129
|
+
And { reversed['author']['user']['links']['avatar']['href'] == "funk_blue.png" }
|
130
|
+
end
|
109
131
|
end
|
110
132
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schemad
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luke van der Hoeven
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|