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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e567c149226c03fee2d2c25f524d46decb1b2963
4
- data.tar.gz: d98b2f8edf777fb58b46d0cda63431f4f98ba229
3
+ metadata.gz: c3381512a80b7e68fe5c76b49def2cdca79b1f43
4
+ data.tar.gz: 1a1c644fdd7a00dee1a712cd9b84f1181ffb692c
5
5
  SHA512:
6
- metadata.gz: 6806f16ae383a4a8afecf2f5f38cd26ec8a47d64ea76b16f824c81e75d3f086c31de7a7a43380f440b7fa2d06d2f2b09b7ee7f41792ae6686224588a39c9bbff
7
- data.tar.gz: a120c9dd83674e0864d6f5d837bbe273a2239f91ea7d1373911b2a69cd6febc49065c2d03c7b92b6dfd6b180ea0c875b6a3219ff46b081a14afee154a6c8f1db
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 each_row(&block)
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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module DataPackage
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -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
- resource.to_hash.should == standard_resource.merge('format' => 'csv', 'dialect' => DataPackage::Dialect.new.to_hash)
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
- resource.to_hash.should == remote_resource.merge('format' => 'csv')
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 "#each_row" do
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.each_row do |row|
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.each_row { |row| row_count += 1 }
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
- schema.to_json.should == Yajl::Encoder.encode(json, :pretty => true)
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.8
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 00:00:00.000000000 Z
11
+ date: 2014-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rcsv