Class: Sangi::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/sangi/cell.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(place_index:, unit_rods: [], five_rods: []) ⇒ Cell

Returns a new instance of Cell.



22
23
24
25
26
# File 'lib/sangi/cell.rb', line 22

def initialize(place_index:, unit_rods: [], five_rods: [])
  @place_index = place_index
  @unit_rods = unit_rods
  @five_rods = five_rods
end

Instance Attribute Details

#five_rodsObject

Returns the value of attribute five_rods.



4
5
6
# File 'lib/sangi/cell.rb', line 4

def five_rods
  @five_rods
end

#place_indexObject (readonly)

Returns the value of attribute place_index.



3
4
5
# File 'lib/sangi/cell.rb', line 3

def place_index
  @place_index
end

#unit_rodsObject

Returns the value of attribute unit_rods.



4
5
6
# File 'lib/sangi/cell.rb', line 4

def unit_rods
  @unit_rods
end

Class Method Details

.empty(place_index:) ⇒ Object



18
19
20
# File 'lib/sangi/cell.rb', line 18

def self.empty(place_index:)
  new(place_index: place_index)
end

.from_digit(place_index:, digit:, rod_factory:) ⇒ Object

Raises:



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/sangi/cell.rb', line 6

def self.from_digit(place_index:, digit:, rod_factory:)
  raise ValidationError, "digit must be between 0 and 9" unless (0..9).cover?(digit)

  five_count = digit / 5
  unit_count = digit % 5
  new(
    place_index: place_index,
    unit_rods: unit_count.times.map { rod_factory.build(:unit) },
    five_rods: five_count.times.map { rod_factory.build(:five) }
  )
end

Instance Method Details

#add_rod(rod) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/sangi/cell.rb', line 36

def add_rod(rod)
  case rod.kind
  when :unit then unit_rods << rod
  when :five then five_rods << rod
  else raise ValidationError, "invalid rod kind"
  end
end

#clone_deepObject



52
53
54
55
56
57
58
# File 'lib/sangi/cell.rb', line 52

def clone_deep
  Cell.new(
    place_index: place_index,
    unit_rods: unit_rods.dup,
    five_rods: five_rods.dup
  )
end

#orientationObject



28
29
30
# File 'lib/sangi/cell.rb', line 28

def orientation
  place_index.even? ? :vertical : :horizontal
end

#remove_rod(kind) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/sangi/cell.rb', line 44

def remove_rod(kind)
  case kind
  when :unit then unit_rods.pop
  when :five then five_rods.pop
  else raise ValidationError, "invalid rod kind"
  end
end

#valueObject



32
33
34
# File 'lib/sangi/cell.rb', line 32

def value
  unit_rods.size + five_rods.size * 5
end