Sha256: 86acff9902adbd6609e1c3e517c09a557d5bd2ef05a518b0b1faabd4d153384c

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Evertils
  module Action
    class DuplicatePrevious < Action::Base
      def initialize(args)
        super(args)

        @args[:content] = find_previous(args)

        query = Evertils::Common::Query::Simple.new
        query.create_note_from_hash(@args)
      end

      private

      def find_previous(args)
        day = Date.today
        note = nil

        Notify.info("Searching for last #{@args[:notebook]}...")

        (1..Evertils::Type::Base::MAX_SEARCH_SIZE).each do |iter|
          day -= 1
          dow = day.strftime('%a')

          # always skip weekends, even if there is a note for those days
          next if %i[Sat Sun].include?(dow)

          note_title = previous_note_title(@args[:title_format], day)
          note = @note_helper.model.find_note_contents(note_title).entity

          Notify.info(" (#{iter}) #{note_title}")

          break unless note.nil?
        end

        @api.convert_to_xml(note.content).prepare
      end

      def previous_note_title(fmt, date)
        # not a good solution but it works
        # TODO: fix this
        replacements = {
          '%DOY%': date.yday,
          '%MONTH_NAME%': date.strftime('%B'),
          '%MONTH%': date.month,
          '%DAY%': date.day,
          '%DOW%': date.wday,
          '%DOW_NAME%': date.strftime('%a'),
          '%YEAR%': date.year,
          '%WEEK%': date.cweek,
          '%WEEK_START%': Date.commercial(date.year, date.cweek, 1),
          '%WEEK_END%': Date.commercial(date.year, date.cweek, 5)
        }

        title_format = fmt.dup

        replacements.each_pair { |k, v| title_format.gsub!(k.to_s, v.to_s) }
        title_format
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
evertils-1.0.12 lib/evertils/actions/duplicate_previous.rb
evertils-1.0.11 lib/evertils/actions/duplicate_previous.rb
evertils-1.0.10 lib/evertils/actions/duplicate_previous.rb