Skip to content

Commit

Permalink
Issue-163: use module instead of inheritance for common context logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Trimble committed May 8, 2016
1 parent dfc9320 commit c4e57db
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
13 changes: 8 additions & 5 deletions lib/holidays/use_case/context/between.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Holidays
module UseCase
module Context
class Between < ContextCommon
class Between
include ContextCommon

def initialize(holidays_by_month_repo, day_of_month_calculator, custom_methods_repo, proc_result_cache_repo)
@holidays_by_month_repo = holidays_by_month_repo
@day_of_month_calculator = day_of_month_calculator
Expand All @@ -13,16 +15,18 @@ def call(start_date, end_date, dates_driver, regions, observed, informal)
validate!(start_date, end_date, dates_driver, regions)

holidays = []

holidays = make_date_array(dates_driver, regions, observed, informal)

holidays = holidays.select{|holiday|holiday[:date].between?(start_date, end_date)}

holidays.sort{|a, b| a[:date] <=> b[:date] }
end

private

attr_reader :holidays_by_month_repo,
:day_of_month_calculator,
:custom_methods_repo,
:proc_result_cache_repo

def validate!(start_date, end_date, dates_driver, regions)
raise ArgumentError unless start_date
raise ArgumentError unless end_date
Expand All @@ -35,7 +39,6 @@ def validate!(start_date, end_date, dates_driver, regions)

raise ArgumentError if regions.nil? || regions.empty?
end

end
end
end
Expand Down
15 changes: 2 additions & 13 deletions lib/holidays/use_case/context/context_common.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
module Holidays
module UseCase
module Context
class ContextCommon
def initialize(holidays_by_month_repo, day_of_month_calculator, custom_methods_repo, proc_result_cache_repo)
@holidays_by_month_repo = holidays_by_month_repo
@day_of_month_calculator = day_of_month_calculator
@custom_methods_repo = custom_methods_repo
@proc_result_cache_repo = proc_result_cache_repo
end

private

attr_reader :holidays_by_month_repo, :day_of_month_calculator, :custom_methods_repo, :proc_result_cache_repo

module ContextCommon
def make_date_array(dates_driver, regions, observed, informal)
holidays = []
dates_driver.each do |year, months|
Expand Down Expand Up @@ -102,7 +91,7 @@ def make_date_array(dates_driver, regions, observed, informal)
end
holidays
end

def call_proc(function_id, *arguments)
function = custom_methods_repo.find(function_id)
raise Holidays::FunctionNotFound.new("Unable to find function with id '#{function_id}'") if function.nil?
Expand Down
22 changes: 17 additions & 5 deletions lib/holidays/use_case/context/next_holiday.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
module Holidays
module UseCase
module Context
class NextHoliday < ContextCommon
class NextHoliday
include ContextCommon

def initialize(holidays_by_month_repo, day_of_month_calculator, custom_methods_repo, proc_result_cache_repo)
@holidays_by_month_repo = holidays_by_month_repo
@day_of_month_calculator = day_of_month_calculator
@custom_methods_repo = custom_methods_repo
@proc_result_cache_repo = proc_result_cache_repo
end

def call(holidays_count, from_date, dates_driver, regions, observed, informal)
validate!(holidays_count, from_date, dates_driver, regions)
holidays = []
ret_holidays = []

ret_holidays = make_date_array(dates_driver, regions, observed, informal)
ret_holidays.each do |holiday|
if holiday[:date] >= from_date
holidays << holiday
holidays_count -= 1
break if holidays_count == 0
end
end
end

holidays.sort{|a, b| a[:date] <=> b[:date] }
end

private

attr_reader :holidays_by_month_repo,
:day_of_month_calculator,
:custom_methods_repo,
:proc_result_cache_repo

def validate!(holidays_count, from_date, dates_driver, regions)
raise ArgumentError unless holidays_count
raise ArgumentError if holidays_count <= 0
Expand All @@ -35,7 +48,6 @@ def validate!(holidays_count, from_date, dates_driver, regions)

raise ArgumentError if regions.nil? || regions.empty?
end

end
end
end
Expand Down

0 comments on commit c4e57db

Please sign in to comment.