Module: SleepingKingStudios::Tools::Toolbox::HeritableData::ClassMethods

Defined in:
lib/sleeping_king_studios/tools/toolbox/heritable_data.rb

Overview

Methods prepended onto the singleton class when including HeritableData.

Instance Method Summary collapse

Instance Method Details

#<(other) ⇒ true, ...

Compares ‘self` and `other`.

Parameters:

  • other (Object)

    the object to compare.

Returns:

  • (true)

    if self is a subclass of other, or if self inherits Data members and methods from other.

  • (false)

    if self and other are the same class, if other is a subclass of self, or if other inherits Data members and methods from self.

  • (nil)

    if none of the above is true.



20
21
22
# File 'lib/sleeping_king_studios/tools/toolbox/heritable_data.rb', line 20

def <(other)
  (self <=> other)&.then { |cmp| cmp == -1 }
end

#<=(other) ⇒ true, ...

Compares ‘self` and `other`.

Parameters:

  • other (Object)

    the object to compare.

Returns:

  • (true)

    if self and other are the same class, if self is a subclass of other, or if self inherits Data members and methods from other.

  • (false)

    if other is a subclass of self, or if other inherits Data members and methods from self.

  • (nil)

    if none of the above is true.



34
35
36
# File 'lib/sleeping_king_studios/tools/toolbox/heritable_data.rb', line 34

def <=(other)
  (self <=> other)&.then { |cmp| cmp < 1 }
end

#<=>(other) ⇒ -1, ...

Compares ‘self` and `other`.

Parameters:

  • other (Object)

    the object to compare.

Returns:

  • (-1)

    if self is a subclass of other, or if self inherits Data members and methods from other.

  • (0)

    if self and other are the same class.

  • (1)

    if self is a parent class of other, or if other inherits Data members and methods from self.

  • (nil)

    if none of the above is true.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sleeping_king_studios/tools/toolbox/heritable_data.rb', line 48

def <=>(other)
  return super unless other.is_a?(Class)

  return -1 if other.equal?(Data)
  return 0  if other.equal?(self)

  return nil unless other.const_defined?(:HeritableMethods)

  return -1 if include?(other::HeritableMethods)
  return 1 if other.include?(self::HeritableMethods)

  nil
end

#===(object) ⇒ true

Returns if the object is an instance of the data class, or if the object is an instance of a Data class that inherits members and methods from self.

Returns:

  • (true)

    if the object is an instance of the data class, or if the object is an instance of a Data class that inherits members and methods from self.



65
66
67
68
69
# File 'lib/sleeping_king_studios/tools/toolbox/heritable_data.rb', line 65

def ===(object)
  return true if super

  (self > object.class) || false
end

#>(other) ⇒ true, ...

Compares ‘self` and `other`.

Parameters:

  • other (Object)

    the object to compare.

Returns:

  • (true)

    if other is a subclass of other, or if other inherits Data members and methods from self.

  • (false)

    if self and other are the same class, if self is a subclass of other, or if self inherits Data members and methods from other.

  • (nil)

    if none of the above is true.



81
82
83
# File 'lib/sleeping_king_studios/tools/toolbox/heritable_data.rb', line 81

def >(other)
  (self <=> other)&.then { |cmp| cmp == 1 }
end

#>=(other) ⇒ true, ...

Compares ‘self` and `other`.

Parameters:

  • other (Object)

    the object to compare.

Returns:

  • (true)

    if self and other are the same class, if other is a subclass of other, or if other inherits Data members and methods from self.

  • (false)

    if self is a subclass of other, or if self inherits Data members and methods from other.

  • (nil)

    if none of the above is true.



95
96
97
# File 'lib/sleeping_king_studios/tools/toolbox/heritable_data.rb', line 95

def >=(other)
  (self <=> other)&.then { |cmp| cmp > -1 }
end

#define(*symbols) { ... } ⇒ Object

Defines a new Data class including the members/methods of this class.

The defined data class will include HeritableData, and will be able to define it’s own Data types using this method.

Parameters:

  • symbols (Array<Symbol>)

    additional Data members to define. Any members listed here will be appended to the members defined on the parent Data class.

Yields:

  • additional methods to define on the new Data class.



109
110
111
# File 'lib/sleeping_king_studios/tools/toolbox/heritable_data.rb', line 109

def define(*symbols, &)
  HeritableData.define(*symbols, parent_class: self, &)
end