Class: Lutaml::Model::Type::String

Inherits:
Value
  • Object
show all
Defined in:
lib/lutaml/model/type/string.rb

Constant Summary

Constants inherited from Value

Value::EMPTY_OPTIONS, Value::LIST_FACETS, Value::MAX_FACETS, Value::MIN_FACETS, Value::WHITE_SPACE_MODES

Instance Attribute Summary

Attributes inherited from Value

#value

Class Method Summary collapse

Methods inherited from Value

enumeration, exclusive, facets, format_type_serializer_for, fraction_digits, from_format, inclusive, inherited, #initialize, #initialized?, length, pattern, register_format_to_from_methods, register_format_type_serializer, serialize, tighter_facet, #to_s, total_digits, white_space

Methods included from Xml::Type::Configurable

included

Methods included from UninitializedClassGuard

#cast, #serialize

Constructor Details

This class inherits a constructor from Lutaml::Model::Type::Value

Class Method Details

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

Performance-optimized cast with short-circuit for already-correct types



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lutaml/model/type/string.rb', line 8

def self.cast(value, options = {})
  return nil if value.nil?
  return value if Utils.uninitialized?(value)

  # Short-circuit an already-::String value with no options only when
  # no xs:whiteSpace normalization is requested (the common :string
  # case); a non-preserve mode must fall through to transform the text.
  mode = white_space_mode
  if value.is_a?(::String) && options.equal?(EMPTY_OPTIONS) &&
      mode == :preserve
    return value
  end

  value = value.to_s
  value = value.gsub(/[\t\n\r]/, " ") unless mode == :preserve
  value = value.squeeze(" ").strip if mode == :collapse

  unless options.equal?(EMPTY_OPTIONS)
    Model::Services::Type::Validator::String.validate!(value,
                                                       options)
  end
  value
end

.default_xsd_typeString

Default XSD type for String

Returns:



53
54
55
# File 'lib/lutaml/model/type/string.rb', line 53

def self.default_xsd_type
  "xs:string"
end

.white_space_modeObject

Effective xs:whiteSpace mode for this type, defaulting to :preserve. Memoized per class so the hot cast path avoids re-walking the facet chain; class-instance variables are not inherited, so each subclass computes (and freezes) its own effective mode.



36
37
38
# File 'lib/lutaml/model/type/string.rb', line 36

def self.white_space_mode
  @white_space_mode ||= facets[:white_space] || :preserve
end