Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply defaults for Resource and Package #45

Merged
merged 1 commit into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/datapackage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'ruby_dig'
require 'tableschema'

require 'datapackage/defaults'
require 'datapackage/helpers'
require 'datapackage/version'
require 'datapackage/exceptions'
Expand Down
27 changes: 27 additions & 0 deletions lib/datapackage/defaults.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module DataPackage
DEFAULTS = {
resource: {
profile: 'data-resource',
tabular_profile: 'tabular-data-resource',
encoding: 'utf-8',
},
package: {
profile: 'data-package',
},
schema: {
format: 'default',
type: 'string',
missing_values: [''],
},
dialect: {
delimiter: ',',
doubleQuote: true,
lineTerminator: '\r\n',
quoteChar: '"',
escapeChar: '\\',
skipInitialSpace: true,
header: true,
caseSensitiveHeader: false,
},
}.freeze
end
11 changes: 5 additions & 6 deletions lib/datapackage/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(descriptor = nil, opts: {})
@opts = opts
@dead_resources = []
self.merge! parse_package(descriptor)
@profile = DataPackage::Profile.new(self.fetch('profile', 'data-package'))
@profile = DataPackage::Profile.new(self.fetch('profile', DataPackage::DEFAULTS[:package][:profile]))
define_properties!
load_resources!
end
Expand Down Expand Up @@ -100,7 +100,6 @@ def update_resources!
load_resource(resource)
rescue ResourceException
@dead_resources << resource['path']
nil
end
end
end
Expand All @@ -113,14 +112,14 @@ def load_resource(resource)
end
end

def default_value(profile_data)
case profile_data['type']
when 'string'
nil
def default_value(field_data)
case field_data['type']
when 'array'
[]
when 'object'
{}
else
nil
end
end

Expand Down
29 changes: 25 additions & 4 deletions lib/datapackage/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ class Resource < Hash
attr_reader :name, :profile, :source, :source_type, :valid, :errors

def initialize(resource, base_path = '')
resource = dereference_descriptor(resource, base_path: base_path,
self.merge! dereference_descriptor(resource, base_path: base_path,
reference_fields: ['schema', 'dialect'])
self.merge! resource
@profile = DataPackage::Profile.new(self.fetch('profile', 'data-resource'))
apply_defaults!
@profile = DataPackage::Profile.new(self['profile'])
@name = self['name']
get_source!(base_path)
apply_table_defaults! if self.tabular?
end

def table
@table ||= TableSchema::Table.new(self.source, self['schema']) if tabular?
end

def tabular?
tabular_profile = 'tabular-data-resource'
tabular_profile = DataPackage::DEFAULTS[:resource][:tabular_profile]
return true if @profile.name == tabular_profile
return true if DataPackage::Profile.new(tabular_profile).valid?(self)
false
Expand Down Expand Up @@ -53,5 +54,25 @@ def get_source!(base_path)
end
end

def apply_defaults!
self['profile'] ||= DataPackage::DEFAULTS[:resource][:profile]
self['encoding'] ||= DataPackage::DEFAULTS[:resource][:encoding]
end

def apply_table_defaults!
if self.fetch('schema', nil)
self['schema']['missingValues'] = DataPackage::DEFAULTS[:schema][:missing_values]
self['schema'].fetch('fields', []).each do |field_descriptor|
field_descriptor['type'] ||= DataPackage::DEFAULTS[:schema][:type]
field_descriptor['format'] ||= DataPackage::DEFAULTS[:schema][:format]
end
end

if self.fetch('dialect', nil)
DataPackage::DEFAULTS[:dialect].each do |key, val|
self['dialect'][key.to_s] ||= val
end
end
end
end
end
29 changes: 20 additions & 9 deletions spec/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
let(:tabular_resource) {
{
'name'=> 'tabular_resource',
'data'=> [['first', 'second'], [1,2]],
'data'=> [['str', 'int'], [1,2]],
'schema'=> {
'fields'=> [
{
'name'=> 'first',
'type'=> 'integer',
'name'=> 'str',
},
{
'name'=> 'second',
'name'=> 'int',
'type'=> 'integer',
}
]
},
'profile'=> 'tabular-data-resource',
'profile'=> DataPackage::DEFAULTS[:resource][:tabular_profile],
}
}

Expand All @@ -30,14 +29,26 @@
expect{ DataPackage::Resource.new(resource_hash) }.to raise_error(DataPackage::ResourceException)
end

it 'returns the resource' do
it 'extends the resource with defaults' do
resource_hash = {
'name'=> 'resource',
'data'=> 'whevs'
'data'=> 'whevs',
}
expected_resource = resource_hash.merge!({
'profile'=> DataPackage::DEFAULTS[:resource][:profile],
'encoding'=> DataPackage::DEFAULTS[:resource][:encoding],
})
resource = DataPackage::Resource.new(resource_hash)

expect(resource).to eq(resource_hash)
expect(resource).to eq(expected_resource)
end

it 'extends a tabular resource with table defaults' do
resource = DataPackage::Resource.new(tabular_resource)

expect(resource['schema']['missingValues']).to eq(DataPackage::DEFAULTS[:schema][:missing_values])
expect(resource['schema']['fields'][0]['type']).to eq(DataPackage::DEFAULTS[:schema][:type])
expect(resource['schema']['fields'][0]['format']).to eq(DataPackage::DEFAULTS[:schema][:format])
end

context 'remote resource' do
Expand Down Expand Up @@ -178,7 +189,7 @@

it 'is true for resources that comply with the tabular profile' do
resource = DataPackage::Resource.new(tabular_resource.merge({
'profile'=>'data-resource',
'profile'=> DataPackage::DEFAULTS[:resource][:profile],
}))

expect(resource.tabular?).to be true
Expand Down