Class: Sashite::Pan::Action::Capture

Inherits:
Object
  • Object
show all
Defined in:
lib/sashite/pan/action/capture.rb

Overview

Capture action class

Handles capture actions at destination, with optional transformation.

Format: <source>+<destination> Examples: “d1+f3”, “b7+a8=R”

Constant Summary collapse

TYPE =

Action type

:capture
OPERATOR =

Operator constant

"+"
TRANSFORMATION_SEPARATOR =

Transformation separator

"="
ERROR_INVALID_CAPTURE =

Error messages

"Invalid capture notation: %s"
ERROR_INVALID_SOURCE =
"Invalid source coordinate: %s"
ERROR_INVALID_DESTINATION =
"Invalid destination coordinate: %s"
ERROR_INVALID_TRANSFORMATION =
"Invalid transformation piece: %s"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, destination, transformation: nil) ⇒ Capture

Create a new capture action instance

Examples:

Capture.new("d1", "f3")                      # => #<Capture ...>
Capture.new("b7", "a8", transformation: "R") # => #<Capture ...>

Raises:

  • (ArgumentError)

    if coordinates or transformation are invalid



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/sashite/pan/action/capture.rb', line 116

def initialize(source, destination, transformation: nil)
  raise ::ArgumentError, format(ERROR_INVALID_SOURCE, source) unless ::Sashite::Cell.valid?(source)
  unless ::Sashite::Cell.valid?(destination)
    raise ::ArgumentError, format(ERROR_INVALID_DESTINATION, destination)
  end

  if transformation && !::Sashite::Epin.valid?(transformation)
    raise ::ArgumentError, format(ERROR_INVALID_TRANSFORMATION, transformation)
  end

  @source = source
  @destination = destination
  @transformation = transformation

  freeze
end

Instance Attribute Details

#destinationString (readonly)



35
36
37
# File 'lib/sashite/pan/action/capture.rb', line 35

def destination
  @destination
end

#sourceString (readonly)



32
33
34
# File 'lib/sashite/pan/action/capture.rb', line 32

def source
  @source
end

#transformationString? (readonly)



38
39
40
# File 'lib/sashite/pan/action/capture.rb', line 38

def transformation
  @transformation
end

Class Method Details

.parse(pan_string) ⇒ Capture

Parse a capture notation string into a Capture instance

Examples:

Capture.parse("d1+f3")      # => #<Capture source="d1" destination="f3">
Capture.parse("b7+a8=R")    # => #<Capture source="b7" destination="a8" transformation="R">

Raises:

  • (ArgumentError)

    if the string is not valid capture notation



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sashite/pan/action/capture.rb', line 87

def self.parse(pan_string)
  raise ::ArgumentError, format(ERROR_INVALID_CAPTURE, pan_string) unless valid?(pan_string)

  parts = pan_string.split(OPERATOR, 2)
  source = parts[0]
  dest_and_transform = parts[1]

  if dest_and_transform.include?(TRANSFORMATION_SEPARATOR)
    dest_parts = dest_and_transform.split(TRANSFORMATION_SEPARATOR, 2)
    destination = dest_parts[0]
    transformation = dest_parts[1]
  else
    destination = dest_and_transform
    transformation = nil
  end

  new(source, destination, transformation: transformation)
end

.valid?(pan_string) ⇒ Boolean

Check if a string represents a valid capture action

Examples:

Capture.valid?("d1+f3")       # => true
Capture.valid?("b7+a8=R")     # => true
Capture.valid?("d1-f3")       # => false


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sashite/pan/action/capture.rb', line 49

def self.valid?(pan_string)
  return false unless pan_string.is_a?(::String)
  return false unless pan_string.include?(OPERATOR)

  parts = pan_string.split(OPERATOR, 2)
  return false if parts.size != 2

  source_part = parts[0]
  dest_and_transform = parts[1]

  return false unless ::Sashite::Cell.valid?(source_part)

  # Check if there's a transformation
  if dest_and_transform.include?(TRANSFORMATION_SEPARATOR)
    dest_parts = dest_and_transform.split(TRANSFORMATION_SEPARATOR, 2)
    return false if dest_parts.size != 2

    destination_part = dest_parts[0]
    transformation_part = dest_parts[1]

    return false unless ::Sashite::Cell.valid?(destination_part)
    return false unless ::Sashite::Epin.valid?(transformation_part)
  else
    return false unless ::Sashite::Cell.valid?(dest_and_transform)
  end

  true
end

Instance Method Details

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

Custom equality comparison



233
234
235
236
237
238
239
# File 'lib/sashite/pan/action/capture.rb', line 233

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

  source == other.source &&
    destination == other.destination &&
    transformation == other.transformation
end

#capture?Boolean

Check if this is a capture action



176
177
178
# File 'lib/sashite/pan/action/capture.rb', line 176

def capture?
  true
end

#drop?Boolean

Check if this is a drop action



197
198
199
# File 'lib/sashite/pan/action/capture.rb', line 197

def drop?
  false
end

#drop_action?Boolean

Check if this is a drop action (drop or drop_capture)



225
226
227
# File 'lib/sashite/pan/action/capture.rb', line 225

def drop_action?
  false
end

#drop_capture?Boolean

Check if this is a drop capture action



204
205
206
# File 'lib/sashite/pan/action/capture.rb', line 204

def drop_capture?
  false
end

#hashInteger

Custom hash implementation for use in collections



247
248
249
# File 'lib/sashite/pan/action/capture.rb', line 247

def hash
  [self.class, source, destination, transformation].hash
end

#modify?Boolean

Check if this is a modify action



211
212
213
# File 'lib/sashite/pan/action/capture.rb', line 211

def modify?
  false
end

#move?Boolean

Check if this is a move action



169
170
171
# File 'lib/sashite/pan/action/capture.rb', line 169

def move?
  false
end

#movement?Boolean

Check if this is a movement action



218
219
220
# File 'lib/sashite/pan/action/capture.rb', line 218

def movement?
  true
end

#pass?Boolean

Check if this is a pass action



162
163
164
# File 'lib/sashite/pan/action/capture.rb', line 162

def pass?
  false
end

#piecenil

Get the piece identifier



143
144
145
# File 'lib/sashite/pan/action/capture.rb', line 143

def piece
  nil
end

#special?Boolean

Check if this is a special action



183
184
185
# File 'lib/sashite/pan/action/capture.rb', line 183

def special?
  false
end

#static_capture?Boolean

Check if this is a static capture action



190
191
192
# File 'lib/sashite/pan/action/capture.rb', line 190

def static_capture?
  false
end

#to_sString

Convert the action to its PAN string representation

Examples:

action.to_s  # => "d1+f3" or "b7+a8=R"


153
154
155
156
157
# File 'lib/sashite/pan/action/capture.rb', line 153

def to_s
  result = "#{source}#{OPERATOR}#{destination}"
  result += "#{TRANSFORMATION_SEPARATOR}#{transformation}" if transformation
  result
end

#typeSymbol

Get the action type



136
137
138
# File 'lib/sashite/pan/action/capture.rb', line 136

def type
  TYPE
end