Class: CsvPack::Pack

Inherits:
Object
  • Object
show all
Defined in:
lib/csvpack/pack.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Pack

load (tabular) datapackage into memory



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/csvpack/pack.rb', line 10

def initialize( path )

  ## convenience

  ## - check: if path is a folder/directory

  ##    (auto-)add  /datapackage.json


  text = File.open( path, 'r:utf-8' ).read
  @h = JSON.parse( text )

  pack_dir = File.dirname(path)

  ## pp @h


  ## read in tables

  @tables = []
  @h['resources'].each do |r|
    ## build table data

    @tables << build_tab( r, pack_dir )
  end

  ## pp @tables

end

Instance Method Details

#build_tab(h, pack_dir) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/csvpack/pack.rb', line 41

def build_tab( h, pack_dir )
  name          = h['name']
  relative_path = h['path']

  if relative_path.nil?
    relative_path = "#{name}.csv"
    puts "  warn: no path defined; using fallback '#{relative_path}'"
  end

  puts "  reading resource (table) #{name} (#{relative_path})..."
  pp h

  path = "#{pack_dir}/#{relative_path}"
  text = File.open( path, 'r:utf-8' ).read
  tab = Tab.new( h, text )
  tab
end

#licenseObject



35
# File 'lib/csvpack/pack.rb', line 35

def license() @h['license']; end

#nameObject



33
# File 'lib/csvpack/pack.rb', line 33

def name()    @h['name']; end

#tableObject

convenience method - return first table



39
# File 'lib/csvpack/pack.rb', line 39

def table()   @tables[0]; end

#tablesObject



37
# File 'lib/csvpack/pack.rb', line 37

def tables()  @tables; end

#titleObject



34
# File 'lib/csvpack/pack.rb', line 34

def title()   @h['title']; end