Class: Ksef::FA3::DataBlock
- Inherits:
-
Data
- Object
- Data
- Ksef::FA3::DataBlock
- 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 =
AkapitismaxOccurs="10", and the ceiling is low enough to hit by accident. 10
Instance Attribute Summary collapse
-
#heading ⇒ Object
readonly
Returns the value of attribute heading.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#paragraphs ⇒ Object
readonly
Returns the value of attribute paragraphs.
-
#tables ⇒ Object
readonly
Returns the value of attribute tables.
Class Method Summary collapse
-
.entries(metadata) ⇒ Object
Accepts the ergonomic form and the faithful one.
-
.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.
Instance Method Summary collapse
-
#initialize(metadata:, heading: nil, paragraphs: [], tables: []) ⇒ DataBlock
constructor
A new instance of DataBlock.
- #to_fa3 ⇒ Object
-
#to_h_metadata ⇒ Hash{String=>String}
The metadata as a Hash.
Methods included from Canonical
Constructor Details
#initialize(metadata:, heading: nil, paragraphs: [], tables: []) ⇒ DataBlock
Returns a new instance of DataBlock.
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
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
15 16 17 |
# File 'lib/ksef/fa3/data_block.rb', line 15 def @metadata end |
#paragraphs ⇒ Object (readonly)
Returns the value of attribute 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
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.
63 |
# File 'lib/ksef/fa3/data_block.rb', line 63 def = .to_h { |entry| [entry.key, entry.value] } |