Class: Dsu::CommandServices::AddEntryService

Inherits:
Object
  • Object
show all
Includes:
Support::ColorThemable, Support::Descriptable, Support::Fileable
Defined in:
lib/dsu/command_services/add_entry_service.rb

Overview

This class adds (does NOT update) an entry to an entry group by writing it to the appropriate entry group json file.

Constant Summary

Constants included from Support::Fileable

Support::Fileable::MIGRATION_VERSION_FILE_NAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::Fileable

#backup_folder_for, #config_file_name, #config_folder, #config_path, #current_project_file, #current_project_file_name, #dsu_folder, #entries_file_name, #entries_folder, #entries_path, #gem_dir, #migration_version_folder, #migration_version_path, #project_file_for, #project_folder_for, #projects_folder, #root_folder, #seed_data_dsu_configuration_for, #seed_data_dsu_folder_for, #temp_folder, #theme_file_name, #themes_folder, #themes_path

Methods included from Support::Descriptable

included, #short_description

Methods included from Support::ColorThemable

apply_theme, #prompt_with_options

Constructor Details

#initialize(entry:, time:) ⇒ AddEntryService

:entry is an Entry object :time is a Time object; the time of the entry group.

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
# File 'lib/dsu/command_services/add_entry_service.rb', line 24

def initialize(entry:, time:)
  raise ArgumentError, 'entry is nil' if entry.nil?
  raise ArgumentError, 'entry is the wrong object type' unless entry.is_a?(Models::Entry)
  raise ArgumentError, 'time is nil' if time.nil?
  raise ArgumentError, 'time is the wrong object type' unless time.is_a?(Time)

  @entry = entry
  @time = time
  @entry_group = Models::EntryGroup.find_or_initialize(time: time)
end

Instance Attribute Details

#entryObject

Returns the value of attribute entry.



18
19
20
# File 'lib/dsu/command_services/add_entry_service.rb', line 18

def entry
  @entry
end

#entry_groupObject

Returns the value of attribute entry_group.



18
19
20
# File 'lib/dsu/command_services/add_entry_service.rb', line 18

def entry_group
  @entry_group
end

#timeObject

Returns the value of attribute time.



18
19
20
# File 'lib/dsu/command_services/add_entry_service.rb', line 18

def time
  @time
end

Instance Method Details

#callObject



35
36
37
38
39
40
41
42
43
# File 'lib/dsu/command_services/add_entry_service.rb', line 35

def call
  entry.validate!
  entry_group.entries << entry
  entry_group.save!
  entry
rescue ActiveModel::ValidationError => e
  header = I18n.t('headers.entry.could_not_be_added')
  Views::Shared::Error.new(messages: e.message, header: header).render
end