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.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/datafile/datafile.rb', line 71

def initialize( opts={} )
  @opts     = opts
  @datasets = []
  @scripts  = []   ## calculation scripts (calc blocks)
  @inlines  = []   ## inline (setup) scripts (run before reading datasets)

  ## (target)name - return nil if noname (set/defined/assigned)
  @name  = opts[:name] || nil
  ## deps (dependencies) - note: always returns an array (empty array if no deps)
  @deps  = opts[:deps] || []     

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

Instance Attribute Details

#datasetsObject (readonly)

Returns the value of attribute datasets.



102
103
104
# File 'lib/datafile/datafile.rb', line 102

def datasets
  @datasets
end

#depsObject (readonly)

dep(endencies)



106
107
108
# File 'lib/datafile/datafile.rb', line 106

def deps
  @deps
end

#inlinesObject (readonly)

inline script blocks – use before?? run before datasets



104
105
106
# File 'lib/datafile/datafile.rb', line 104

def inlines
  @inlines
end

#nameObject (readonly)

Returns the value of attribute name.



105
106
107
# File 'lib/datafile/datafile.rb', line 105

def name
  @name
end

#scriptsObject (readonly)

calc(ulation) scripts (calc blocks)



103
104
105
# File 'lib/datafile/datafile.rb', line 103

def scripts
  @scripts
end

#workerObject

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



108
109
110
# File 'lib/datafile/datafile.rb', line 108

def worker
  @worker
end

Class Method Details

.load(code) ⇒ Object

another convenience method - use like Datafile.load()



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/datafile/datafile.rb', line 55

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
  ## check for auto-configure (just guessing)
  ##   zip or file worker
  datafile.guess_file_or_zip_worker
  datafile
end

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

convenience method - use like Datafile.load_file()



49
50
51
52
# File 'lib/datafile/datafile.rb', line 49

def self.load_file( path='./Datafile' )
  code = File.read_utf8( path )
  self.load( code )
end

Instance Method Details

#calcObject



131
132
133
134
# File 'lib/datafile/datafile.rb', line 131

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

#downloadObject



120
121
122
123
124
# File 'lib/datafile/datafile.rb', line 120

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



136
137
138
139
140
# File 'lib/datafile/datafile.rb', line 136

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

#guess_file_or_zip_workerObject

change/rename to configure_file_or_zip_worker - why? why not??



90
91
92
93
94
95
96
97
98
99
# File 'lib/datafile/datafile.rb', line 90

def guess_file_or_zip_worker   ## change/rename to configure_file_or_zip_worker - why? why not??
  ## if opts file or zip exists do NOT change (assume set manually)
  return  if @opts[:file] || @opts[:zip]

  ## for now only change if single (just 1) dataset and it's present
  if @datasets.size == 1 && @datasets[0].file?
    puts "  bingo!! assume (in-situ) datafile; use file workers"
    @worker = FileWorker.new( self )
  end
end

#readObject



126
127
128
129
# File 'lib/datafile/datafile.rb', line 126

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

#runObject



111
112
113
114
115
116
117
# File 'lib/datafile/datafile.rb', line 111

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
  calc         # step 3 - run calc(ulations) scripts
  logger.info( "[datafile] end - run" )
end