Class: Warpera::Pair

Inherits:
Object
  • Object
show all
Defined in:
lib/warpera/pair.rb

Overview

Pairs year and era together for better readability and DRYing

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year:, era:) ⇒ Pair

Sets the attributes

Parameters:

  • year (Integer)

    the year

  • era (:ce, :bce)

    the era

Raises:

  • (ArgumentError)

18
19
20
21
22
23
24
# File 'lib/warpera/pair.rb', line 18

def initialize(year:, era:)
  @year = year
  @era = era

  raise ArgumentError, 'Year is not valid' unless valid_year?
  raise ArgumentError, 'Era is not valid' unless valid_era?
end

Instance Attribute Details

#era:ce, :bce

The era

Returns:

  • (:ce, :bce)

11
12
13
# File 'lib/warpera/pair.rb', line 11

def era
  @era
end

#yearInteger

The year as a whole number

Returns:

  • (Integer)

    the year


7
8
9
# File 'lib/warpera/pair.rb', line 7

def year
  @year
end

Instance Method Details

#to_iInteger

Format the year and era into an integer

Examples:

Current Era

>> Warpera::Pair.new(year: 2019, era: :ce).to_s
=> 2019

Previous Era

>> Warpera::Pair.new(year: 2019, era: :bce).to_s
=> -2019

Returns:

  • (Integer)

    the formatted year


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

def to_i
  if @era == :bce
    return -1 * @year
  end

  @year
end

#to_sString

Format the year and era into a string

Examples:

Current Era

>> Warpera::Pair.new(year: 2019, era: :ce).to_s
=> "2019 CE"

Previous Era

>> Warpera::Pair.new(year: 2019, era: :bce).to_s
=> "2019 BCE"

Returns:

  • (String)

    the formatted year


37
38
39
# File 'lib/warpera/pair.rb', line 37

def to_s
  "#{@year} #{@era.upcase}"
end