Sha256: 1b5b869a3e31b58c0cb21ed6b84dfaaa84bec65e2372596bb4652a22879b4697

Contents?: true

Size: 1.53 KB

Versions: 39

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require_relative '../models/entry'
require_relative '../support/color_themable'
require_relative '../support/descriptable'
require_relative '../support/fileable'
require_relative '../views/shared/error'

module Dsu
  module CommandServices
    # This class adds (does NOT update) an entry to an entry group by
    # writing it to the appropriate entry group json file.
    class AddEntryService
      include Support::ColorThemable
      include Support::Descriptable
      include Support::Fileable

      attr_reader :entry, :entry_group, :time

      delegate :description, to: :entry

      # :entry is an Entry object
      # :time is a Time object; the time of the entry group.
      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

      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

      private

      attr_writer :entry, :entry_group, :time
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
dsu-3.0.5 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.4 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.3 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.1 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.0 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.0.beta.3 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.0.beta.2 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.0.beta.1 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.0.beta.0 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.0.alpha.12 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.0.alpha.11 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.0.alpha.10 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.0.alpha.9 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.0.alpha.8 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.0.alpha.7 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.0.alpha.6 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.0.alpha.5 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.0.alpha.4 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.0.alpha.3 lib/dsu/command_services/add_entry_service.rb
dsu-3.0.0.alpha.2 lib/dsu/command_services/add_entry_service.rb