Class: Datafile::Datafile

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/datafile/datafile.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Datafile

Returns a new instance of Datafile.



28
29
30
31
32
33
34
35
36
37
# File 'lib/datafile/datafile.rb', line 28

def initialize( opts={} )
  @opts     = opts
  @datasets = []

  @worker = if opts[:file]
              FileWorker.new( self )
            else   ## default to zip worker for now
              ZipWorker.new( self )
            end
end

Instance Attribute Details

#datasetsObject (readonly)

Returns the value of attribute datasets.



39
40
41
# File 'lib/datafile/datafile.rb', line 39

def datasets
  @datasets
end

Class Method Details

.load(code) ⇒ Object

another convenience method - use like Datafile.load()



14
15
16
17
18
19
20
21
22
# File 'lib/datafile/datafile.rb', line 14

def self.load( code )
  builder = Builder.new
  builder.instance_eval( code )

  # Note: return datafile (of course, NOT the builder)
  #  if you want a builder use Datafile::Builder ;-)
  datafile = builder.datafile
  datafile
end

.load_file(path = './Datafile') ⇒ Object

convenience method - use like Datafile.load_file()



8
9
10
11
# File 'lib/datafile/datafile.rb', line 8

def self.load_file( path='./Datafile' )
  code = File.open( path, 'r:utf-8' ) { |f| f.read }
  self.load( code )
end

Instance Method Details

#downloadObject



63
64
65
66
67
# File 'lib/datafile/datafile.rb', line 63

def download
  logger.info( "[datafile] dowload" )
  @worker.download
  ## check: use @worker.download( @datasets) - why, why not??  link worker w/ datafile - why, why not??
end

#dumpObject



74
75
76
77
78
# File 'lib/datafile/datafile.rb', line 74

def dump
  ## for debugging dump datasets (note: will/might also check if zip exits)
  logger.info( "[datafile] dump datasets (for debugging)" )
  @worker.dump
end

#readObject



69
70
71
72
# File 'lib/datafile/datafile.rb', line 69

def read
  logger.info( "[datafile] read" )
  @worker.read
end

#runObject



55
56
57
58
59
60
# File 'lib/datafile/datafile.rb', line 55

def run
  logger.info( "[datafile] begin - run" )
  download     # step 1 - download zips for datasets
  read         # step 2 - read in datasets from zips  - note: includes running inlines
  logger.info( "[datafile] end - run" )
end

#worker=(value) ⇒ Object

lets you change worker - find a better way - how, why, why not??



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/datafile/datafile.rb', line 42

def worker=( value )  # lets you change worker - find a better way - how, why, why not??
  @worker =  if value.is_a?( Class )   ## let's you pass in FileWorker or ZipWorker etc.
               value.new( self )
             elsif value.to_sym == :file
               FileWorker.new( self )
             elsif value.to_sym == :zip
               ZipWorker.new( self )
             else
               value
             end
end