Sha256: b49c7b764c0e152635ecf96c0f99eaef429c3118a623a323db0b233948e1d139

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Jive
  class Issue
    class << self
      def what_type?
        Jive.prompt?(
          Jive.root
            .join("lib/jive/templates")
            .glob("*.md")
            .map { |x| x.basename.to_s.gsub(".md", "") }
        )
      end

      def create!(name:, type:, repo: Repo.current)
        new(repo: repo, name: name, type: type)
      end

      def for(type, repo: Repo.current)
        dir_for(type, repo: repo).glob("*.md").map do |x|
          name = x.basename.to_s.gsub(".md", "")
          new(repo: repo, name: name, type: type)
        end
      end

      def dir_for(type, repo: Repo.current)
        Jive.home
            .join(repo.uri.host)
            .join(repo.nwo)
            .join(type)
      end
    end

    def initialize(name:, type:, repo: Repo.current)
      @repo = repo
      @type = type
      @name = name
    end

    def file_name
      "#{name.gsub(/[^a-z0-9\-_]+/i, "-").downcase}.md"
    end

    def edit(editor: ENV["EDITOR"])
      Jive.shell.execute([editor, issue.to_s])
    end

    private

    attr_reader :repo, :name, :type

    def issue
      @issue ||=
        begin
          dir = self.class.dir_for(type, repo: repo)
          Jive.shell.execute([:mkdir, "-p", dir])
          dir.join(file_name).tap do |file|
            file.write(template_for(type).read) unless file.exist?
          end
        end
    end

    def template_for(type)
      Jive.root.join("lib/jive/templates/#{type}.md")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jive-0.7.0 lib/jive/issue.rb
jive-0.6.0 lib/jive/issue.rb
jive-0.5.0 lib/jive/issue.rb