From 2d10c61b2f1d43fa98b46bdd17cfce86da95b7e5 Mon Sep 17 00:00:00 2001 From: Carlos Goce Date: Fri, 23 Jan 2015 00:07:05 +0100 Subject: Calendar can localize months --- Gemfile | 1 + Gemfile.lock | 1 + lib/carlosgoce/calendar.rb | 19 ++++++++++++++++++- spec/calendar_spec.rb | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index c95c1ab..97cc778 100644 --- a/Gemfile +++ b/Gemfile @@ -5,3 +5,4 @@ gem 'prawn-table', '~> 0.2.1' gem 'rake', '~> 10.4.2' gem 'activesupport', '~> 4.2.0' gem 'rspec', '~> 3.1.0' +gem 'rspec-expectations', '~> 3.1.2' diff --git a/Gemfile.lock b/Gemfile.lock index c92cb67..a1263c4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -43,3 +43,4 @@ DEPENDENCIES prawn-table (~> 0.2.1) rake (~> 10.4.2) rspec (~> 3.1.0) + rspec-expectations (~> 3.1.2) diff --git a/lib/carlosgoce/calendar.rb b/lib/carlosgoce/calendar.rb index cb77325..ea5bd6a 100644 --- a/lib/carlosgoce/calendar.rb +++ b/lib/carlosgoce/calendar.rb @@ -1,7 +1,24 @@ +require 'rubygems' +require 'active_support/core_ext/time' +require 'active_support/core_ext/date' + module CarlosGoce class Calendar + attr_reader :year + + def initialize(year=Date.today.year) + @year = year + end + def to_h - {enero: []} + h = {} + (1..12).each do |month| + month_name = I18n.t('date.month_names')[month].downcase + month_days =(1...Time.days_in_month(month, @year)).to_a.each_slice(7).to_a + h[month_name] = month_days + end + + h end end end \ No newline at end of file diff --git a/spec/calendar_spec.rb b/spec/calendar_spec.rb index a543dbe..71252b3 100644 --- a/spec/calendar_spec.rb +++ b/spec/calendar_spec.rb @@ -1,13 +1,27 @@ require 'rspec' +require 'rspec/expectations' require_relative '../lib/carlosgoce/calendar' +I18n.load_path = Dir['config/locales/*.yml'] +I18n.backend.load_translations + describe 'Calendar' do before(:each) do @calendar = CarlosGoce::Calendar.new end + it 'should be initialized with the current year' do + expect(@calendar.year).to eq(Date.today.year) + end + describe 'Return months and days of given year' do + it 'should have all months of the year' do + expect(@calendar.to_h.count).to eq(12) + end + it 'localize month names' do + expect(@calendar.to_h.keys.first.to_s).to eq('january') + I18n.locale = 'es' expect(@calendar.to_h.keys.first.to_s).to eq('enero') end end -- cgit v1.2.3