Skip to content

Commit

Permalink
Start to implement dsu project command
Browse files Browse the repository at this point in the history
  • Loading branch information
gangelo committed Jan 16, 2024
1 parent 2c7d1bc commit 5bb0b39
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 9 deletions.
17 changes: 11 additions & 6 deletions lib/dsu/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require 'fileutils'

# require_relative '../support/presentable'
require_relative '../crud/json_file'
require_relative '../migration/version'
require_relative '../models/entry_group'
Expand Down Expand Up @@ -35,13 +34,10 @@ class Project

def initialize(project_name:, description: nil, version: nil, options: {})
raise ArgumentError, 'project_name is blank' if project_name.blank?
raise ArgumentError, 'project_name is not a String' unless project_name.is_a?(String)
raise ArgumentError, 'description is blank' if description.blank?
raise ArgumentError, 'description is not a String' unless description.is_a?(String)
raise ArgumentError, 'version is the wrong object type' unless version.is_a?(Integer) || version.nil?

self.project_name = project_name
self.description = description || "#{project_name.capitalize} project"
self.description = description
self.version = version || VERSION
self.options = options || {}
end
Expand Down Expand Up @@ -271,7 +267,16 @@ def current_project_file_path_for(current_project_file:)

private

attr_writer :description, :current_project_file, :options, :version
attr_writer :current_project_file, :options, :version

def description=(value)
description = if value.blank?
"#{project_name.capitalize} project"
else
value
end
@description = description
end

def project_name=(value)
@project_name = begin
Expand Down
38 changes: 38 additions & 0 deletions lib/dsu/presenters/project/create_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

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

module Dsu
module Presenters
module Project
class CreatePresenter < BasePresenterEx
attr_reader :project

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

@project = Models::Project.new(project_name: project_name, description: description, options: options)
end

def render(response:)
return false unless response

project.create!
end

def project_already_exists?
project.exist?
end

def project_errors?
project.invalid?
end

private

attr_reader :options
end
end
end
end
10 changes: 7 additions & 3 deletions lib/dsu/subcommands/project.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative '../views/import'
require_relative '../presenters/project/create_presenter'
require_relative '../views/project/create'
require_relative '../views/shared/error'
require_relative 'base_subcommand'

Expand All @@ -20,8 +21,11 @@ class Project < BaseSubcommand
option :description, type: :string, required: false, aliases: '-d', banner: 'DESCRIPTION'
option :prompts, type: :hash, default: {}, hide: true, aliases: '-p'
def create
# Views::Import.new(presenter: all_presenter(import_file_path: options[:import_file],
# options: options)).render
project_name = options[:project_name]
description = options[:description]
presenter = Presenters::Project::CreatePresenter.new(project_name: project_name,
description: description, options: options)
Views::Project::Create.new(presenter: presenter, options: options).render
end

desc I18n.t('subcommands.project.delete.desc'), I18n.t('subcommands.project.delete.usage')
Expand Down
80 changes: 80 additions & 0 deletions lib/dsu/views/project/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# frozen_string_literal: true

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

module Dsu
module Views
module Project
class Create
include Support::Ask
include Support::ColorThemable

def initialize(presenter:, options: {})
@presenter = presenter
@options = options&.dup || {}
@color_theme = Models::ColorTheme.find(theme_name: theme_name)
end

def render
return display_project_errors if presenter.project_errors?
return display_project_already_exists if presenter.project_already_exists?

response = display_project_create_prompt
if presenter.render response: response
display_project_created_message
else
display_cancelled_message
end
rescue StandardError => e
puts apply_theme(e.message, theme_color: color_theme.error)
end

private

attr_reader :presenter, :color_theme, :options

def project_name
presenter.project.project_name
end

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

def display_project_create_prompt
yes?(prompt_with_options(prompt: create_prompt, options: create_prompt_options), options: options)
end

def display_project_created_message
puts apply_theme(I18n.t('subcommands.project.create.messages.created', project_name: project_name), theme_color: color_theme.info)
end

def display_project_errors
presenter.project.errors.full_messages.each do |error|
puts apply_theme(error, theme_color: color_theme.error)
end
end

def display_project_already_exists
message = I18n.t('subcommands.project.create.messages.already_exists', project_name: project_name)
puts apply_theme(message, theme_color: color_theme.error)
end

def create_prompt
I18n.t('subcommands.project.create.prompts.create_confirm', project_name: project_name)
end

def create_prompt_options
I18n.t('subcommands.project.create.prompts.create_options')
end

def theme_name
@theme_name ||= options.fetch(:theme_name, Models::Configuration.new.theme_name)
end
end
end
end
end
9 changes: 9 additions & 0 deletions lib/locales/en/subcommands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,15 @@ en:
$ dsu project create -n "My project" -d "My project description"
$ dsu p c -n "My project"
messages:
created: Created project "%{project_name}".
cancelled: Cancelled.
already_exists: Project "%{project_name}" already exists.
prompts:
create_confirm: Create project "%{project_name}"?
create_options:
- y
- N
delete:
desc: delete|d OPTIONS
usage: Permanently deletes the DSU project indicated by the given OPTIONS
Expand Down

0 comments on commit 5bb0b39

Please sign in to comment.