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

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

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



19
20
21
# File 'lib/metanorma/collection/config/config.rb', line 19

def collection
  @collection
end

#from_xmlObject

Returns the value of attribute from_xml.



19
20
21
# File 'lib/metanorma/collection/config/config.rb', line 19

def from_xml
  @from_xml
end

#pathObject

Returns the value of attribute path.



19
20
21
# File 'lib/metanorma/collection/config/config.rb', line 19

def path
  @path
end

Class Method Details

.flavor_from_yaml(yaml) ⇒ Object



136
137
138
139
140
# File 'lib/metanorma/collection/config/config.rb', line 136

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



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

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



152
153
154
155
# File 'lib/metanorma/collection/config/config.rb', line 152

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

Instance Method Details

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



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

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

#directives_from_yaml(model, value) ⇒ Object



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

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



126
127
128
129
130
# File 'lib/metanorma/collection/config/config.rb', line 126

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



132
133
134
# File 'lib/metanorma/collection/config/config.rb', line 132

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

#final_from_xml(model, node) ⇒ Object



111
112
113
# File 'lib/metanorma/collection/config/config.rb', line 111

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

#final_to_xml(model, parent, doc) ⇒ Object



94
95
96
# File 'lib/metanorma/collection/config/config.rb', line 94

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

#manifest_from_xml(model, node) ⇒ Object



77
78
79
# File 'lib/metanorma/collection/config/config.rb', line 77

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

#manifest_to_xml(model, _parent, doc) ⇒ Object



81
82
83
84
# File 'lib/metanorma/collection/config/config.rb', line 81

def manifest_to_xml(model, _parent, doc)
  model.collection&.manifest&.clean_manifest(model.manifest)
  add_raw_xml_element(doc, model.manifest.to_xml)
end

#prefatory_from_xml(model, node) ⇒ Object



86
87
88
# File 'lib/metanorma/collection/config/config.rb', line 86

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

#prefatory_to_xml(model, parent, doc) ⇒ Object



90
91
92
# File 'lib/metanorma/collection/config/config.rb', line 90

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