Module: Sashite::Stn
- Defined in:
- lib/sashite/stn.rb,
lib/sashite/stn/error.rb,
lib/sashite/stn/transition.rb
Defined Under Namespace
Classes: Error, Transition
Constant Summary collapse
- EMPTY_TRANSITION =
Canonical, immutable transitions reused across calls.
Transition.new.freeze
- PASS_TRANSITION =
Transition.new(toggle: true).freeze
Class Method Summary collapse
-
.combine(*transitions) ⇒ Transition
Combine (compose) several transitions or Hash payloads left-to-right.
-
.compose(*transitions) ⇒ Object
Friendly alias for Stn.combine.
-
.empty ⇒ Transition
Return the canonical empty transition: no board changes, no hand changes, no toggle.
-
.parse(data) ⇒ Transition
Parse an STN payload (Hash) into a Transition, or return the same Transition if one is passed.
-
.pass ⇒ Transition
Return the canonical pass transition: toggle only, no board/hands changes.
-
.transition(board: {}, hands: {}, toggle: false) ⇒ Transition
Construct a Transition directly from keyword arguments.
-
.valid?(data) ⇒ Boolean
Validate an STN payload (Hash) or a Transition instance.
Class Method Details
.combine(*transitions) ⇒ Transition
Combine (compose) several transitions or Hash payloads left-to-right. Composition semantics follow STN rules:
- board: last value wins per cell
- hands: deltas are summed; entries summing to zero are removed
- toggle: XOR across the sequence
147 148 149 150 |
# File 'lib/sashite/stn.rb', line 147 def combine(*transitions) parsed = transitions.flatten.compact.map { |t| parse(t) } parsed.reduce(EMPTY_TRANSITION) { |acc, t| acc.combine(t) } end |
.compose(*transitions) ⇒ Object
Friendly alias for combine.
155 156 157 |
# File 'lib/sashite/stn.rb', line 155 def compose(*transitions) combine(*transitions) end |
.empty ⇒ Transition
Return the canonical empty transition: no board changes, no hand changes, no toggle. This instance is immutable and safe to reuse.
111 112 113 |
# File 'lib/sashite/stn.rb', line 111 def empty EMPTY_TRANSITION end |
.parse(data) ⇒ Transition
Parse an STN payload (Hash) into a Transition, or return the same Transition if one is passed. Raises on invalid input.
Top-level keys may be symbols or strings and are normalized.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sashite/stn.rb', line 66 def parse(data) case data when Transition data when ::Hash Transition.parse(_normalize_root(data)) else raise Error::Validation, "STN must be provided as a Hash or a Transition, got: #{data.class}" end end |
.pass ⇒ Transition
Return the canonical pass transition: toggle only, no board/hands changes. This instance is immutable and safe to reuse.
123 124 125 |
# File 'lib/sashite/stn.rb', line 123 def pass PASS_TRANSITION end |
.transition(board: {}, hands: {}, toggle: false) ⇒ Transition
Construct a Transition directly from keyword arguments. Inputs are not mutated; keys are normalized to strings.
97 98 99 100 101 |
# File 'lib/sashite/stn.rb', line 97 def transition(board: {}, hands: {}, toggle: false) board_norm = _stringify_map(board) hands_norm = _stringify_map(hands) Transition.new(board: board_norm, hands: hands_norm, toggle: !!toggle) end |
.valid?(data) ⇒ Boolean
Validate an STN payload (Hash) or a Transition instance.
The validation is strict and delegated to Sashite::Stn::Transition.parse. It accepts top-level keys as strings (“board”, “hands”, “toggle”) or symbols (:board, :hands, :toggle). Nested keys are normalized to strings.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/sashite/stn.rb', line 33 def valid?(data) case data when Transition true when ::Hash begin Transition.parse(_normalize_root(data)) true rescue Error false end else false end end |