From 70f634dc12cbc6e9fde6c0bc8854da0ae704b906 Mon Sep 17 00:00:00 2001 From: Carlos Goce Date: Sat, 24 Jan 2015 22:36:48 +0100 Subject: Improve data structure --- lib/carlosgoce/calendar.rb | 39 ++++++++++++++++++++------------------- lib/carlosgoce/layout/layouts.rb | 3 +-- spec/calendar_spec.rb | 18 +++++++++--------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/carlosgoce/calendar.rb b/lib/carlosgoce/calendar.rb index 6a5d8f6..1379e48 100644 --- a/lib/carlosgoce/calendar.rb +++ b/lib/carlosgoce/calendar.rb @@ -12,29 +12,30 @@ module CarlosGoce def to_h Hash.new.tap {|h| - (1..12).each do |month| - h[month] = Hash.new.tap {|m| - m[:days] = (1..Time.days_in_month(month, @year)).to_a - m[:name] = I18n.t('date.month_names')[month].downcase + h[:months] = Hash.new.tap do |h| + (1..12).each do |month| + h[month] = Hash.new.tap {|m| + m[:days] = (1..Time.days_in_month(month, @year)).to_a + m[:name] = I18n.t('date.month_names')[month].downcase - # todo: Need improvement. Not too performant... - # todo: Maby it should be moved to it's own class too to keep things simple - m[:days_names] = Array.new.tap {|a| - m[:days].each do |d| - t = Time.new @year, month, d - a << I18n.t('date.day_names')[t.wday].downcase - end + # todo: Need improvement. Not too performant... + # todo: Maby it should be moved to it's own class too to keep things simple + m[:days_names] = Array.new.tap {|a| + m[:days].each do |d| + t = Time.new @year, month, d + a << I18n.t('date.day_names')[t.wday].downcase + end + } } - } - end - h[:formatted_days] = Array.new.tap {|a| - # todo Why I need to use 1? - days_before_week_start = Date.new(@year, 1, h[1][:days].first).wday - empty_days = [''] * (days_before_week_start - 1) + h[month][:formatted_days] = Array.new.tap {|a| + days_before_week_start = Date.new(@year, 1, h[1][:days].first).wday + empty_days = [''] * (days_before_week_start - 1) - a << (empty_days + h[1][:days]) - } + a << (empty_days + h[1][:days]) + } + end + end } end diff --git a/lib/carlosgoce/layout/layouts.rb b/lib/carlosgoce/layout/layouts.rb index 2382573..65da961 100644 --- a/lib/carlosgoce/layout/layouts.rb +++ b/lib/carlosgoce/layout/layouts.rb @@ -7,12 +7,11 @@ module CarlosGoce class Simple def create (file, data, year) tables = [] - Prawn::Document.generate(file) do font_size = 16 data.each do |k, month| - days = data[k][:days] + days = data[k][:formatted_days] cells = (days).to_a.each_slice(7).to_a cells.unshift [data[k][:name]] diff --git a/spec/calendar_spec.rb b/spec/calendar_spec.rb index 8d8e346..2dc7dc3 100644 --- a/spec/calendar_spec.rb +++ b/spec/calendar_spec.rb @@ -18,34 +18,34 @@ describe 'Calendar' do describe 'Return a data structure with all needed data to generate a pdf' do it 'should have all months of the year' do - expect(@calendar.to_h.count).to eq(12) + expect(@calendar.to_h[:months].count).to eq(12) end it 'return month names localized' do - expect(@calendar.to_h[1][:name]).to eq 'january' + expect(@calendar.to_h[:months][1][:name]).to eq 'january' I18n.locale = 'es' - expect(@calendar.to_h[1][:name]).to eq 'enero' + expect(@calendar.to_h[:months][1][:name]).to eq 'enero' end it 'return the numbers of available days for each month' do - expect(@calendar.to_h[1][:days]).to eq (1..31).to_a + expect(@calendar.to_h[:months][1][:days]).to eq (1..31).to_a end it 'return the days as names' do - expect(@calendar.to_h[1][:days_names].first).to eq 'thursday' - expect(@calendar.to_h[1][:days_names].count).to eq 31 + expect(@calendar.to_h[:months][1][:days_names].first).to eq 'thursday' + expect(@calendar.to_h[:months][1][:days_names].count).to eq 31 end it 'return the day names localized' do - expect(@calendar.to_h[1][:days_names].first).to eq 'thursday' + expect(@calendar.to_h[:months][1][:days_names].first).to eq 'thursday' I18n.locale = 'es' - expect(@calendar.to_h[1][:days_names].first).to eq 'jueves' + expect(@calendar.to_h[:months][1][:days_names].first).to eq 'jueves' end # todo Improve spec description it 'return the days available formated as array leaving blank days for the starting week\'s day' do days_for_january_2015 = ['', '', ''] + (1..31).to_a - expect(@calendar.to_h[:formatted_days].first).to eq days_for_january_2015 + expect(@calendar.to_h[:months][1][:formatted_days].first).to eq days_for_january_2015 end end -- cgit v1.2.3