Class: Sashite::Snn::Name
- Inherits:
-
Object
- Object
- Sashite::Snn::Name
- Defined in:
- lib/sashite/snn/name.rb
Overview
Represents a style name in SNN (Style Name Notation) format.
SNN provides a canonical naming system for abstract strategy game styles. Each name must start with an uppercase ASCII letter, followed by zero or more lowercase letters or digits.
All instances are immutable.
Constant Summary collapse
- SNN_PATTERN =
SNN validation pattern matching the specification
/\A[A-Z][a-z0-9]*\z/- ERROR_INVALID_NAME =
Error messages
"Invalid SNN string: %s"
Instance Attribute Summary collapse
-
#value ⇒ String
readonly
The canonical style name.
Class Method Summary collapse
-
.parse(string) ⇒ Name
Parse an SNN string into a Name object.
-
.valid?(string) ⇒ Boolean
Check whether the given string is a valid SNN name.
-
.validate_format(str) ⇒ Object
Validate that the string is in proper SNN format.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Equality based on normalized string value.
-
#hash ⇒ Integer
Hash based on class and value.
-
#initialize(name) ⇒ Name
constructor
Create a new style name instance.
-
#to_s ⇒ String
Returns the string representation of the name.
Constructor Details
#initialize(name) ⇒ Name
Create a new style name instance
26 27 28 29 30 31 32 |
# File 'lib/sashite/snn/name.rb', line 26 def initialize(name) string_value = name.to_s self.class.validate_format(string_value) @value = string_value.freeze freeze end |
Instance Attribute Details
#value ⇒ String (readonly)
Returns the canonical style name.
20 21 22 |
# File 'lib/sashite/snn/name.rb', line 20 def value @value end |
Class Method Details
.parse(string) ⇒ Name
Parse an SNN string into a Name object
42 43 44 |
# File 'lib/sashite/snn/name.rb', line 42 def self.parse(string) new(string) end |
.valid?(string) ⇒ Boolean
Check whether the given string is a valid SNN name
54 55 56 |
# File 'lib/sashite/snn/name.rb', line 54 def self.valid?(string) string.is_a?(::String) && string.match?(SNN_PATTERN) end |
.validate_format(str) ⇒ Object
Validate that the string is in proper SNN format
87 88 89 |
# File 'lib/sashite/snn/name.rb', line 87 def self.validate_format(str) raise ::ArgumentError, format(ERROR_INVALID_NAME, str.inspect) unless str.match?(SNN_PATTERN) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Equality based on normalized string value
69 70 71 |
# File 'lib/sashite/snn/name.rb', line 69 def ==(other) other.is_a?(self.class) && value == other.value end |
#hash ⇒ Integer
Hash based on class and value
79 80 81 |
# File 'lib/sashite/snn/name.rb', line 79 def hash [self.class, value].hash end |
#to_s ⇒ String
Returns the string representation of the name
61 62 63 |
# File 'lib/sashite/snn/name.rb', line 61 def to_s value end |