Class: Sashite::Pnn::Name
- Inherits:
-
Object
- Object
- Sashite::Pnn::Name
- Defined in:
- lib/sashite/pnn/name.rb
Overview
Represents a piece name in PNN (Piece Name Notation) format.
PNN provides a canonical naming system for abstract strategy game pieces. Each name consists of an optional state modifier (+ or -) followed by a case-consistent alphabetic name, encoding piece identity, player assignment, and state in a human-readable format.
All instances are immutable.
Constant Summary collapse
- PNN_PATTERN =
PNN validation pattern matching the specification
/\A([+-]?)([A-Z]+|[a-z]+)\z/- ERROR_INVALID_NAME =
Error messages
"Invalid PNN string: %s"
Instance Attribute Summary collapse
-
#value ⇒ String
readonly
The canonical piece name.
Class Method Summary collapse
-
.parse(string) ⇒ Name
Parse a PNN string into a Name object.
-
.valid?(string) ⇒ Boolean
Check whether the given string is a valid PNN name.
-
.validate_format(str) ⇒ Object
Validate that the string is in proper PNN format.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Equality based on normalized string value.
-
#base_name ⇒ String
Returns the base name without state modifier.
-
#diminished? ⇒ Boolean
Check if the piece has diminished state (-).
-
#enhanced? ⇒ Boolean
Check if the piece has enhanced state (+).
-
#first_player? ⇒ Boolean
Check if the piece belongs to the first player (uppercase).
-
#hash ⇒ Integer
Hash based on class and value.
-
#initialize(name) ⇒ Name
constructor
Create a new piece name instance.
-
#normal? ⇒ Boolean
Check if the piece has normal state (no modifier).
-
#same_base_name?(other) ⇒ Boolean
Check if another piece has the same base name (ignoring case and state).
-
#second_player? ⇒ Boolean
Check if the piece belongs to the second player (lowercase).
-
#to_s ⇒ String
Returns the string representation of the name.
Constructor Details
#initialize(name) ⇒ Name
Create a new piece name instance
27 28 29 30 31 32 33 34 |
# File 'lib/sashite/pnn/name.rb', line 27 def initialize(name) string_value = name.to_s self.class.validate_format(string_value) @value = string_value.freeze @parsed = parse_components(string_value) freeze end |
Instance Attribute Details
#value ⇒ String (readonly)
Returns the canonical piece name.
21 22 23 |
# File 'lib/sashite/pnn/name.rb', line 21 def value @value end |
Class Method Details
.parse(string) ⇒ Name
Parse a PNN string into a Name object
45 46 47 |
# File 'lib/sashite/pnn/name.rb', line 45 def self.parse(string) new(string) end |
.valid?(string) ⇒ Boolean
Check whether the given string is a valid PNN name
59 60 61 |
# File 'lib/sashite/pnn/name.rb', line 59 def self.valid?(string) string.is_a?(::String) && string.match?(PNN_PATTERN) end |
.validate_format(str) ⇒ Object
Validate that the string is in proper PNN format
67 68 69 |
# File 'lib/sashite/pnn/name.rb', line 67 def self.validate_format(str) raise ::ArgumentError, format(ERROR_INVALID_NAME, str.inspect) unless str.match?(PNN_PATTERN) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Equality based on normalized string value
167 168 169 |
# File 'lib/sashite/pnn/name.rb', line 167 def ==(other) other.is_a?(self.class) && value == other.value end |
#base_name ⇒ String
Returns the base name without state modifier
86 87 88 |
# File 'lib/sashite/pnn/name.rb', line 86 def base_name @parsed[:base_name] end |
#diminished? ⇒ Boolean
Check if the piece has diminished state (-)
108 109 110 |
# File 'lib/sashite/pnn/name.rb', line 108 def diminished? @parsed[:state_modifier] == "-" end |
#enhanced? ⇒ Boolean
Check if the piece has enhanced state (+)
97 98 99 |
# File 'lib/sashite/pnn/name.rb', line 97 def enhanced? @parsed[:state_modifier] == "+" end |
#first_player? ⇒ Boolean
Check if the piece belongs to the first player (uppercase)
130 131 132 |
# File 'lib/sashite/pnn/name.rb', line 130 def first_player? @parsed[:base_name] == @parsed[:base_name].upcase end |
#hash ⇒ Integer
Hash based on class and value
177 178 179 |
# File 'lib/sashite/pnn/name.rb', line 177 def hash [self.class, value].hash end |
#normal? ⇒ Boolean
Check if the piece has normal state (no modifier)
119 120 121 |
# File 'lib/sashite/pnn/name.rb', line 119 def normal? @parsed[:state_modifier].empty? end |
#same_base_name?(other) ⇒ Boolean
Check if another piece has the same base name (ignoring case and state)
157 158 159 160 161 |
# File 'lib/sashite/pnn/name.rb', line 157 def same_base_name?(other) return false unless other.is_a?(self.class) base_name.downcase == other.base_name.downcase end |
#second_player? ⇒ Boolean
Check if the piece belongs to the second player (lowercase)
141 142 143 |
# File 'lib/sashite/pnn/name.rb', line 141 def second_player? @parsed[:base_name] == @parsed[:base_name].downcase end |
#to_s ⇒ String
Returns the string representation of the name
74 75 76 |
# File 'lib/sashite/pnn/name.rb', line 74 def to_s value end |