Skip to content

sashite/snn.rb

Repository files navigation

snn.rb

Version Yard documentation CI License

SNN (Style Name Notation) implementation for Ruby.

Overview

This library implements the SNN Specification v1.0.0.

Implementation Constraints

Constraint Value Rationale
Max string length 32 Sufficient for realistic style names

These constraints enable bounded memory usage and safe parsing.

Installation

# In your Gemfile
gem "sashite-snn"

Or install manually:

gem install sashite-snn

Usage

Parsing (String → StyleName)

Convert an SNN string into a StyleName object.

require "sashite/snn"

# Standard parsing (raises on error)
snn = Sashite::Snn.parse("Chess")
snn.name  # => "Chess"

# With numeric suffix
snn = Sashite::Snn.parse("Chess960")
snn.name  # => "Chess960"

# Invalid input raises ArgumentError
Sashite::Snn.parse("chess")  # => raises ArgumentError, "invalid format"
Sashite::Snn.parse("")       # => raises ArgumentError, "empty input"

Formatting (StyleName → String)

Convert a StyleName back to an SNN string.

# From StyleName object
snn = Sashite::Snn::StyleName.new("Chess")
snn.to_s  # => "Chess"

# String interpolation
"Playing #{snn}"  # => "Playing Chess"

Validation

# Boolean check
Sashite::Snn.valid?("Chess")     # => true
Sashite::Snn.valid?("Chess960")  # => true
Sashite::Snn.valid?("chess")     # => false (lowercase start)
Sashite::Snn.valid?("")          # => false (empty)

Accessing Data

snn = Sashite::Snn.parse("Chess960")

# Get the name (attribute)
snn.name  # => "Chess960"

API Reference

Types

# StyleName represents a validated SNN style name.
class Sashite::Snn::StyleName
  # Creates a StyleName from a valid name string.
  # Raises ArgumentError if the name is invalid.
  #
  # @param name [String] SNN style name
  # @return [StyleName]
  def initialize(name)

  # Returns the style name.
  #
  # @return [String]
  def name

  # Returns the SNN string representation.
  #
  # @return [String]
  def to_s
end

Constants

Sashite::Snn::StyleName::MAX_STRING_LENGTH  # => 32

Parsing

# Parses an SNN string into a StyleName.
# Raises ArgumentError if the string is not valid.
#
# @param string [String] SNN style name string
# @return [StyleName]
# @raise [ArgumentError] if invalid
def Sashite::Snn.parse(string)

Validation

# Reports whether string is a valid SNN style name.
#
# @param string [String] SNN style name string
# @return [Boolean]
def Sashite::Snn.valid?(string)

Errors

All parsing and validation errors raise ArgumentError with descriptive messages:

Message Cause
"empty input" String length is 0
"input too long" String exceeds 32 characters
"invalid format" Does not match SNN format

Design Principles

  • Bounded values: Maximum string length prevents resource exhaustion
  • Object-oriented: StyleName class enables methods and encapsulation
  • Ruby idioms: valid? predicate, to_s conversion, ArgumentError for invalid input
  • Immutable style names: freeze after construction
  • No dependencies: Pure Ruby standard library only

Related Specifications

License

Available as open source under the Apache License 2.0.

About

SNN support for the Ruby language.

Resources

License

Code of conduct

Stars

Watchers

Forks