Sha256: 7a89875f0f3888ded9e4aefe7271bf7994ae3bda9cbd7302b1fdbccb0a6f88f2

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module Evertils
  module Controller
    class Render < Controller::Base
      def from_file
        set_allowed_fields

        if @allowed_fields[:action] == 'create'
          return Notify.warning("Note already exists\n- #{@link}") if note_exists?

          Notify.info 'Note not found, creating a new one'
        end

        execute_action(@allowed_fields[:action])
      end

      def note_exists?
        helper = Evertils::Helper::Note.instance
        note = helper.wait_for_with_grammar(grammar)

        @link = helper.external_url_for(note.entity) unless note.entity.nil?

        note.exists?
      end

      def execute_action(action)
        case action
        when nil
          Notify.info 'Action not provided, creating new note...'
          Action::Create.new(@allowed_fields)
        when 'create'
          Action::Create.new(@allowed_fields)
        when 'duplicate_previous'
          Action::DuplicatePrevious.new(@allowed_fields)
        else
          Action::Default.new(action: action)
        end
      end

      private

      def set_allowed_fields
        @allowed_fields = config.translate_placeholders.pluck(
          :title,
          :title_format,
          :notebook,
          :path,
          :action,
          :tags
        )
      end

      def grammar
        terms = Grammar.new
        terms.notebook = @allowed_fields[:notebook]
        terms.tags = {
          day: Date.today.yday,
          week: Date.today.cweek
        }
        terms.created = Date.new(Date.today.year, 1, 1).strftime('%Y%m%d')
        terms
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
evertils-2.0.1 lib/evertils/controllers/render.rb