Class: Lutaml::Model::TypeSubstitution Private

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/type_substitution.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

TypeSubstitution represents a single type substitution rule.

This is an INTERNAL class. Users should use Register and GlobalRegister.

Responsibility: Represent an immutable type substitution

(from_type => to_type)

This is a Value Object:

  • Immutable after creation

  • Equality based on from_type and to_type

  • NO knowledge of where substitutions are stored

  • NO knowledge of substitution chains

Examples:

Basic usage

sub = TypeSubstitution.new(
  from_type: CustomText,
  to_type: Lutaml::Model::Type::String
)
sub.applies_to?(CustomText) #=> true
sub.apply(CustomText) #=> Lutaml::Model::Type::String
sub.apply(OtherClass) #=> nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_type:, to_type:) ⇒ TypeSubstitution

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new type substitution rule

Examples:

sub = TypeSubstitution.new(
  from_type: MyCustomText,
  to_type: Lutaml::Model::Type::String
)

Parameters:

  • from_type (Class)

    The type to be substituted

  • to_type (Class)

    The type to substitute with



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

def initialize(from_type:, to_type:)
  @from_type = from_type
  @to_type = to_type
  freeze
end

Instance Attribute Details

#from_typeClass (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The type to substitute from.

Returns:

  • (Class)

    The type to substitute from



31
32
33
# File 'lib/lutaml/model/type_substitution.rb', line 31

def from_type
  @from_type
end

#to_typeClass (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The type to substitute to.

Returns:

  • (Class)

    The type to substitute to



34
35
36
# File 'lib/lutaml/model/type_substitution.rb', line 34

def to_type
  @to_type
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Value object equality

Two TypeSubstitutions are equal if they have the same from_type and to_type

Parameters:

  • other (Object)

    The object to compare

Returns:

  • (Boolean)

    true if equal



83
84
85
86
87
# File 'lib/lutaml/model/type_substitution.rb', line 83

def ==(other)
  return false unless other.is_a?(TypeSubstitution)

  from_type == other.from_type && to_type == other.to_type
end

#applies_to?(klass) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if this substitution applies to the given class

Examples:

sub.applies_to?(CustomText) #=> true
sub.applies_to?(OtherClass) #=> false

Parameters:

  • klass (Class)

    The class to check

Returns:

  • (Boolean)

    true if this substitution applies



60
61
62
# File 'lib/lutaml/model/type_substitution.rb', line 60

def applies_to?(klass)
  klass == from_type
end

#apply(klass) ⇒ Class?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Apply this substitution to a class

Examples:

sub.apply(CustomText) #=> Lutaml::Model::Type::String
sub.apply(OtherClass) #=> nil

Parameters:

  • klass (Class)

    The class to potentially substitute

Returns:

  • (Class, nil)

    The substituted type if applies, nil otherwise



72
73
74
# File 'lib/lutaml/model/type_substitution.rb', line 72

def apply(klass)
  applies_to?(klass) ? to_type : nil
end

#hashInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Hash code for use as hash key

Returns:

  • (Integer)

    Hash code



94
95
96
# File 'lib/lutaml/model/type_substitution.rb', line 94

def hash
  [from_type, to_type].hash
end

#to_sString Also known as: inspect

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Human-readable representation

Returns:

  • (String)

    String representation



101
102
103
# File 'lib/lutaml/model/type_substitution.rb', line 101

def to_s
  "#<#{self.class.name} #{from_type} => #{to_type}>"
end

#with(from_type: self.from_type, to_type: self.to_type) ⇒ TypeSubstitution

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a copy with potentially different values

Parameters:

  • from_type (Class) (defaults to: self.from_type)

    Optional new from_type (defaults to current)

  • to_type (Class) (defaults to: self.to_type)

    Optional new to_type (defaults to current)

Returns:



112
113
114
# File 'lib/lutaml/model/type_substitution.rb', line 112

def with(from_type: self.from_type, to_type: self.to_type)
  self.class.new(from_type: from_type, to_type: to_type)
end