Ruby on rails 避免重复使用相同代码的新类
我有一个类在Jira board中创建了一个问题。我想在5种不同的场景中使用它,但唯一不同的是必填字段,如Ruby on rails 避免重复使用相同代码的新类,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个类在Jira board中创建了一个问题。我想在5种不同的场景中使用它,但唯一不同的是必填字段,如摘要,说明,和发行类型。如何处理这样的场景,以避免创建几个具有90%相同内容的类 这是本课程的主要内容: module Jira class TicketCreator def call issue = client.Issue.build issue.save(required_fields) end private def
摘要
,说明
,和发行类型
。如何处理这样的场景,以避免创建几个具有90%相同内容的类
这是本课程的主要内容:
module Jira
class TicketCreator
def call
issue = client.Issue.build
issue.save(required_fields)
end
private
def client
@client ||= Jira::JiraConnection.new.call
end
def required_fields
#data from below examples
end
end
end
以下是我想使用的必填字段的场景,具体取决于webhook信息:
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
def required_fields
{
'fields' => {
'summary' => 'Add <Github user> collaborator to <github_repo>',
'description' => 'This is an automatic ticket confirmation of user added',
'project' => { 'key' => 'TEST' },
'issuetype' => { 'id' => '12580' }, # nonautoresolved
'customfield_15100' => 'None'
}
}
end
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' => 'TEST' }, # change to project_key
'issuetype' => { 'id' => '12579' }, # autoresolved
'customfield_15100' => 'None'
}
}
end
def必填_字段
{
“字段”=>{
'摘要'=>'为'',
'description'=>'这是创建新私人回购的自动确认
-对于',
'project'=>{'key'=>'TEST'},
'issuetype'=>{'id'=>'12580'},
“customfield_15100”=>“无”
}
}
结束
def必填_字段
{
“字段”=>{
'摘要'=>'将协作者添加到',
'description'=>'这是用户添加的自动票证确认',
'project'=>{'key'=>'TEST'},
'issuetype'=>{'id'=>'12580'},#非自动解析
“customfield_15100”=>“无”
}
}
结束
def必填_字段
{
“字段”=>{
'摘要'=>'重新检查已删除',
'description'=>'这是删除回购的自动票据确认',
'project'=>{'key'=>'TEST'},#更改为project_key
'issuetype'=>{'id'=>'12579'},#自动解决
“customfield_15100”=>“无”
}
}
结束
如何避免创建新类,其中唯一的区别是此
必填\u字段
方法?您可以将关键字参数与合并默认选项和传递的选项:
module Jira
class TicketCreator
# Do setup at initializion instead
def initialize
# FIXME - style violation - this should be client.issue.build
@issue = client.Issue.build
end
def call(**options)
@issue.save(required_fields(options))
end
# Takes care of initializion so that you can call
# Jira::TicketCreator.call - you can DRY this though inheritance or mixins
def self.call(**options)
new.call(options)
end
private
def client
@client ||= Jira::JiraConnection.new.call
end
def required_fields(**options)
{
'fields' => {
'summary' => 'Recheck <Github user> deleted <github_repo>',
'description' => 'This is an automatic ticket confirmation of delete repo <github_repo>',
'project' => { 'key' => 'SUP' }, # change to project_key
'issuetype' => { 'id' => '12579' }, # autoresolved
'customfield_15100' => 'None'
}.reverse_merge(options.deep_stringify_keys)
}
end
end
end
你能将所需的_字段发送给你的班级吗
# TicketCreator.new(required_fields).call
module Jira
class TicketCreator
def initialize(required_fields)
@required_fields = required_fields
end
def call
issue = client.Issue.build
issue.save(@required_fields)
end
private
def client
@client ||= Jira::JiraConnection.new.call
end
end
end
如果您希望为每一个都使用一个命令,可以使用继承:
module Jira
class Base
def call
issue = client.Issue.build
issue.save(required_fields)
end
private
def client
@client ||= Jira::JiraConnection.new.call
end
def required_fields; end
end
end
module Jira
class CreateRepo < Base
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
end
module Jira
class AddCollaborator < Base
def required_fields
{
# data
}
end
end
end
模块Jira
阶级基础
def呼叫
issue=client.issue.build
问题.保存(必填字段)
结束
私有的
def客户端
@client | |=Jira::JiraConnection.new.call
结束
def必填_字段;结束
结束
结束
吉拉模块
类CreateRepo {
'摘要'=>'为'',
'description'=>'这是创建新私人回购的自动确认
-对于',
'project'=>{'key'=>'TEST'},
'issuetype'=>{'id'=>'12580'},
“customfield_15100”=>“无”
}
}
结束
结束
结束
吉拉模块
类AddCollaborator
module Jira
class Base
def call
issue = client.Issue.build
issue.save(required_fields)
end
private
def client
@client ||= Jira::JiraConnection.new.call
end
def required_fields; end
end
end
module Jira
class CreateRepo < Base
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
end
module Jira
class AddCollaborator < Base
def required_fields
{
# data
}
end
end
end