Sha256: 5c9880e44bf249533a11044d8126cdd0e1320f13593e038d807d1c0d1ed50ea9

Contents?: true

Size: 1.79 KB

Versions: 3

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

require 'time'
require 'active_support/core_ext/numeric/time'
require_relative '../../models/entry_group'
require_relative '../../support/colorable'
require_relative '../../support/say'
require_relative '../../support/time_formatable'

module Dsu
  module Views
    module EntryGroup
      class Show
        include Support::Colorable
        include Support::Say
        include Support::TimeFormatable

        def initialize(entry_group:, options: {})
          raise ArgumentError, 'entry_group is nil' if entry_group.nil?
          raise ArgumentError, 'entry_group is the wrong object type' unless entry_group.is_a?(Models::EntryGroup)
          raise ArgumentError, 'options is nil' if options.nil?
          raise ArgumentError, 'options is the wrong object type' unless options.is_a?(Hash)

          @entry_group = entry_group
          @options = options || {}
        end

        def call
          # Just in case the entry group is invalid, we'll
          # validate it before displaying it.
          entry_group.validate!
          render_entry_group!
        rescue ActiveModel::ValidationError
          puts "Error(s) encountered: #{entry_group.errors.full_messages}"
          raise
        end
        alias render call

        private

        attr_reader :entry_group, :options

        def render_entry_group!
          say formatted_time(time: entry_group.time), HIGHLIGHT
          say('(no entries available for this day)') and return if entry_group.entries.empty?

          entry_group.entries.each_with_index do |entry, index|
            prefix = "#{format('%03s', index + 1)}. #{entry.uuid}"
            description = colorize_string(string: entry.description, mode: :bold)
            say "#{prefix} #{description}"
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dsu-0.1.0.alpha.4 lib/dsu/views/entry_group/show.rb
dsu-0.1.0.alpha.3 lib/dsu/views/entry_group/show.rb
dsu-0.1.0.alpha.2 lib/dsu/views/entry_group/show.rb