Class: Metanorma::CollectionManifest

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/collection_manifest.rb

Overview

Metanorma collection’s manifest

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level, title = nil, docref = [], manifest = []) ⇒ CollectionManifest

Returns a new instance of CollectionManifest.

Parameters:

  • level (String)
  • title (String, nil) (defaults to: nil)
  • docref (Array<Hash{String=>String}>) (defaults to: [])
  • manifest (Array<Metanorma::CollectionManifest>) (defaults to: [])


15
16
17
18
19
20
21
# File 'lib/metanorma/collection_manifest.rb', line 15

def initialize(level, title = nil, docref = [], manifest = [])
  @level = level
  @title = title
  @docref = docref
  @manifest = manifest
  @disambig = Util::DisambigFiles.new
end

Instance Attribute Details

#collectionMetanorma::Collection



9
10
11
# File 'lib/metanorma/collection_manifest.rb', line 9

def collection
  @collection
end

Class Method Details

.from_xml(mnf) ⇒ Metanorma::CollectionManifest

Parameters:

  • mnf (Nokogiri::XML::Element)

Returns:



36
37
38
39
40
41
# File 'lib/metanorma/collection_manifest.rb', line 36

def from_xml(mnf)
  level = mnf.at("level").text
  title = mnf.at("title")&.text
  manifest = mnf.xpath("xmlns:manifest").map { |m| from_xml(m) }
  new(level, title, parse_docref(mnf), manifest)
end

.from_yaml(mnf) ⇒ Metanorma::CollectionManifest

Parameters:

  • mnf (Nokogiri::XML::Element)

Returns:



26
27
28
29
30
31
32
# File 'lib/metanorma/collection_manifest.rb', line 26

def from_yaml(mnf)
  manifest = RelatonBib.array(mnf["manifest"]).map do |m|
    from_yaml m
  end
  docref = RelatonBib.array mnf["docref"]
  new(mnf["level"], mnf["title"], docref, manifest)
end

Instance Method Details

#docref_by_id(docid) ⇒ Object



97
98
99
100
101
# File 'lib/metanorma/collection_manifest.rb', line 97

def docref_by_id(docid)
  refs = docrefs
  dref = refs.detect { |k| k["identifier"] == docid }
  dref || docrefs.detect { |k| /^#{k["identifier"]}/ =~ docid }
end

#docrefsArray<Hash{String=>String}>

Returns:

  • (Array<Hash{String=>String}>)


90
91
92
93
94
95
# File 'lib/metanorma/collection_manifest.rb', line 90

def docrefs
  return @docrefs if @docrefs

  drfs = @docref.map { |dr| dr }
  @manifest.reduce(drfs) { |mem, mnf| mem + mnf.docrefs }
end

#documents(dir = "") ⇒ Hash<String, Metanorma::Document>

Parameters:

  • dir (String) (defaults to: "")

    path to collection

Returns:



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/metanorma/collection_manifest.rb', line 67

def documents(dir = "")
  docs = @docref.each_with_object({}) do |dr, m|
    dr["fileref"] or next m
    m[Util::key dr["identifier"]] = Document.parse_file(
      Util::rel_path_resolve(dir, dr["fileref"]),
      dr["attachment"], dr["identifier"], dr["index"]
    )
    m
  end
  @manifest.reduce(docs) { |mem, mnf| mem.merge mnf.documents(dir) }
end

#to_xml(builder) ⇒ Object

Parameters:

  • builder (Nokogiri::XML::Builder)


80
81
82
83
84
85
86
87
# File 'lib/metanorma/collection_manifest.rb', line 80

def to_xml(builder)
  builder.manifest do |b|
    b.level @level
    b.title @title if @title
    docref_to_xml b
    @manifest.each { |m| m.to_xml b }
  end
end