Class: Datanorm::Document

Inherits:
Object
  • Object
show all
Includes:
Logging, Enumerable
Defined in:
lib/datanorm/document.rb

Overview

Loads and parses a datanorm file product by product.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

included

Constructor Details

#initialize(path:, timestamp: nil) ⇒ Document

Returns a new instance of Document.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/datanorm/document.rb', line 11

def initialize(path:, timestamp: nil)
  @path = path

  if timestamp
    # Re-use an existing workdir in case the preprocessing was already done earlier.
    @timestamp = timestamp
    @preprocessed = true
  else
    @timestamp = (Time.now.to_f * 1_000_000_000).to_i.to_s # Timestamp with nanoseconds
  end
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/datanorm/document.rb', line 9

def path
  @path
end

Instance Method Details

#dateObject



35
36
37
# File 'lib/datanorm/document.rb', line 35

def date
  file.date
end

#each(yield_progress: false) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/datanorm/document.rb', line 39

def each(yield_progress: false, &)
  unless @preprocessed
    ::Datanorm::Documents::Preprocess.call(file:, workdir:, yield_progress:, &)
    @preprocessed = true
  end

  ::Datanorm::Documents::Assemble.call(workdir:, yield_progress:, &)
ensure
  # At this point all yields have gone through and we can clean up.
  workdir.rmtree unless ENV['DEBUG_DATANORM']
end

#headerObject



23
24
25
# File 'lib/datanorm/document.rb', line 23

def header
  file.header
end

#titleObject



31
32
33
# File 'lib/datanorm/document.rb', line 31

def title
  file.title
end

#versionObject



27
28
29
# File 'lib/datanorm/document.rb', line 27

def version
  file.version
end

#workdirObject



51
52
53
# File 'lib/datanorm/document.rb', line 51

def workdir
  @workdir ||= Pathname.new('/tmp/datanorm_ruby').join(@timestamp)
end