Class: Sashite::Qpi::Identifier
- Inherits:
-
Object
- Object
- Sashite::Qpi::Identifier
- Defined in:
- lib/sashite/qpi/identifier.rb
Overview
Represents a parsed QPI (Qualified Piece Identifier) identifier.
A QPI identifier encodes complete Piece Identity by combining:
-
A SIN (Style Identifier Notation) component for Piece Style
-
A PIN (Piece Identifier Notation) component for Piece Name, Side, State, and Terminal Status
Additionally, QPI defines a Native/Derived relationship based on case comparison between the SIN and PIN letters.
Instances are immutable (frozen after creation).
Instance Attribute Summary collapse
-
#pin ⇒ Sashite::Pin::Identifier
readonly
The PIN component.
-
#sin ⇒ Sashite::Sin::Identifier
readonly
The SIN component.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Checks equality with another Identifier.
-
#derive ⇒ Identifier
Returns a new Identifier with PIN case opposite to SIN case.
-
#derived? ⇒ Boolean
Checks if the identifier is derived.
-
#flip ⇒ Identifier
Returns a new Identifier with PIN side flipped.
-
#hash ⇒ Integer
Returns a hash code for the Identifier.
-
#initialize(sin, pin) ⇒ Identifier
constructor
Creates a new Identifier instance.
-
#inspect ⇒ String
Returns an inspect string for the Identifier.
-
#native ⇒ Identifier
Returns a new Identifier with PIN case aligned to SIN case.
-
#native? ⇒ Boolean
Checks if the identifier is native.
-
#to_s ⇒ String
Returns the QPI string representation.
-
#with_pin(new_pin) ⇒ Identifier
Returns a new Identifier with a different PIN component.
-
#with_sin(new_sin) ⇒ Identifier
Returns a new Identifier with a different SIN component.
Constructor Details
#initialize(sin, pin) ⇒ Identifier
Creates a new Identifier instance.
57 58 59 60 61 62 63 64 65 |
# File 'lib/sashite/qpi/identifier.rb', line 57 def initialize(sin, pin) validate_sin!(sin) validate_pin!(pin) @sin = sin @pin = pin freeze end |
Instance Attribute Details
#pin ⇒ Sashite::Pin::Identifier (readonly)
Returns The PIN component.
44 45 46 |
# File 'lib/sashite/qpi/identifier.rb', line 44 def pin @pin end |
#sin ⇒ Sashite::Sin::Identifier (readonly)
Returns The SIN component.
41 42 43 |
# File 'lib/sashite/qpi/identifier.rb', line 41 def sin @sin end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Checks equality with another Identifier.
210 211 212 213 214 |
# File 'lib/sashite/qpi/identifier.rb', line 210 def ==(other) return false unless self.class === other sin == other.sin && pin == other.pin end |
#derive ⇒ Identifier
Returns a new Identifier with PIN case opposite to SIN case.
If already derived, returns self.
147 148 149 150 151 152 |
# File 'lib/sashite/qpi/identifier.rb', line 147 def derive return self if derived? opposite_side = sin.side.equal?(:first) ? :second : :first self.class.new(sin, pin.with_side(opposite_side)) end |
#derived? ⇒ Boolean
Checks if the identifier is derived.
A QPI is derived when sin.side differs from pin.side (different case).
109 110 111 |
# File 'lib/sashite/qpi/identifier.rb', line 109 def derived? !native? end |
#flip ⇒ Identifier
Returns a new Identifier with PIN side flipped.
The SIN component remains unchanged. This reflects the QPI specification constraint that SIN (Player Style + Player Side) is stable within a Match, while PIN side (Piece Side) may change via Mutation.
198 199 200 |
# File 'lib/sashite/qpi/identifier.rb', line 198 def flip self.class.new(sin, pin.flip) end |
#hash ⇒ Integer
Returns a hash code for the Identifier.
221 222 223 |
# File 'lib/sashite/qpi/identifier.rb', line 221 def hash [sin, pin].hash end |
#inspect ⇒ String
Returns an inspect string for the Identifier.
228 229 230 |
# File 'lib/sashite/qpi/identifier.rb', line 228 def inspect "#<#{self.class} #{self}>" end |
#native ⇒ Identifier
Returns a new Identifier with PIN case aligned to SIN case.
If already native, returns self.
129 130 131 132 133 |
# File 'lib/sashite/qpi/identifier.rb', line 129 def native return self if native? self.class.new(sin, pin.with_side(sin.side)) end |
#native? ⇒ Boolean
Checks if the identifier is native.
A QPI is native when sin.side equals pin.side (same case).
95 96 97 |
# File 'lib/sashite/qpi/identifier.rb', line 95 def native? sin.side.equal?(pin.side) end |
#to_s ⇒ String
Returns the QPI string representation.
77 78 79 |
# File 'lib/sashite/qpi/identifier.rb', line 77 def to_s "#{sin}#{Constants::SEPARATOR}#{pin}" end |
#with_pin(new_pin) ⇒ Identifier
Returns a new Identifier with a different PIN component.
180 181 182 |
# File 'lib/sashite/qpi/identifier.rb', line 180 def with_pin(new_pin) self.class.new(sin, new_pin) end |
#with_sin(new_sin) ⇒ Identifier
Returns a new Identifier with a different SIN component.
167 168 169 |
# File 'lib/sashite/qpi/identifier.rb', line 167 def with_sin(new_sin) self.class.new(new_sin, pin) end |