Class: Uniword::Ooxml::Types::OoxmlBoolean

Inherits:
Lutaml::Model::Type::Boolean
  • Object
show all
Defined in:
lib/uniword/ooxml/types/ooxml_boolean.rb

Overview

OOXML Boolean type for attributes (ST_OnOff, ECMA-376) OOXML uses "1"/"0" encoding for boolean attributes; ST_OnOff also accepts the xsd:boolean spellings "true"/"false" and "on"/"off".

Parsing: "1"/"true"/"on" -> true, "0"/"false"/"off"/nil -> false Serialization: true -> "1", false -> "0" Anything else raises Lutaml::Model::Type::InvalidValueError instead of passing through unchanged.

Constant Summary collapse

TRUE_VALUES =

Accepted ST_OnOff spellings for Boolean true

[true, 1, "1", "true", "on"].freeze
FALSE_VALUES =

Accepted ST_OnOff spellings for Boolean false

[false, 0, "0", "false", "off"].freeze
ON_OFF_VALUES =

All accepted ST_OnOff spellings

(TRUE_VALUES + FALSE_VALUES).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cast(value, _options = {}) ⇒ Object

Raises:

  • (Lutaml::Model::Type::InvalidValueError)


26
27
28
29
30
31
32
33
34
# File 'lib/uniword/ooxml/types/ooxml_boolean.rb', line 26

def self.cast(value, _options = {})
  return value if Lutaml::Model::Utils.uninitialized?(value)
  return true if TRUE_VALUES.include?(value)
  return false if FALSE_VALUES.include?(value) || value.nil?

  raise Lutaml::Model::Type::InvalidValueError.new(
    value, ON_OFF_VALUES
  )
end

.serialize(value) ⇒ Object

Raises:

  • (Lutaml::Model::Type::InvalidValueError)


36
37
38
39
40
41
42
43
44
# File 'lib/uniword/ooxml/types/ooxml_boolean.rb', line 36

def self.serialize(value)
  return nil if value.nil?
  return "1" if TRUE_VALUES.include?(value)
  return "0" if FALSE_VALUES.include?(value)

  raise Lutaml::Model::Type::InvalidValueError.new(
    value, ON_OFF_VALUES
  )
end

Instance Method Details

#to_xmlObject

Override instance to_xml for OOXML boolean serialization Parent class returns value.to_s which gives "true"/"false" OOXML requires "1"/"0" for boolean attributes



49
50
51
52
53
54
55
56
57
58
# File 'lib/uniword/ooxml/types/ooxml_boolean.rb', line 49

def to_xml
  case @value
  when true
    "1"
  when false
    "0"
  else
    @value.to_s
  end
end