Sha256: b2da504434e759cfa608826d7e0adee762a2ff14b1dabe14b56fa9e6ee987322

Contents?: true

Size: 1.54 KB

Versions: 9

Compression:

Stored size: 1.54 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 = 'An error was encountered; the 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

9 entries across 9 versions & 1 rubygems

Version Path
dsu-2.0.8 lib/dsu/command_services/add_entry_service.rb
dsu-2.0.7 lib/dsu/command_services/add_entry_service.rb
dsu-2.0.6 lib/dsu/command_services/add_entry_service.rb
dsu-2.0.5 lib/dsu/command_services/add_entry_service.rb
dsu-2.0.4 lib/dsu/command_services/add_entry_service.rb
dsu-2.0.3 lib/dsu/command_services/add_entry_service.rb
dsu-2.0.2 lib/dsu/command_services/add_entry_service.rb
dsu-2.0.1 lib/dsu/command_services/add_entry_service.rb
dsu-2.0.0.alpha.1 lib/dsu/command_services/add_entry_service.rb