Skip to content
Browse files

Refactor day data coordination into methods

  • Loading branch information...
1 parent b58a801 commit fd6a67124b484fcb81dc750d671ba763945019a8 @ryanbriones committed Feb 14, 2012
View
4 lib/core_ext/enumerable.rb
@@ -11,8 +11,6 @@ def map_to(to = nil)
def do_map(obj, method)
return self unless obj
raise ArgumentError, "argument must respond to #{method}" unless obj.respond_to?(method)
- map do |element|
- obj.send(method, element)
- end
+ map &obj.method(method)
end
end
View
1 lib/time_budget.rb
@@ -1,5 +1,6 @@
module TimeBudget
require "core_ext/enumerable"
+ require "time_budget/models/activity"
require "time_budget/presenters/day"
require "time_budget/presenters/activity"
require "time_budget/application"
View
7 lib/time_budget/application.rb
@@ -5,12 +5,7 @@ class Application < Sinatra::Base
end
get("/") do
- days = (0..7).map_into(Date::ABBR_DAYNAMES).zip((0..7)).map do |day_name, day_number|
- activities = DB[:activities].filter(day: day_number).all.map_to(OpenStruct)
- Presenters::Day.new(day_name, activities)
- end
-
- haml :"budget/show", locals: {days: days}
+ haml :"budget/show", locals: {days: TimeBudget::Presenters::Day.this_week}
end
end
end
View
21 lib/time_budget/models/activity.rb
@@ -0,0 +1,21 @@
+module TimeBudget
+ module Models
+ class Activity
+ attr_accessor :title, :day, :duration
+
+ def initialize(activity_data = {})
+ @title = activity_data[:title]
+ @day = activity_data[:day]
+ @duration = activity_data[:duration]
+ end
+
+ def self.for_day(day_number)
+ table.filter(day: day_number).all.map_to(self)
+ end
+
+ def self.table
+ DB[:activities]
+ end
+ end
+ end
+end
View
4 lib/time_budget/presenters/activity.rb
@@ -26,6 +26,10 @@ def hours
def minutes
"%02d" % Decorators::HoursAndMinutes.new(duration).minutes
end
+
+ def self.for_day(day_number)
+ Models::Activity.for_day(day_number).map_to(self)
+ end
end
end
end
View
22 lib/time_budget/presenters/day.rb
@@ -1,15 +1,21 @@
require "time_budget/decorators/hours_and_minutes"
+require "core_ext/enumerable"
+require "date"
module TimeBudget
module Presenters
class Day
DAY_DURATION = 60*60*24
- attr_reader :name, :activities
+ attr_reader :activities
- def initialize(name, activities = [])
- @name = name
- @activities = activities.map { |a| Activity.new(a) }
+ def initialize(number, activities = [])
+ @number = number
+ @activities = activities
+ end
+
+ def name
+ Date::ABBR_DAYNAMES[@number]
end
def has_time_available?
@@ -35,6 +41,14 @@ def minutes_available
available_duration = DAY_DURATION - activities_duration
"%02d" % Decorators::HoursAndMinutes.new(available_duration).minutes
end
+
+ def self.this_week
+ (0..6).map &method(:for_day_number)
+ end
+
+ def self.for_day_number(day_number)
+ self.new(day_number, Presenters::Activity.for_day(day_number))
+ end
end
end
end
View
14 spec/activity_spec.rb
@@ -30,4 +30,18 @@
TimeBudget::Presenters::Activity.new(stub("activity", duration: (60*60*12)+(60*2))).minutes.should == "02"
end
end
+
+ context "collecting activities for a day" do
+ let(:activities) { [stub(duration: 60*60), stub(duration: 60*60*2)] }
+
+ before(:each) do
+ TimeBudget::Models::Activity.stub(:for_day).and_return(activities)
+ end
+
+ it "converts the activity models into presenters" do
+ activities = TimeBudget::Presenters::Activity.for_day(5)
+ activities.first.hours.should == "01"
+ activities.last.hours.should == "02"
+ end
+ end
end
View
43 spec/day_spec.rb
@@ -1,29 +1,25 @@
-require_relative "../lib/time_budget/presenters/day"
-require_relative "../lib/time_budget/presenters/activity"
+require "time_budget/presenters/day"
+require "time_budget/presenters/activity"
+require "time_budget/models/activity"
+
describe TimeBudget::Presenters::Day do
it "displays the name of the day" do
- day = TimeBudget::Presenters::Day.new("Sunday")
- day.name.should == "Sunday"
+ day = TimeBudget::Presenters::Day.new(0)
+ day.name.should == "Sun"
end
it "exposes a list of activities specified for this day" do
activity = stub("activity")
- day = TimeBudget::Presenters::Day.new("Sunday", [activity])
+ day = TimeBudget::Presenters::Day.new(0, [activity])
day.activities.should_not be_empty
end
it "activities is an empty list if not specified" do
- day = TimeBudget::Presenters::Day.new("Sunday")
+ day = TimeBudget::Presenters::Day.new(0)
day.activities.should == []
end
- it "activities are wrapped in Activitys" do
- activity = stub("activity")
- day = TimeBudget::Presenters::Day.new("Sunday", [activity])
- day.activities.first.should be_instance_of(TimeBudget::Presenters::Activity)
- end
-
it "has time available if the sum of the activities duration is less than the duration of a day" do
activity = stub("activity", duration: 1)
day = TimeBudget::Presenters::Day.new("Sunday", [activity])
@@ -55,4 +51,27 @@
day.minutes_available.should == "07"
end
end
+
+ context "when collecting days for this week" do
+ it "builds 7 Day presenters for the day number" do
+ TimeBudget::Presenters::Day.should_receive(:for_day_number).exactly(7).times
+ TimeBudget::Presenters::Day.this_week
+ end
+ end
+
+ context "when creating a Day from a day number" do
+ let(:activities) { stub }
+
+ before do
+ TimeBudget::Presenters::Activity.stub(:for_day) { activities }
+ end
+
+ it "returns a day with the day name" do
+ TimeBudget::Presenters::Day.for_day_number(0).name.should == "Sun"
+ end
+
+ it "looks up the activities for the day" do
+ TimeBudget::Presenters::Day.for_day_number(0).activities.should == activities
+ end
+ end
end
View
26 spec/models/activity_spec.rb
@@ -0,0 +1,26 @@
+require "time_budget/models/activity"
+
+module TimeBudget
+ module Models
+ describe Activity do
+ context "when looking up activities by day" do
+ it "filters the table by day" do
+ table = stub
+ Activity.stub(:table) { table }
+ table.should_receive(:filter).with(day: 1) { stub(all: []) }
+ Activity.for_day(1)
+ end
+
+ it "converts the results into Activity objects" do
+ table = stub
+ Activity.stub(:table) { table }
+ activity_data = stub
+ table.stub(:filter) { stub(all: [activity_data]) }
+ activity_model = stub
+ Activity.stub(:new).with(activity_data) { activity_model}
+ Activity.for_day(1).should == [activity_model]
+ end
+ end
+ end
+ end
+end

0 comments on commit fd6a671

Please sign in to comment.
Something went wrong with that request. Please try again.