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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/csvpack/pack.rb', line 51

def initialize( path )

  ## convenience
  ## - check: if path is a folder/directory
  ##    (auto-)add  /datapackage.json

  @meta = Meta.load_file( path )

  pack_dir = File.dirname(path)

  pp @meta

  ## read in tables
  @tables = []
  @meta.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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/csvpack/pack.rb', line 80

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

#metaObject

delegate known meta props (e.g. name, title, etc. - why? why not?)



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

def meta() @meta; end

#tableObject

convenience method - return first table



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

def table()   @tables[0]; end

#tablesObject



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

def tables()  @tables; end