Module: Lutaml::Model::Type

Defined in:
lib/lutaml/model/type.rb,
lib/lutaml/model/type/date.rb,
lib/lutaml/model/type/hash.rb,
lib/lutaml/model/type/time.rb,
lib/lutaml/model/type/float.rb,
lib/lutaml/model/type/value.rb,
lib/lutaml/model/type/string.rb,
lib/lutaml/model/type/boolean.rb,
lib/lutaml/model/type/decimal.rb,
lib/lutaml/model/type/integer.rb,
lib/lutaml/model/type/date_time.rb,
lib/lutaml/model/type/time_without_date.rb,
lib/lutaml/model/error/type/max_bound_error.rb,
lib/lutaml/model/error/type/min_bound_error.rb,
lib/lutaml/model/error/type/max_length_error.rb,
lib/lutaml/model/error/type/min_length_error.rb,
lib/lutaml/model/error/type/invalid_value_error.rb,
lib/lutaml/model/error/type/pattern_not_matched_error.rb

Defined Under Namespace

Classes: Boolean, Date, DateTime, Decimal, Float, Hash, Integer, InvalidValueError, MaxBoundError, MaxLengthError, MinBoundError, MinLengthError, PatternNotMatchedError, String, Time, TimeWithoutDate, Value

Constant Summary collapse

TYPE_CODES =
{
  string: "Lutaml::Model::Type::String",
  integer: "Lutaml::Model::Type::Integer",
  float: "Lutaml::Model::Type::Float",
  double: "Lutaml::Model::Type::Float",
  decimal: "Lutaml::Model::Type::Decimal",
  date: "Lutaml::Model::Type::Date",
  time: "Lutaml::Model::Type::Time",
  date_time: "Lutaml::Model::Type::DateTime",
  time_without_date: "Lutaml::Model::Type::TimeWithoutDate",
  boolean: "Lutaml::Model::Type::Boolean",
  hash: "Lutaml::Model::Type::Hash",
}.freeze

Class Method Summary collapse

Class Method Details

.cast(value, type) ⇒ Object



46
47
48
49
50
# File 'lib/lutaml/model/type.rb', line 46

def cast(value, type)
  return nil if value.nil?

  lookup(type).cast(value)
end

.lookup(type_name) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
43
44
# File 'lib/lutaml/model/type.rb', line 35

def lookup(type_name)
  return type_name if type_name.is_a?(Class)

  @registry ||= {}
  klass = @registry[type_name.to_sym]

  raise UnknownTypeError.new(type_name) unless klass

  klass
end

.register(type_name, type_class) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/lutaml/model/type.rb', line 25

def register(type_name, type_class)
  unless type_class < Value
    raise TypeError,
          "class '#{type_class}' is not a valid Lutaml::Model::Type::Value"
  end

  @registry ||= {}
  @registry[type_name.to_sym] = type_class
end

.register_builtin_typesObject



19
20
21
22
23
# File 'lib/lutaml/model/type.rb', line 19

def register_builtin_types
  TYPE_CODES.each do |type_name, type_class|
    register(type_name, const_get(type_class))
  end
end

.serialize(value, type) ⇒ Object



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

def serialize(value, type)
  return nil if value.nil?

  lookup(type).serialize(value)
end