Class: Sashite::Snn::Name

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Name

Create a new style name instance

Raises:

  • (ArgumentError)

    if the name does not match SNN pattern



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

#valueString (readonly)



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

Examples:

Sashite::Snn::Name.parse("Shogi") # => #<Snn::Name value="Shogi">

Raises:

  • (ArgumentError)

    if the string is invalid



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

Examples:

Sashite::Snn::Name.valid?("Chess")    # => true
Sashite::Snn::Name.valid?("chess")    # => false


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

Raises:

  • (ArgumentError)

    if invalid



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

#hashInteger

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_sString

Returns the string representation of the name



61
62
63
# File 'lib/sashite/snn/name.rb', line 61

def to_s
  value
end