Class: R0man::Digit

Inherits:
Object
  • Object
show all
Defined in:
lib/r0man/digit.rb

Defined Under Namespace

Classes: Invalid

Constant Summary collapse

M =
new(name: "M", value: 1000, max_consecutive_allowed: 3, allowed_next_digits: [])
D =
new(name: "D", value: 500, max_consecutive_allowed: 1, allowed_next_digits: [])
C =
new(name: "C", value: 100, max_consecutive_allowed: 3, allowed_next_digits: [M, D])
L =
new(name: "L", value: 50, max_consecutive_allowed: 1, allowed_next_digits: [])
X =
new(name: "X", value: 10, max_consecutive_allowed: 3, allowed_next_digits: [C, L])
V =
new(name: "V", value: 5, max_consecutive_allowed: 1, allowed_next_digits: [])
I =
new(name: "I", value: 1, max_consecutive_allowed: 3, allowed_next_digits: [X, V])
LOOKUP =
{
  "I" => I,
  "V" => V,
  "X" => X,
  "L" => L,
  "C" => C,
  "D" => D,
  "M" => M,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Digit

Returns a new instance of Digit.



5
6
7
8
9
10
# File 'lib/r0man/digit.rb', line 5

def initialize(options)
  @value                   = options[:value]
  @name                    = options[:name]
  @max_consecutive_allowed = options[:max_consecutive_allowed]
  @allowed_next_digits     = options[:allowed_next_digits]
end

Instance Attribute Details

#allowed_next_digitsObject (readonly)

Returns the value of attribute allowed_next_digits.



3
4
5
# File 'lib/r0man/digit.rb', line 3

def allowed_next_digits
  @allowed_next_digits
end

#max_consecutive_allowedObject (readonly)

Returns the value of attribute max_consecutive_allowed.



3
4
5
# File 'lib/r0man/digit.rb', line 3

def max_consecutive_allowed
  @max_consecutive_allowed
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/r0man/digit.rb', line 3

def name
  @name
end

#valueObject (readonly)

Returns the value of attribute value.



3
4
5
# File 'lib/r0man/digit.rb', line 3

def value
  @value
end

Class Method Details

.parse(letter) ⇒ Object



63
64
65
# File 'lib/r0man/digit.rb', line 63

def parse(letter)
  LOOKUP[letter.upcase] || Invalid.new(letter)
end

Instance Method Details

#allows_next_digit?(other_digit) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/r0man/digit.rb', line 32

def allows_next_digit?(other_digit)
  allowed_next_digits.include?(other_digit)
end

#greater_than?(other_digit) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/r0man/digit.rb', line 24

def greater_than?(other_digit)
  value > other_digit.value
end

#inspectObject



20
21
22
# File 'lib/r0man/digit.rb', line 20

def inspect
  to_s
end

#to_sObject



16
17
18
# File 'lib/r0man/digit.rb', line 16

def to_s
  "#{name}(#{value})"
end

#valid?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/r0man/digit.rb', line 12

def valid?
  true
end

#value_compared_to(other_digit) ⇒ Object



28
29
30
# File 'lib/r0man/digit.rb', line 28

def value_compared_to(other_digit)
  other_digit.greater_than?(self) ? -value : value
end