Class: Metanorma::Collection::Config::Config

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Includes:
Converters
Defined in:
lib/metanorma/collection/config/config.rb

Constant Summary

Constants included from Converters

Metanorma::Collection::Config::Converters::V1_BIBDATA_KEYS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Converters

#bibdata_from_xml, #bibdata_from_yaml, #bibdata_to_xml, #bibdata_to_yaml, #bibdata_yaml_v1_format?, #documents_from_xml, #documents_to_xml, #documents_to_xml?, #force_primary_docidentifier_xml, #force_primary_docidentifier_yaml, #nop_to_xml, #nop_to_yaml

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



17
18
19
# File 'lib/metanorma/collection/config/config.rb', line 17

def collection
  @collection
end

#from_xmlObject

Returns the value of attribute from_xml.



17
18
19
# File 'lib/metanorma/collection/config/config.rb', line 17

def from_xml
  @from_xml
end

#pathObject

Returns the value of attribute path.



17
18
19
# File 'lib/metanorma/collection/config/config.rb', line 17

def path
  @path
end

Class Method Details

.flavor_from_yaml(yaml) ⇒ Object



138
139
140
141
142
# File 'lib/metanorma/collection/config/config.rb', line 138

def self.flavor_from_yaml(yaml)
  yaml["directives"]&.detect do |x|
    x.is_a?(Hash) && x.has_key?("flavor")
  end&.dig("flavor")&.upcase
end

.flavor_to_bibdata(file) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/metanorma/collection/config/config.rb', line 144

def self.flavor_to_bibdata(file)
  # propagate flavor from directives to bibdata
  yaml = YAML.safe_load(file)
  flavor = flavor_from_yaml(yaml) or return file
  yaml["bibdata"] or return file
  yaml["bibdata"]["ext"] ||= {}
  yaml["bibdata"]["ext"]["flavor"] ||= flavor
  yaml.to_yaml
end

.from_yaml(file) ⇒ Object



154
155
156
157
# File 'lib/metanorma/collection/config/config.rb', line 154

def self.from_yaml(file)
  file = flavor_to_bibdata(file)
  super
end

Instance Method Details

#content_to_xml(model, parent, doc, type) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/metanorma/collection/config/config.rb', line 100

def content_to_xml(model, parent, doc, type)
  x = model.send("#{type}_content") or return
  n = Nokogiri::XML(x)
  elem = if n.elements.size == 1
           "<#{type}-content>#{x}</#{type}-content>" # n.root
         else
           b = Nokogiri::XML::Builder.new
           model.collection.content_to_xml(type, b)
           b.parent.elements.first
         end
  doc.add_element(parent, elem)
end

#directives_from_yaml(model, value) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/metanorma/collection/config/config.rb', line 117

def directives_from_yaml(model, value)
  model.directive = value&.each_with_object([]) do |v, m|
    m << case v
         when String then Directive.new(key: v)
         when Hash
           k = v.keys.first
           Directive.new(key: k, value: v[k])
         end
  end
end

#directives_to_yaml(model, doc) ⇒ Object



128
129
130
131
132
# File 'lib/metanorma/collection/config/config.rb', line 128

def directives_to_yaml(model, doc)
  doc["directives"] = model.directive.each_with_object([]) do |d, m|
    m << { d.key => d.value }
  end
end

#entry_from_yaml(model, value) ⇒ Object



134
135
136
# File 'lib/metanorma/collection/config/config.rb', line 134

def entry_from_yaml(model, value)
  model.manifest ||= value
end

#final_from_xml(model, node) ⇒ Object



113
114
115
# File 'lib/metanorma/collection/config/config.rb', line 113

def final_from_xml(model, node)
  model.final_content = node.children.map(&:to_xml).join
end

#final_to_xml(model, parent, doc) ⇒ Object



96
97
98
# File 'lib/metanorma/collection/config/config.rb', line 96

def final_to_xml(model, parent, doc)
  content_to_xml(model, parent, doc, "final")
end

#manifest_from_xml(model, node) ⇒ Object



79
80
81
# File 'lib/metanorma/collection/config/config.rb', line 79

def manifest_from_xml(model, node)
  model.manifest = Manifest.from_xml(node.to_xml)
end

#manifest_to_xml(model, parent, doc) ⇒ Object



83
84
85
86
# File 'lib/metanorma/collection/config/config.rb', line 83

def manifest_to_xml(model, parent, doc)
  model.collection&.manifest&.clean_manifest(model.manifest)
  doc.add_element(parent, model.manifest.to_xml)
end

#prefatory_from_xml(model, node) ⇒ Object



88
89
90
# File 'lib/metanorma/collection/config/config.rb', line 88

def prefatory_from_xml(model, node)
  model.prefatory_content = node.children.map(&:to_xml).join
end

#prefatory_to_xml(model, parent, doc) ⇒ Object



92
93
94
# File 'lib/metanorma/collection/config/config.rb', line 92

def prefatory_to_xml(model, parent, doc)
  content_to_xml(model, parent, doc, "prefatory")
end