Skip to content

Commit

Permalink
dsu use subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
gangelo committed Jan 27, 2024
1 parent e6688df commit 8457a0d
Show file tree
Hide file tree
Showing 14 changed files with 605 additions and 223 deletions.
37 changes: 12 additions & 25 deletions lib/dsu/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ def all
end
end

# def any?
# project_metadata.any?
# end

def can_delete?(project_name:)
exist?(project_name: project_name) &&
# Cannot delete the last project.
Expand Down Expand Up @@ -217,20 +213,20 @@ def find(project_name:)
raise I18n.t('models.project.errors.does_not_exist', project_name: project_name)
end

project_file_path = project_file_for(project_name: project_name)
project_file = project_file_for(project_name: project_name)

unless project_file_exist?(project_name: project_name)
raise I18n.t('models.project.errors.project_file_not_exist', project_file: project_file_path)
raise I18n.t('models.project.errors.project_file_not_exist', project_file: project_file)
end

project_hash = Crud::JsonFile.read!(file_path: project_file_path)
project_hash = Crud::JsonFile.read!(file_path: project_file)
Services::Project::HydratorService.new(project_hash: project_hash).call
end

# project_number is 1 based.
def find_by_number(project_number:)
project = project_metadata.find do |metadata|
metadata[:project_number] == project_number
metadata[:project_number] == project_number.to_i
end
return unless project

Expand All @@ -239,26 +235,17 @@ def find_by_number(project_number:)

# def find_or_create(project_name:)
# find_or_initialize(project_name: project_name).tap do |project|
# project.write! unless project.persisted?
# project.save! unless project.persisted?
# end
# end

# def find_or_initialize(project_name:)
# project_path = project_folder_for(project_name: project_name)
# # Dif.exist?(project_path) do |project_hash|
# # Services::Project::HydratorService.new(project_hash: project_hash).call
# # end || new(project_name: project_name)
# if Dir.exist?(project_path)
# Crud::JsonFile.read!(file_path: project_path).fetch(:project).tap do |project_name|
# # description = "#{project_name.capitalize} project}"
# # unless project_exist?(project_name: project_name)
# # create!(project_name: project_name, description: description)
# # end
# end
# else
# # TODO: Create
# end
# end
def find_or_initialize(project_name:)
return Models::Project.new(project_name: project_name) unless project_file_exist?(project_name: project_name)

project_file = project_file_for(project_name: project_name)
project_hash = Crud::JsonFile.read!(file_path: project_file)
Services::Project::HydratorService.new(project_hash: project_hash).call
end

# def update(project_name:, description:, version:, options:)
# # TODO: Update the project
Expand Down
14 changes: 9 additions & 5 deletions lib/dsu/presenters/project/delete_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
module Dsu
module Presenters
module Project
class UsePresenter < BasePresenterEx
class DeletePresenter < BasePresenterEx
attr_writer :project_name_or_number

def initialize(project_name_or_number:, options: {})
Expand All @@ -21,6 +21,10 @@ def respond(response:)
project.delete! if project&.present?
end

def project_name
project.project_name
end

def project_name_or_number
return project_name if delete_by_project_name?
return project_number if delete_by_project_number?
Expand Down Expand Up @@ -76,11 +80,11 @@ def project_errors?
project&.invalid?
end

def project_name
return unless delete_by_project_name?
# def project_name
# return unless delete_by_project_name?

@project_name_or_number
end
# @project_name_or_number
# end

def project_number
return -1 unless delete_by_project_number?
Expand Down
49 changes: 49 additions & 0 deletions lib/dsu/presenters/project/use_by_number_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

require_relative '../../models/project'
require_relative '../base_presenter_ex'

module Dsu
module Presenters
module Project
class UseByNumberPresenter < BasePresenterEx
attr_reader :project_number

delegate :project_name, to: :project, allow_nil: true
delegate :description, to: :project, prefix: true, allow_nil: true

def initialize(project_number:, options: {})
super(options: options)

raise ArgumentError, 'project_number is blank' if project_number.blank?

self.project_number = project_number
end

def respond(response:)
return false unless response

project.use! if project&.present?
end

def project_does_not_exist?
!project&.exist?
end

def project_errors
return false unless project&.persisted?

project.errors.full_messages
end

private

attr_writer :project_number

def project
@project ||= Models::Project.find_by_number(project_number: project_number)
end
end
end
end
end
67 changes: 11 additions & 56 deletions lib/dsu/presenters/project/use_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ module Dsu
module Presenters
module Project
class UsePresenter < BasePresenterEx
attr_writer :project_name_or_number
attr_reader :project_name

def initialize(project_name_or_number:, options: {})
delegate :description, to: :project, prefix: true, allow_nil: true

def initialize(project_name:, options: {})
super(options: options)

@project_name_or_number = project_name_or_number
raise ArgumentError, 'project_name is blank' if project_name.blank?

self.project_name = project_name
end

def respond(response:)
Expand All @@ -21,71 +25,22 @@ def respond(response:)
project.use! if project&.present?
end

def project_name_or_number
return project_name if use_by_project_name?
return project_number if use_by_project_number?

Models::Project.default_project_name
end

def project_description
return unless project&.present?

project.description
end

def project_does_not_exist?
!project&.exist?
!project.exist?
end

def project_errors
return [] unless project_errors?
return false unless project.persisted?

project.errors.full_messages
end

def use_by_project_name?
!use_by_project_number? && !use_by_project_default?
end

def use_by_project_number?
/\A\d+\z/.match?(@project_name_or_number.to_s)
end

def use_by_project_default?
@project_name_or_number.blank?
end

private

attr_reader :options
attr_writer :project_name

def project
return @project if defined?(@project)

@project = if use_by_project_name? && Dsu::Models::Project.project_initialized?(project_name: project_name)
Dsu::Models::Project.find(project_name: project_name)
elsif use_by_project_number?
Dsu::Models::Project.find_by_number(project_number: project_number)
elsif use_by_project_default?
Dsu::Models::Project.default_project
end
end

def project_errors?
project&.invalid?
end

def project_name
return unless use_by_project_name?

@project_name_or_number
end

def project_number
return -1 unless use_by_project_number?

@project_name_or_number.to_i
@project ||= Models::Project.find_or_initialize(project_name: project_name)
end
end
end
Expand Down
30 changes: 27 additions & 3 deletions lib/dsu/subcommands/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
require_relative '../presenters/project/create_presenter'
require_relative '../presenters/project/delete_presenter'
require_relative '../presenters/project/list_presenter'
require_relative '../presenters/project/use_by_number_presenter'
require_relative '../presenters/project/use_presenter'
require_relative '../views/project/create'
require_relative '../views/project/use'
require_relative '../views/project/use_by_number'
require_relative '../views/shared/error'
require_relative 'base_subcommand'

Expand Down Expand Up @@ -65,9 +67,31 @@ def show
option :prompts, type: :hash, default: {}, hide: true, aliases: '-p'
def use(project_name_or_number = nil)
options = configuration.to_h.merge(self.options).with_indifferent_access
presenter = Presenters::Project::UsePresenter.new(project_name_or_number: project_name_or_number,
options: options)
Views::Project::Use.new(presenter: presenter, options: options).render
presenter = use_presenter_for(project_name_or_number, options: options)
use_view_for(project_name_or_number, presenter: presenter, options: options).render
end

private

def use_view_for(project_name, presenter:, options:)
if project_number?(project_name)
Views::Project::UseByNumber.new(presenter: presenter, options: options)
else
Views::Project::Use.new(presenter: presenter, options: options)
end
end

def use_presenter_for(project_name, options:)
if project_number?(project_name)
Presenters::Project::UseByNumberPresenter.new(project_number: project_name.to_i, options: options)
else
project_name = Models::Project.default_project_name if project_name.blank?
Presenters::Project::UsePresenter.new(project_name: project_name, options: options)
end
end

def project_number?(project_name)
/^[+-]?\d+(\.\d+)?$/.match?(project_name.to_s)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/dsu/views/project/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ def display_project_does_not_exists
end

def display_deleted_project_message
message = I18n.t('subcommands.project.delete.messages.using_project',
message = I18n.t('subcommands.project.delete.messages.deleted_project',
project_name: presenter.project_name_or_number)
puts apply_theme(message, theme_color: color_theme.success)
end

def delete_prompt
I18n.t('subcommands.project.delete.prompts.delete_confirm',
project_name: presenter.project_name_or_number, description: presenter.project_description)
project_name: presenter.project_name, description: presenter.project_description)
end

def delete_prompt_options
Expand Down
16 changes: 6 additions & 10 deletions lib/dsu/views/project/use.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def display_project_use_prompt
end

def display_use_project_cancelled_message
message = I18n.t('subcommands.project.messages.cancelled', project_name: presenter.project_name_or_number)
message = I18n.t('subcommands.project.messages.cancelled')
puts apply_theme(message, theme_color: color_theme.info)
end

Expand All @@ -60,25 +60,21 @@ def display_project_errors
end

def display_project_does_not_exists
message = if presenter.use_by_project_number?
I18n.t('subcommands.project.messages.number_does_not_exist',
project_number: presenter.project_name_or_number)
else
I18n.t('subcommands.project.messages.does_not_exist',
project_name: presenter.project_name_or_number)
end
message = I18n.t('subcommands.project.messages.does_not_exist',
project_name: presenter.project_name)
puts apply_theme(message, theme_color: color_theme.error)
end

def display_using_project_message
message = I18n.t('subcommands.project.use.messages.using_project',
project_name: presenter.project_name_or_number)
project_name: presenter.project_name)
puts apply_theme(message, theme_color: color_theme.success)
end

def use_prompt
I18n.t('subcommands.project.use.prompts.use_confirm',
project_name: presenter.project_name_or_number, description: presenter.project_description)
project_name: presenter.project_name,
description: presenter.project_description)
end

def use_prompt_options
Expand Down
23 changes: 23 additions & 0 deletions lib/dsu/views/project/use_by_number.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require_relative '../../env'
require_relative '../../models/color_theme'
require_relative '../../support/ask'
require_relative '../../support/color_themable'
require_relative 'use'

module Dsu
module Views
module Project
class UseByNumber < Use
private

def display_project_does_not_exists
message = I18n.t('subcommands.project.messages.number_does_not_exist',
project_number: presenter.project_number)
puts apply_theme(message, theme_color: color_theme.error)
end
end
end
end
end
Loading

0 comments on commit 8457a0d

Please sign in to comment.