data_package 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/data_package/resource.rb +4 -4
- data/lib/data_package/schema.rb +10 -1
- data/lib/data_package/version.rb +1 -1
- data/spec/data_package/resource_spec.rb +20 -5
- data/spec/data_package/schema_spec.rb +14 -1
- 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: c3381512a80b7e68fe5c76b49def2cdca79b1f43
|
4
|
+
data.tar.gz: 1a1c644fdd7a00dee1a712cd9b84f1181ffb692c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ddd631e94965058ff2c6e674570d6ca2759190de615661c67e653fa65a0cca271cde2d970b42525c0232365346d7a18203e60e4f0640bcae5f0ed3656cb1ba49
|
7
|
+
data.tar.gz: 5136e1b5b3e3e6188db29f76c238589cf3b07f2a891625e2813f971bde614a1ec2e0bf5fd6f86f1dcb72c7274c7e9c2fce99740470a62b6883bc88a8ab1813e9
|
@@ -1,7 +1,9 @@
|
|
1
1
|
require 'data_kit'
|
2
2
|
|
3
3
|
module DataPackage
|
4
|
-
class Resource < Base
|
4
|
+
class Resource < Base
|
5
|
+
include Enumerable
|
6
|
+
|
5
7
|
attr_optional :name
|
6
8
|
|
7
9
|
attr_required :data,
|
@@ -45,7 +47,7 @@ module DataPackage
|
|
45
47
|
# this makes it possible for scripts to rely on a file location
|
46
48
|
#
|
47
49
|
|
48
|
-
def
|
50
|
+
def each(&block)
|
49
51
|
case data_source_type
|
50
52
|
when :data
|
51
53
|
data.each(&block)
|
@@ -65,8 +67,6 @@ module DataPackage
|
|
65
67
|
end
|
66
68
|
end
|
67
69
|
|
68
|
-
private
|
69
|
-
|
70
70
|
def data_source_type
|
71
71
|
if data
|
72
72
|
:data
|
data/lib/data_package/schema.rb
CHANGED
@@ -4,7 +4,8 @@ module DataPackage
|
|
4
4
|
fields.collect(&:to_hash)
|
5
5
|
}
|
6
6
|
|
7
|
-
attr_optional :primary_key, :key => 'primaryKey'
|
7
|
+
attr_optional :primary_key, :key => 'primaryKey',
|
8
|
+
:serialize => Proc.new{|pkey| pkey.length == 1 ? pkey.first : pkey }
|
8
9
|
|
9
10
|
def initialize(attrs = {})
|
10
11
|
@fields ||= []
|
@@ -15,5 +16,13 @@ module DataPackage
|
|
15
16
|
def fields=(json)
|
16
17
|
@fields = json.collect{|f| Field.new(f)}
|
17
18
|
end
|
19
|
+
|
20
|
+
def primary_key=(json)
|
21
|
+
if json.is_a?(Array)
|
22
|
+
@primary_key = json
|
23
|
+
else
|
24
|
+
@primary_key = [json]
|
25
|
+
end
|
26
|
+
end
|
18
27
|
end
|
19
28
|
end
|
data/lib/data_package/version.rb
CHANGED
@@ -12,7 +12,15 @@ describe DataPackage::Resource do
|
|
12
12
|
resource.schema.fields.length.should == 10
|
13
13
|
resource.schema.primary_key.should == ['id']
|
14
14
|
|
15
|
-
|
15
|
+
modified_resource = standard_resource.merge(
|
16
|
+
'format' => 'csv',
|
17
|
+
'dialect' => DataPackage::Dialect.new.to_hash,
|
18
|
+
'schema' => standard_resource['schema'].merge(
|
19
|
+
'primaryKey' => 'id'
|
20
|
+
)
|
21
|
+
)
|
22
|
+
|
23
|
+
resource.to_hash.should == modified_resource
|
16
24
|
end
|
17
25
|
|
18
26
|
it "should initialize and deserialize a remote resource" do
|
@@ -26,15 +34,22 @@ describe DataPackage::Resource do
|
|
26
34
|
resource.schema.fields.length.should == 3
|
27
35
|
resource.schema.primary_key.should == ['id']
|
28
36
|
|
29
|
-
|
37
|
+
modified_resource = remote_resource.merge(
|
38
|
+
'format' => 'csv',
|
39
|
+
'schema' => remote_resource['schema'].merge(
|
40
|
+
'primaryKey' => 'id'
|
41
|
+
)
|
42
|
+
)
|
43
|
+
|
44
|
+
resource.to_hash.should == modified_resource.merge('format' => 'csv')
|
30
45
|
end
|
31
46
|
|
32
|
-
describe "#
|
47
|
+
describe "#each" do
|
33
48
|
it "should enumerate over inline data" do
|
34
49
|
resource = DataPackage::Resource.new(base_path, inline_resource)
|
35
50
|
|
36
51
|
row_count = 0
|
37
|
-
resource.
|
52
|
+
resource.each do |row|
|
38
53
|
row_count += 1
|
39
54
|
end
|
40
55
|
row_count.should == 3
|
@@ -44,7 +59,7 @@ describe DataPackage::Resource do
|
|
44
59
|
resource = DataPackage::Resource.new(base_path, standard_resource)
|
45
60
|
|
46
61
|
row_count = 0
|
47
|
-
resource.
|
62
|
+
resource.each { |row| row_count += 1 }
|
48
63
|
row_count.should == 10
|
49
64
|
end
|
50
65
|
|
@@ -20,6 +20,19 @@ describe DataPackage::Schema do
|
|
20
20
|
schema.fields.length.should == 1
|
21
21
|
schema.fields.first.name.should == 'income'
|
22
22
|
schema.primary_key.should == ['income']
|
23
|
-
|
23
|
+
|
24
|
+
modified_json = json.merge('primaryKey' => 'income')
|
25
|
+
schema.to_json.should == Yajl::Encoder.encode(modified_json, :pretty => true)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should initialize with string primaryKey" do
|
29
|
+
modified_json = json.merge('primaryKey' => 'income')
|
30
|
+
|
31
|
+
schema = DataPackage::Schema.new(modified_json)
|
32
|
+
|
33
|
+
schema.fields.length.should == 1
|
34
|
+
schema.fields.first.name.should == 'income'
|
35
|
+
schema.primary_key.should == ['income']
|
36
|
+
schema.to_json.should == Yajl::Encoder.encode(modified_json, :pretty => true)
|
24
37
|
end
|
25
38
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_package
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mode Analytics
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rcsv
|