Skip to content

Commit

Permalink
Implement dsu project create
Browse files Browse the repository at this point in the history
  • Loading branch information
gangelo committed Jan 19, 2024
1 parent 5bb0b39 commit 971281e
Show file tree
Hide file tree
Showing 32 changed files with 619 additions and 206 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,9 @@ Style/StringMethods:
# Checks for use of parentheses around ternary conditions.
Style/TernaryParentheses:
EnforcedStyle: require_parentheses_when_complex

RSpec/NotToNot:
EnforcedStyle: to_not
SupportedStyles:
- to_not
- not_to
2 changes: 1 addition & 1 deletion current_project.bak
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": 20230613121411,
"project": "default"
"project_name": "default"
}
209 changes: 93 additions & 116 deletions lib/dsu/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

require_relative '../crud/json_file'
require_relative '../migration/version'
require_relative '../models/entry_group'
require_relative '../models/configuration'
require_relative '../services/project/hydrator_service'
require_relative '../support/descriptable'
require_relative '../support/fileable'
require_relative '../support/project_file_system'
require_relative '../validators/description_validator'
Expand All @@ -17,8 +18,9 @@ module Models
# This class represents a project. A project is a collection of entry groups.
class Project
include ActiveModel::Model
include Support::ProjectFileSystem
include Support::Descriptable
include Support::Fileable
include Support::ProjectFileSystem

VERSION = Migration::VERSION
MIN_PROJECT_NAME_LENGTH = 2
Expand Down Expand Up @@ -51,10 +53,6 @@ def ==(other)
end
alias eql? ==

# def clone
# self.class.new(project_name: project_name, description: description, version: version, options: options)
# end

def create
self.class.create(project_name: project_name, description: description)
end
Expand All @@ -65,6 +63,20 @@ def create!
end
alias save! create!

def current_project?
project_name == self.class.current_project_name
end

def default!
return if default_project?

self.class.default!(project: self)
end

def default_project?
project_name == self.class.default_project_name
end

# def delete
# self.class.delete(project_name: project_name)
# end
Expand All @@ -73,27 +85,18 @@ def create!
# self.class.delete!(time: time)
# end

# def exist?
# project_exist?
# end
# alias persisted? exist?

# # Returns true if the project folder exists
# def project_exist?
# self.class.project_exist?(project_name: project_name)
# end
# alias project_persisted? project_exist?

# # Returns true if the project file exists
# def file_exist?
# self.class.file_exist?(project_name: project_name)
# end
# alias file_persisted? file_exist?

def hash
[project_name, description, version].map(&:hash).hash
end

def project_file
self.class.project_file(project_name: project_name)
end

def project_folder
self.class.project_folder(project_name: project_name)
end

def to_h
{
version: version,
Expand All @@ -102,17 +105,33 @@ def to_h
}
end

def update
self.class.update(project_name: project_name, description: description, version: version, options: options)
end
# def update
# self.class.update(project_name: project_name, description: description, version: version, options: options)
# end

# def update!
# self.class.update!(project_name: project_name, description: description, version: version, options: options)
# end

def use!
return if current_project?

def update!
self.class.update!(project_name: project_name, description: description, version: version, options: options)
self.class.use!(project: self)
end

class << self
delegate :project_folder_for, to: Support::Fileable

def all
project_metadata.map do |metadata|
find(project_name: metadata[:project_name])
end
end

# def any?
# project_metadata.any?
# end

def create(project_name:, description: nil, options: {})
Models::Project.new(project_name: project_name, description: description, options: options).tap do |project|
project.validate!
Expand All @@ -131,31 +150,21 @@ def create!(project_name:, description: nil, options: {})
end

def current_project
current_project_file = Support::Fileable.current_project_file
Crud::JsonFile.read!(file_path: current_project_file).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
find(project_name: current_project_name)
end

# def all
# entry_groups.filter_map do |file_path|
# entry_group_file_name = File.basename(file_path)
# next unless entry_file_name.match?(ENTRIES_FILE_NAME_REGEX)
#
# entry_date = File.basename(entry_file_name, '.*')
# find time: Time.parse(entry_date)
# end
# end
def default!(project:)
project.validate!

# def any?
# entry_files.any? do |file_path|
# entry_date = File.basename(file_path, '.*')
# entry_date.match?(ENTRIES_FILE_NAME_TIME_REGEX)
# end
# end
Models::Configuration.new.tap do |configuration|
configuration.default_project = project.project_name
configuration.save!
end
end

def default_project
find(project_name: default_project_name)
end

# def delete(project_name:)
# # TODO: read all entry groups and delete them
Expand All @@ -169,99 +178,67 @@ def current_project
# # superclass.delete!(file_path: project_folder_for(project_name: project_name))
# end

def project_exist?(project_name:)
Dir.exist?(project_folder_for(project_name: project_name))
end
alias project_persisted? project_exist?

def file_exist?(project_name:)
return false unless project_exist?(project_name: project_name)

File.exist?(current_project_file_path_for(current_project_file: project_folder_for(project_name: project_name)))
end
alias file_persisted? file_exist?

# def entry_groups(between:)
# entry_group_times(between: between).filter_map do |time|
# Models::EntryGroup.find(time: Time.parse(time))
# end
# end

def find(project_name:)
unless project_exist?(project_name: project_name)
unless project_folder_exist?(project_name: project_name)
raise I18n.t('models.project.errors.does_not_exist', project_name: project_name)
end

project_file_path = current_project_file_path_for(current_project_file: current_project_file)
project_file_path = project_file_for(project_name: project_name)

unless file_exist?(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)
end

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

def find_or_create(project_name:)
find_or_initialize(project_name: project_name).tap do |project|
project.write! unless project.persisted?
# project_number is 1 based.
def find_by_number(project_number:)
project = project_metadata.find do |metadata|
metadata[:project_number] == project_number
end
end
return unless project

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
find(project_name: project[:project_name])
end

# def write(file_data:, file_path:)
# if file_data[:entry_groups].empty?
# superclass.delete(file_path: file_path)
# return true
# def find_or_create(project_name:)
# find_or_initialize(project_name: project_name).tap do |project|
# project.write! unless project.persisted?
# end

# super
# end

# def write!(file_data:, file_path:)
# if file_data[:entries].empty?
# superclass.delete!(file_path: file_path)
# return
# 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

# super
# end

# def project_folder_for(project_name:)
# Support::Fileable.project_folder_for(project_name: project_name)
# def update(project_name:, description:, version:, options:)
# # TODO: Update the project
# end

# def entry_files
# current_project_file = File.join(project_folder_for(project_name: project_name))
# Dir.glob("#{Support::Fileable.entries_folder}/*")
# def update!(project_name:, description:, version:, options:)
# # TODO: Update the project
# end

def update(project_name:, description:, version:, options:)
end

def update!(project_name:, description:, version:, options:)
end

private
def use!(project:)
project.validate!

def current_project_file_path_for(current_project_file:)
File.join(current_project_file, Support::Fileable.current_project_file_name)
current_project_hash = { version: project.version, project_name: project.project_name }
Crud::JsonFile.write!(file_data: current_project_hash, file_path: current_project_file)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/dsu/presenters/project/create_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(project_name:, description:, options: {})
@project = Models::Project.new(project_name: project_name, description: description, options: options)
end

def render(response:)
def respond(response:)
return false unless response

project.create!
Expand Down
Loading

0 comments on commit 971281e

Please sign in to comment.