Class: Ksef::FA3::DataBlock

Inherits:
Data
  • Object
show all
Includes:
Canonical
Defined in:
lib/ksef/fa3/data_block.rb,
lib/ksef/fa3/data_block.rb

Overview

Construction, invariants and serialisation for DataBlock.

Constant Summary collapse

NEEDS_METADATA =
"An attachment data block needs at least one metadata entry. " \
"`MetaDane` is minOccurs=1 within `BlokDanych`."
TOO_MANY_PARAGRAPHS =
"`Tekst` holds at most 10 `Akapit` paragraphs."
MAX_PARAGRAPHS =

Akapit is maxOccurs="10", and the ceiling is low enough to hit by accident.

10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Canonical

#with

Constructor Details

#initialize(metadata:, heading: nil, paragraphs: [], tables: []) ⇒ DataBlock

Returns a new instance of DataBlock.

Parameters:

  • metadata (Hash{String=>String}, MetaEntry, Array<MetaEntry>) —

    MetaDane, 1..1000. A Hash is accepted for convenience and converted to ordered pairs; see MetaEntry for why the pairs are what gets stored.

  • heading (String, nil) (defaults to: nil) —

    ZNaglowek, TZnakowy512

  • paragraphs (String, Array<String>) (defaults to: []) —

    Tekst/Akapit, at most 10

  • tables (AttachmentTable, Array<AttachmentTable>) (defaults to: []) —

    Tabela, 0..1000

Raises:



36
37
38
39
40
41
42
43
44
# File 'lib/ksef/fa3/data_block.rb', line 36

def initialize(metadata:, heading: nil, paragraphs: [], tables: [])
  entries = self.class.entries()
  text = self.class.paragraphs_from(paragraphs)
  raise ValidationError, NEEDS_METADATA if entries.empty?
  raise ValidationError, TOO_MANY_PARAGRAPHS if text.size > MAX_PARAGRAPHS

  super(metadata: entries.dup.freeze, heading: Formatting.text(heading),
        paragraphs: text, tables: Correction.wrap(tables).dup.freeze)
end

Instance Attribute Details

#heading ⇒ Object (readonly)

Returns the value of attribute heading

Returns:

  • (Object) —

    the current value of heading



15
16
17
# File 'lib/ksef/fa3/data_block.rb', line 15

def heading
  @heading
end

#metadata ⇒ Object (readonly)

Returns the value of attribute metadata

Returns:

  • (Object) —

    the current value of metadata



15
16
17
# File 'lib/ksef/fa3/data_block.rb', line 15

def 
  @metadata
end

#paragraphs ⇒ Object (readonly)

Returns the value of attribute paragraphs

Returns:

  • (Object) —

    the current value of paragraphs



15
16
17
# File 'lib/ksef/fa3/data_block.rb', line 15

def paragraphs
  @paragraphs
end

#tables ⇒ Object (readonly)

Returns the value of attribute tables

Returns:

  • (Object) —

    the current value of tables



15
16
17
# File 'lib/ksef/fa3/data_block.rb', line 15

def tables
  @tables
end

Class Method Details

.entries(metadata) ⇒ Object

Accepts the ergonomic form and the faithful one. A Hash cannot express a repeated key, which the schema permits — so it is sugar for building, never what parsing produces.



55
56
57
58
59
# File 'lib/ksef/fa3/data_block.rb', line 55

def self.entries()
  return .map { |key, value| MetaEntry.new(key: key, value: value) } if .is_a?(Hash)

  Correction.wrap()
end

.paragraphs_from(paragraphs) ⇒ Object

compact, because a nil paragraph is nothing rather than an empty one — unlike a table cell, where nil becomes "" to keep the row's width. Paragraphs have no positional pairing to preserve, so dropping one loses nothing.



49
50
51
# File 'lib/ksef/fa3/data_block.rb', line 49

def self.paragraphs_from(paragraphs)
  Correction.wrap(paragraphs).map { |paragraph| Formatting.text(paragraph) }.compact.freeze
end

Instance Method Details

#to_fa3 ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/ksef/fa3/data_block.rb', line 65

def to_fa3
  {
    "ZNaglowek" => heading,
    "MetaDane" => .map { |e| e.to_fa3(key_element: "ZKlucz", value_element: "ZWartosc") },
    "Tekst" => paragraphs.empty? ? nil : { "Akapit" => paragraphs },
    "Tabela" => tables.map(&:to_fa3)
  }.compact
end

#to_h_metadata ⇒ Hash{String=>String}

Returns the metadata as a Hash. Lossy where a key repeats; read #metadata when that matters.

Returns:

  • (Hash{String=>String}) —

    the metadata as a Hash. Lossy where a key repeats; read #metadata when that matters.



63
# File 'lib/ksef/fa3/data_block.rb', line 63

def  = .to_h { |entry| [entry.key, entry.value] }