-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
573 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,5 @@ todo.txt | |
/spec/.tmp | ||
|
||
migration_version.yml | ||
|
||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../models/entry_group' | ||
require_relative '../../services/entry_group/importer_service' | ||
require_relative '../../support/ask' | ||
require_relative '../base_presenter_ex' | ||
require_relative 'import_file' | ||
require_relative 'messages' | ||
require_relative 'service_callable' | ||
|
||
module Dsu | ||
module Presenters | ||
module Import | ||
class AllPresenter < BasePresenterEx | ||
include ImportFile | ||
include Messages | ||
include ServiceCallable | ||
include Support::Ask | ||
|
||
def initialize(import_file_path:, options: {}) | ||
super(options: options) | ||
|
||
@import_file_path = import_file_path | ||
end | ||
|
||
def render(response:) | ||
return display_cancelled_message unless response | ||
|
||
importer_service_call.tap do |import_results| | ||
if import_results.values.all?(&:empty?) | ||
display_import_success_message | ||
else | ||
display_import_error_message import_results | ||
end | ||
end | ||
end | ||
|
||
def display_import_prompt | ||
yes?(prompt_with_options(prompt: import_prompt, options: import_prompt_options), options: options) | ||
end | ||
|
||
private | ||
|
||
attr_reader :import_file_path, :options | ||
|
||
def import_entry_groups | ||
@import_entry_groups ||= CSV.foreach(import_file_path, | ||
headers: true).with_object({}) do |entry_group_entry, entry_groups_hash| | ||
next unless entry_group_entry['version'].to_i == Dsu::Migration::VERSION | ||
|
||
Date.parse(entry_group_entry['entry_group']).to_s.tap do |time| | ||
entry_groups_hash[time] = [] unless entry_groups_hash.key?(time) | ||
entry_groups_hash[time] << entry_group_entry['entry_group_entry'] | ||
end | ||
end | ||
end | ||
|
||
def import_prompt | ||
I18n.t('subcommands.import.prompts.import_all_confirm', count: import_entry_groups.count) | ||
end | ||
|
||
def import_prompt_options | ||
I18n.t('subcommands.import.prompts.options') | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../models/entry_group' | ||
require_relative '../../services/entry_group/importer_service' | ||
require_relative '../../support/ask' | ||
require_relative '../base_presenter_ex' | ||
require_relative 'import_file' | ||
require_relative 'messages' | ||
require_relative 'service_callable' | ||
|
||
module Dsu | ||
module Presenters | ||
module Import | ||
class DatesPresenter < BasePresenterEx | ||
include ImportFile | ||
include Messages | ||
include ServiceCallable | ||
include Support::Ask | ||
|
||
def initialize(from:, to:, import_file_path:, options: {}) | ||
super(options: options) | ||
|
||
@from = from.beginning_of_day | ||
@to = to.end_of_day | ||
@import_file_path = import_file_path | ||
end | ||
|
||
def render(response:) | ||
return display_cancelled_message unless response | ||
|
||
importer_service_call.tap do |import_results| | ||
if import_results.values.all?(&:empty?) | ||
display_import_success_message | ||
else | ||
display_import_error_message import_results | ||
end | ||
end | ||
end | ||
|
||
def display_import_prompt | ||
yes?(prompt_with_options(prompt: import_prompt, options: import_prompt_options), options: options) | ||
end | ||
|
||
private | ||
|
||
attr_reader :from, :to, :import_file_path, :options | ||
|
||
def import_entry_groups | ||
@import_entry_groups ||= CSV.foreach(import_file_path, | ||
headers: true).with_object({}) do |entry_group_entry, entry_groups_hash| | ||
next unless entry_group_entry['version'].to_i == Dsu::Migration::VERSION | ||
|
||
entry_group_time = middle_of_day_for(entry_group_entry['entry_group']) | ||
next unless entry_group_time.to_date.between?(from.to_date, to.to_date) | ||
|
||
entry_group_time.to_date.to_s.tap do |time| | ||
entry_groups_hash[time] = [] unless entry_groups_hash.key?(time) | ||
entry_groups_hash[time] << entry_group_entry['entry_group_entry'] | ||
end | ||
end | ||
end | ||
|
||
def import_prompt | ||
I18n.t('subcommands.import.prompts.import_dates_confirm', | ||
from: from.to_date, to: to.to_date, count: import_entry_groups.keys.count) | ||
end | ||
|
||
def import_prompt_options | ||
I18n.t('subcommands.import.prompts.options') | ||
end | ||
|
||
def middle_of_day_for(date_string) | ||
Time.parse(date_string).in_time_zone.middle_of_day | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module Dsu | ||
module Presenters | ||
module Import | ||
module ImportFile | ||
def import_file_path_exist? | ||
File.exist? import_file_path | ||
end | ||
|
||
def nothing_to_import? | ||
return true unless import_file_path_exist? | ||
|
||
import_entry_groups.empty? | ||
end | ||
|
||
def import_entry_groups | ||
# Should return a Hash of entry group entries | ||
# Example: { '2023-12-32' => ['Entry description 1', 'Entry description 2', ...] } | ||
raise NotImplementedError | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
module Dsu | ||
module Presenters | ||
module Import | ||
module Messages | ||
def display_import_prompt | ||
raise NotImplementedError | ||
end | ||
|
||
def display_import_file_not_exist_message | ||
puts apply_theme(I18n.t('subcommands.import.messages.file_not_exist', | ||
file_path: import_file_path), theme_color: color_theme.info) | ||
end | ||
|
||
def display_nothing_to_import_message | ||
puts apply_theme(I18n.t('subcommands.import.messages.nothing_to_import'), theme_color: color_theme.info) | ||
end | ||
|
||
private | ||
|
||
def display_cancelled_message | ||
puts apply_theme(I18n.t('subcommands.import.messages.cancelled'), theme_color: color_theme.info) | ||
end | ||
|
||
def display_import_success_message | ||
puts apply_theme(I18n.t('subcommands.import.messages.import_success'), | ||
theme_color: color_theme.success) | ||
end | ||
|
||
def display_import_error_message(import_results) | ||
import_results.each_pair do |entry_group_date, errors| | ||
if errors.empty? | ||
puts apply_theme(I18n.t('subcommands.import.messages.import_success', | ||
date: entry_group_date), theme_color: color_theme.success) | ||
else | ||
errors.each do |error| | ||
puts apply_theme(I18n.t('subcommands.import.messages.import_error', | ||
date: entry_group_date, error: error), theme_color: color_theme.error) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../services/entry_group/importer_service' | ||
|
||
module Dsu | ||
module Presenters | ||
module Import | ||
module ServiceCallable | ||
private | ||
|
||
def importer_service_call | ||
@importer_service_call ||= begin | ||
importer_service = Services::EntryGroup::ImporterService.new(import_entry_groups: import_entry_groups, | ||
options: options) | ||
importer_service.call | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.