Sha256: d9b83b574b8a664c4d6500a479bbad2a52a2704687adbe1416ec1b718ea59a48

Contents?: true

Size: 1009 Bytes

Versions: 4

Compression:

Stored size: 1009 Bytes

Contents

module Lutaml
  module Model
    module Type
      class Decimal < Value
        def self.cast(value)
          return nil if value.nil?

          check_dependencies!(value)
          case value
          when BigDecimal
            # If already a BigDecimal, return as-is
            value
          else
            # Convert to string first to handle various input types
            BigDecimal(value.to_s)
          end
        rescue ArgumentError
          nil
        end

        # # xs:decimal format
        def self.serialize(value)
          return nil if value.nil?

          check_dependencies!(value)
          value = cast(value)
          value.to_s("F") # Use fixed-point notation to match test expectations
        end

        def self.from_xml(value)
          cast(value.text)
        end

        def self.check_dependencies!(value)
          unless defined?(BigDecimal)
            raise TypeNotEnabledError.new("Decimal", value)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lutaml-model-0.3.28 lib/lutaml/model/type/decimal.rb
lutaml-model-0.3.27 lib/lutaml/model/type/decimal.rb
lutaml-model-0.3.26 lib/lutaml/model/type/decimal.rb
lutaml-model-0.3.25 lib/lutaml/model/type/decimal.rb