Class: Sangi::Cell
- Inherits:
-
Object
- Object
- Sangi::Cell
- Defined in:
- lib/sangi/cell.rb
Instance Attribute Summary collapse
-
#five_rods ⇒ Object
Returns the value of attribute five_rods.
-
#place_index ⇒ Object
readonly
Returns the value of attribute place_index.
-
#unit_rods ⇒ Object
Returns the value of attribute unit_rods.
Class Method Summary collapse
Instance Method Summary collapse
- #add_rod(rod) ⇒ Object
- #clone_deep ⇒ Object
-
#initialize(place_index:, unit_rods: [], five_rods: []) ⇒ Cell
constructor
A new instance of Cell.
- #orientation ⇒ Object
- #remove_rod(kind) ⇒ Object
- #value ⇒ Object
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_rods ⇒ Object
Returns the value of attribute five_rods.
4 5 6 |
# File 'lib/sangi/cell.rb', line 4 def five_rods @five_rods end |
#place_index ⇒ Object (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_rods ⇒ Object
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
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_deep ⇒ Object
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 |
#orientation ⇒ Object
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 |
#value ⇒ Object
32 33 34 |
# File 'lib/sangi/cell.rb', line 32 def value unit_rods.size + five_rods.size * 5 end |