Class: Sashite::Pcn::Game::Meta

Inherits:
Object
  • Object
show all
Defined in:
lib/sashite/pcn/game/meta.rb

Overview

Represents game metadata with standard and custom fields

All fields are optional. An empty Meta object (no metadata) is valid. Standard fields are validated, custom fields are accepted without validation.

Examples:

With standard fields

meta = Meta.new(
  name: "Italian Game",
  event: "World Championship",
  location: "London",
  round: 5,
  started_at: "2025-01-27T14:00:00Z",
  href: "https://example.com/game/123"
)

With custom fields

meta = Meta.new(
  event: "Online Tournament",
  platform: "lichess.org",
  time_control: "5+3",
  rated: true,
  opening_eco: "C50"
)

Empty metadata

meta = Meta.new  # Valid, no metadata

Constant Summary collapse

ERROR_INVALID_NAME =

Error messages

"name must be a string"
ERROR_INVALID_EVENT =
"event must be a string"
ERROR_INVALID_LOCATION =
"location must be a string"
ERROR_INVALID_ROUND =
"round must be a positive integer (>= 1)"
ERROR_INVALID_STARTED_AT =
"started_at must be in ISO 8601 datetime format (e.g., 2025-01-27T14:00:00Z)"
ERROR_INVALID_HREF =
"href must be an absolute URL (http:// or https://)"
STANDARD_FIELDS =

Standard field keys

i[name event location round started_at href].freeze
DATETIME_PATTERN =

Regular expressions for validation ISO 8601 datetime - accepts various formats:

  • Basic: 2025-01-27T14:00:00Z

  • With milliseconds: 2025-01-27T14:00:00.123Z

  • With timezone offset: 2025-01-27T14:00:00+02:00

  • Local time without timezone: 2025-01-27T14:00:00

/\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?\z/
URL_PATTERN =
/\Ahttps?:\/\/.+/

Instance Method Summary collapse

Constructor Details

#initialize(**fields) ⇒ Meta

Create a new Meta instance

Raises:

  • (ArgumentError)

    if standard field values don’t meet validation requirements



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sashite/pcn/game/meta.rb', line 57

def initialize(**fields)
  @data = {}

  # Process and validate each field
  fields.each do |key, value|
    validate_and_store(key, value)
  end

  @data.freeze
  freeze
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Check equality with another Meta object



153
154
155
156
157
# File 'lib/sashite/pcn/game/meta.rb', line 153

def ==(other)
  return false unless other.is_a?(self.class)

  @data == other.to_h
end

#[](key) ⇒ Object?

Get a metadata value by key

Examples:

meta[:event]  # => "World Championship"
meta["started_at"]  # => "2025-01-27T14:00:00Z"


77
78
79
# File 'lib/sashite/pcn/game/meta.rb', line 77

def [](key)
  @data[key.to_sym]
end

#each {|key, value| ... } ⇒ Enumerator

Iterate over each metadata field

Examples:

meta.each { |k, v| puts "#{k}: #{v}" }

Yields:

  • (key, value)

    yields each key-value pair



120
121
122
123
124
# File 'lib/sashite/pcn/game/meta.rb', line 120

def each(&)
  return @data.each unless block_given?

  @data.each(&)
end

#empty?Boolean

Check if no metadata is present

Examples:

meta.empty?  # => true


87
88
89
# File 'lib/sashite/pcn/game/meta.rb', line 87

def empty?
  @data.empty?
end

#hashInteger

Hash code for use in collections



164
165
166
# File 'lib/sashite/pcn/game/meta.rb', line 164

def hash
  @data.hash
end

#inspectString

String representation for debugging



145
146
147
# File 'lib/sashite/pcn/game/meta.rb', line 145

def inspect
  "#<#{self.class.name} #{@data.inspect}>"
end

#key?(key) ⇒ Boolean

Check if a metadata field is present

Examples:

meta.key?(:event)  # => true
meta.key?("round")  # => true


109
110
111
# File 'lib/sashite/pcn/game/meta.rb', line 109

def key?(key)
  @data.key?(key.to_sym)
end

#keysArray<Symbol>

Get all metadata keys

Examples:

meta.keys  # => [:event, :round, :platform]


97
98
99
# File 'lib/sashite/pcn/game/meta.rb', line 97

def keys
  @data.keys
end

#to_hHash

Convert to hash representation

Examples:

meta.to_h
# => {
#   event: "Tournament",
#   round: 5,
#   started_at: "2025-01-27T14:00:00Z",
#   platform: "lichess.org"
# }


138
139
140
# File 'lib/sashite/pcn/game/meta.rb', line 138

def to_h
  @data.dup.freeze
end