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: [])


13
14
15
16
17
18
# File 'lib/metanorma/collection_manifest.rb', line 13

def initialize(level, title = nil, docref = [], manifest = [])
  @level = level
  @title = title
  @docref = docref
  @manifest = manifest
end

Instance Attribute Details

#collectionMetanorma::Collection



7
8
9
# File 'lib/metanorma/collection_manifest.rb', line 7

def collection
  @collection
end

Class Method Details

.from_xml(mnf) ⇒ Metanorma::CollectionManifest

Parameters:

  • mnf (Nokogiri::XML::Element)

Returns:



33
34
35
36
37
38
# File 'lib/metanorma/collection_manifest.rb', line 33

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:



23
24
25
26
27
28
29
# File 'lib/metanorma/collection_manifest.rb', line 23

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

Instance Method Details

#docref_by_id(docid) ⇒ Object



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

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}>)


84
85
86
87
88
89
# File 'lib/metanorma/collection_manifest.rb', line 84

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 coolection

Returns:



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/metanorma/collection_manifest.rb', line 61

def documents(dir = "")
  docs = @docref.each_with_object({}) do |dr, m|
    next m unless dr["fileref"]

    m[dr["identifier"]] = Document.parse_file File.join(dir, dr["fileref"])
    m
  end
  @manifest.reduce(docs) do |mem, mnf|
    mem.merge mnf.documents(dir)
  end
end

#to_xml(builder) ⇒ Object

Parameters:

  • builder (Nokogiri::XML::Builder)


74
75
76
77
78
79
80
81
# File 'lib/metanorma/collection_manifest.rb', line 74

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