Class: Merger

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cards, month_year) ⇒ Merger

Returns a new instance of Merger.


4
5
6
7
8
9
10
# File 'lib/merger.rb', line 4

def initialize(cards, month_year)
  fail ArgumentError, "cards must not be empty" if cards.empty?
  @cards = cards
  @month_nr = month_year.month
  @month_name = Month.name(month_nr)
  @year = month_year.year
end

Instance Attribute Details

#cardsObject (readonly)

Returns the value of attribute cards


2
3
4
# File 'lib/merger.rb', line 2

def cards
  @cards
end

#month_nameObject (readonly)

Returns the value of attribute month_name


2
3
4
# File 'lib/merger.rb', line 2

def month_name
  @month_name
end

#month_nrObject (readonly)

Returns the value of attribute month_nr


2
3
4
# File 'lib/merger.rb', line 2

def month_nr
  @month_nr
end

#yearObject (readonly)

Returns the value of attribute year


2
3
4
# File 'lib/merger.rb', line 2

def year
  @year
end

Instance Method Details

#monthObject


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/merger.rb', line 12

def month
  merged_month = Month.new("#{cards.join(", ")} "\
    "- #{month_name.capitalize} #{year}")

  cards.each do |card|
    next unless (card_config = config.cards[card.to_sym])
    hours_folder = card_config.fetch(:hours_folder) { next }

    brf_file_path = "#{hours_folder}/#{month_name}_#{year}.txt".absolute_path
    month = Month.from(File.read(brf_file_path), month_nr, year)
    new_days = month.days

    merged_month.add(*new_days)
  end

  merged_month
end