Class: Ksef::FA3::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/ksef/fa3/serializer.rb

Overview

Writes an FA(3) document (DESIGN.md §7.5).

Takes a nested structure keyed by XSD element names and emits XML. Element order is never hand-written here: it comes from Generated::Types, which is derived from the pinned schema. KSeF rejects out-of-order elements, so ordering is a correctness property, and the only way to keep it right through future schema revisions is to regenerate rather than to maintain a list by hand (§7.1).

Consequences of that design worth knowing:

- Keys the schema does not define at that position raise, rather than being
silently dropped. A typo in an element name would otherwise produce a document
that is missing a field and valid-looking.
- The input order of the structure is irrelevant. Callers describe *what* the
invoice contains; the schema decides where it goes.

Defined Under Namespace

Classes: Element

Constant Summary collapse

NAMESPACE =
"http://crd.gov.pl/wzor/2025/06/25/13775/"
ROOT =
"Faktura"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Serializer

Returns a new instance of Serializer.

Parameters:

  • content (Hash) —

    nested, keyed by XSD element name



56
57
58
# File 'lib/ksef/fa3/serializer.rb', line 56

def initialize(content)
  @content = content
end

Class Method Details

.child_type_key(parent_key, particle) ⇒ Object

Named types are keyed by name, anonymous ones by path (§7.1).

Public because ModelValidator walks the same tree: its Adnotacje check has to resolve each nested element's type exactly as the serializer will, or the two disagree about what is permitted where.



48
49
50
51
52
53
# File 'lib/ksef/fa3/serializer.rb', line 48

def self.child_type_key(parent_key, particle)
  named = particle[:type].to_s.sub(/\Atns:/, "")
  return named if Generated::Types[named]

  "#{parent_key}/#{particle[:name]}"
end

Instance Method Details

#to_xml ⇒ String

Returns UTF-8 XML with an explicit declaration.

Returns:

  • (String) —

    UTF-8 XML with an explicit declaration



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ksef/fa3/serializer.rb', line 61

def to_xml
  document = Nokogiri::XML::Document.new
  document.encoding = "UTF-8"

  root = document.create_element(ROOT)
  root.add_namespace_definition(nil, NAMESPACE)
  document.root = root

  write_children(document, root, ROOT, @content)
  qualify(root, root.namespace)

  document.to_xml(indent: 2)
end