Более читаемый, DRY класс с созданием проблемы jira - PullRequest
0 голосов
/ 02 апреля 2020

В моем приложении rails у меня есть базовый класс - creator - который создает проблему на доске Jira с использованием jira- ruby gem . Я хочу использовать его в 15 различных сценариях ios, но единственное, что будет отличаться, - это обязательные поля, такие как summary, description и issuetype. Чтобы избежать дубликатов, я использую наследование, где в каждом случае у меня есть класс только с одним методом - required_fields.

creator

module Jira
  class Creator
    def initialize(webhook)
      @webhook = webhook
    end

    def call
      issue = client.Issue.build
      issue.save(ticket_class.new(webhook).required_fields)
    end

    private

    def client
      @client ||= Jira::JiraConnection.new.call
    end

    def ticket_class
      @ticket_class ||= "Issues::#{webhook.action_type_class}".constantize"
    end

    def required_fields; end
  end
end

Пример наследования класса от Creator:

class NewRepo < Creator
  def required_fields
    {
      'fields' => {
        'summary' => 'Create new repo <github_repo> for <Github user>',
        'description' => 'This is an automatic confirmation of creating new PRIVATE repo
        - <github_repo> for <Github user>',
        'project' => { 'key' => 'TEST' },
        'issuetype' => { 'id' => '12580' },
        'customfield_15100' => 'None'
      }
    }
  end
end

Это required_fields не так читабельно, как создать такой класс, где ha sh в required_fields будет выглядеть так:

{
  'fields' => {
    'summary' => new_repo,
    'project' => testing_project,
    'issuetype' => autoresolved,
    'customfield_15100' => 'None'
  }
}

Как положить new_repo, testing_project et c (какой метод, вероятно, будет?) внутри этого га sh? я должен слить это как-то? Или, может быть, есть лучший способ сделать этот ха sh более читабельным?

Вот различные сценарии ios для обязательных полей, которые я хочу использовать, зависит от информации о webhook:

class AddMember < Creator
  def required_fields
    {
      'fields' => {
        'summary' => 'Add <Github user> collaborator to <github_repo>',
        'description' => 'This is an automatic ticket confirmation of user added',
        'project' => { 'key' => 'New Board' },
        'issuetype' => { 'id' => '12581' },
        'customfield_15100' => 'None'
      }
    }
  end
end

class DeleteRepo < Creator
  def required_fields
    {
      'fields' => {
        'summary' => 'Recheck <Github user> deleted <github_repo>',
        'description' => 'This is an automatic ticket confirmation of delete repo <github_repo>',
        'project' => { 'key' => 'Second Board' },
        'issuetype' => { 'id' => '1234' },
        'customfield_15100' => 'None'
      }
    }
  end
end
...