Skip to content

Commit

Permalink
Add language option to change month name language
Browse files Browse the repository at this point in the history
  • Loading branch information
rathrio committed Aug 27, 2016
1 parent e2a2707 commit 095d7cc
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 29 deletions.
5 changes: 5 additions & 0 deletions lib/config.rb
Expand Up @@ -122,6 +122,11 @@ def configure
"Title that appears in the BRF file.",
""

# @return [Symbol]
option :language,
"What language to display month names in. Use two char language codes.",
:en

# @return [String]
option :default_args,
"Arguments to pass by default. e.g. '-y' to always punch yesterday.",
Expand Down
4 changes: 2 additions & 2 deletions lib/merger.rb
Expand Up @@ -5,7 +5,7 @@ def initialize(cards, month_year)
raise ArgumentError, "cards must not be empty" if cards.empty?
@cards = cards
@month_nr = month_year.month
@month_name = Month.name(month_nr)
@month_name = MonthNames.name(month_nr)
@year = month_year.year
end

Expand All @@ -17,7 +17,7 @@ def month
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
brf_file_path = "#{hours_folder}/#{year}-#{month_nr}.txt".absolute_path
month = Month.from(File.read(brf_file_path), month_nr, year)
new_days = month.days

Expand Down
21 changes: 1 addition & 20 deletions lib/month.rb
Expand Up @@ -3,29 +3,10 @@ class Month

NEWLINE = "\r\n".freeze

NAMES = {
1 => 'januar',
2 => 'februar',
3 => 'maerz',
4 => 'april',
5 => 'mai',
6 => 'juni',
7 => 'juli',
8 => 'august',
9 => 'september',
10 => 'oktober',
11 => 'november',
12 => 'dezember'
}.freeze

attr_accessor :name, :days, :number, :year

alias_method :month, :number

def self.name(month_nr)
NAMES[month_nr]
end

def self.from(brf_str, month_nr, year)
month = BRFParser.new.parse(brf_str)
month.number = month_nr
Expand All @@ -45,7 +26,7 @@ def newline
def name
return @name if year.nil? || number.nil?

name = "#{NAMES[number].capitalize} #{year}"
name = "#{MonthNames.name(number).capitalize} #{year}"
name << " - #{Punch.config.name}" unless Punch.config.name.empty?
name.prepend("#{Punch.config.title} - ") unless Punch.config.title.empty?
name
Expand Down
36 changes: 36 additions & 0 deletions lib/month_names.rb
@@ -0,0 +1,36 @@
module MonthNames
NAMES = {
:en => {
1 => 'january',
2 => 'february',
3 => 'march',
4 => 'april',
5 => 'may',
6 => 'june',
7 => 'juli',
8 => 'august',
9 => 'september',
10 => 'october',
11 => 'november',
12 => 'december'
},
:de => {
1 => 'januar',
2 => 'februar',
3 => 'märz',
4 => 'april',
5 => 'mai',
6 => 'juni',
7 => 'juli',
8 => 'august',
9 => 'september',
10 => 'oktober',
11 => 'november',
12 => 'dezember'
}
}.freeze

def self.name(month_nr, lang = Punch.config.language)
NAMES.fetch(lang.to_sym).fetch(month_nr)
end
end
9 changes: 5 additions & 4 deletions lib/punch_clock.rb
Expand Up @@ -272,14 +272,14 @@ def punch
month_year = MonthYear.new(:month => month_year.month, :year => year)
end

@month_name = Month.name month_year.month
@month_name = MonthNames.name month_year.month

switch "-m", "--merge" do
puts Merger.new(@args, month_year).month
exit
end

@brf_filepath = generate_brf_filepath(month_year, month_name)
@brf_filepath = generate_brf_filepath(month_year)

flag "-b", "--backup" do |path|
system "cp #{brf_filepath} #{path}"
Expand Down Expand Up @@ -443,11 +443,12 @@ def now
@now ||= Date.today
end

def generate_brf_filepath(month_year, month_name)
def generate_brf_filepath(month_year)
filepath = "#{hours_folder}/#{month_year.year}-#{month_year.month}.txt"
return filepath if File.exist?(filepath)

legacy_filepath = "#{hours_folder}/#{month_name}_#{month_year.year}.txt"
german_month_name = MonthNames.name(month_year.month, :de)
legacy_filepath = "#{hours_folder}/#{german_month_name}_#{month_year.year}.txt"
if File.exist?(legacy_filepath)
FileUtils.mv(legacy_filepath, filepath)
return filepath
Expand Down
1 change: 1 addition & 0 deletions punch.rb
Expand Up @@ -13,6 +13,7 @@
require 'attributes'
require 'block'
require 'day'
require 'month_names'
require 'month'
require 'block_parser'
require 'date'
Expand Down
2 changes: 2 additions & 0 deletions test/.punchrc
@@ -1,6 +1,8 @@
Punch.configure do |config|
config.name = 'Spongebob Squarepants'

config.language = :de

config.cards = {
:test => {
:hours_folder => TEST_HOURS_FOLDER,
Expand Down
4 changes: 2 additions & 2 deletions test/month_year_flag_test.rb
Expand Up @@ -4,15 +4,15 @@ class MonthYearFlagTest < PunchTest
def test_month_flag_sets_month
punch '--month 3 12-13'

assert_punched 'Maerz 2015'
assert_punched 'März 2015'
assert_punched '28.01.15 12:00-13:00'
assert_includes clock.brf_filepath, '2015-3.txt'
end

def test_month_flag_takes_optional_year
punch '--month 3.14 13-14'

assert_punched 'Maerz 2014'
assert_punched 'März 2014'
assert_punched '28.01.15 13:00-14:00'
assert_includes clock.brf_filepath, '2014-3.txt'
end
Expand Down
2 changes: 1 addition & 1 deletion test/punch_clock_test.rb
Expand Up @@ -134,7 +134,7 @@ def test_punch_previous_month_with_previous_switch
def test_punch_next_month_with_next_switch
punch "-n 2-3"

assert_punched "Maerz 2015"
assert_punched "März 2015"
end

def test_option_order_shouldnt_matter
Expand Down

0 comments on commit 095d7cc

Please sign in to comment.