Skip to content

Commit

Permalink
Removed resource.table prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
roll committed Aug 30, 2017
1 parent d297b7e commit 0b17650
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,23 @@ first_resource.tabular?
first_resource.source
```

You can then read the source depending on its `source_type`: `inline`, `remote` or `local`.
You can then read the source depending on its type. For example if resource is local and not multipart it could by open as a file: `File.open(resource.source)`.

If a resource complies with the [Tabular Data Resource spec](https://specs.frictionlessdata.io/tabular-data-resource/) or uses the
`tabular-data-resource` [profile](#profiles) you can make a [TableSchema::Table](https://github.com/frictionlessdata/tableschema-rb) for it:
`tabular-data-resource` [profile](#profiles) you can read resource rows:

```ruby
package.resources[0].tabular?
table = package.resources[0].table
resoure = package.resources[0]
resource.tabular?
resource.headers
resource.schema

# Read the entire table at once
data = table.read
# Read the the whole rows at once
data = resource.read
data = resource.read(keyed: true)

# Or iterate through it
data = table.iter {|row| print row}
data = resource.iter {|row| print row}
```

See [TableSchema](https://github.com/frictionlessdata/tableschema-rb) documentation for other things you can do with tabular resource.
Expand Down
38 changes: 37 additions & 1 deletion lib/datapackage/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,40 @@ def tabular?

alias :tabular :tabular?

def headers
if !tabular
nil
end
get_table.headers
end

def schema
if !tabular
nil
end
get_table.schema
end

def iter(*args, &block)
if !tabular
message ='Methods iter/read are not supported for non tabular data'
raise ResourceException.new message
end
get_table.iter(*args, &block)
end

def read(*args, &block)
if !tabular
message ='Methods iter/read are not supported for non tabular data'
raise ResourceException.new message
end
get_table.read(*args, &block)
end

# Deprecated

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

# Private
Expand All @@ -90,6 +122,10 @@ def get_source!(base_path)
end
end

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

def apply_defaults!
self['profile'] ||= DataPackage::DEFAULTS[:resource][:profile]
self['encoding'] ||= DataPackage::DEFAULTS[:resource][:encoding]
Expand Down
13 changes: 13 additions & 0 deletions spec/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,17 @@
end

end

context 'read' do

it 'reads tabular data' do
resource = DataPackage::Resource.new(tabular_resource)
expect(resource.headers).to eq(['str', 'int'])
expect(resource.schema.field_names).to eq(['str', 'int'])
expect(resource.read).to eq([['1', 2]])
expect(resource.read(keyed: true)).to eq([{'str'=> '1', 'int'=> 2}])
end

end

end

0 comments on commit 0b17650

Please sign in to comment.