Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
d3490f69
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
d3490f69
编写于
8月 14, 2018
作者:
N
Nick Thomas
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Introduce a LicenseTemplate model and LicenseTemplateFinder helper
上级
ffd164d2
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
197 addition
and
0 deletion
+197
-0
app/finders/license_template_finder.rb
app/finders/license_template_finder.rb
+36
-0
app/models/license_template.rb
app/models/license_template.rb
+53
-0
spec/finders/license_template_finder_spec.rb
spec/finders/license_template_finder_spec.rb
+49
-0
spec/models/license_template_spec.rb
spec/models/license_template_spec.rb
+59
-0
未找到文件。
app/finders/license_template_finder.rb
0 → 100644
浏览文件 @
d3490f69
# LicenseTemplateFinder
#
# Used to find license templates, which may come from a variety of external
# sources
#
# Arguments:
# popular: boolean. When set to true, only "popular" licenses are shown. When
# false, all licenses except popular ones are shown. When nil (the
# default), *all* licenses will be shown.
class
LicenseTemplateFinder
attr_reader
:params
def
initialize
(
params
=
{})
@params
=
params
end
def
execute
Licensee
::
License
.
all
(
featured:
popular_only?
).
map
do
|
license
|
LicenseTemplate
.
new
(
id:
license
.
key
,
name:
license
.
name
,
nickname:
license
.
nickname
,
category:
(
license
.
featured?
?
:Popular
:
:Other
),
content:
license
.
content
,
url:
license
.
url
,
meta:
license
.
meta
)
end
end
private
def
popular_only?
params
.
fetch
(
:popular
,
nil
)
end
end
app/models/license_template.rb
0 → 100644
浏览文件 @
d3490f69
class
LicenseTemplate
PROJECT_TEMPLATE_REGEX
=
%r{[
\<\{\[
]
(project|description|
one
\s
line
\s
.+
\s
what
\s
it
\s
does
\.
) # matching the start and end is enough here
[
\>\}\]
]}xi
.
freeze
YEAR_TEMPLATE_REGEX
=
/[<{\[](year|yyyy)[>}\]]/i
.
freeze
FULLNAME_TEMPLATE_REGEX
=
%r{[
\<\{\[
]
(fullname|name
\s
of
\s
(author|copyright
\s
owner))
[
\>\}\]
]}xi
.
freeze
attr_reader
:id
,
:name
,
:category
,
:nickname
,
:url
,
:meta
alias_method
:key
,
:id
def
initialize
(
id
:,
name
:,
category
:,
content
:,
nickname:
nil
,
url:
nil
,
meta:
{})
@id
=
id
@name
=
name
@category
=
category
@content
=
content
@nickname
=
nickname
@url
=
url
@meta
=
meta
end
def
popular?
category
==
:Popular
end
alias_method
:featured?
,
:popular?
# Returns the text of the license
def
content
if
@content
.
respond_to?
(
:call
)
@content
=
@content
.
call
else
@content
end
end
# Populate placeholders in the LicenseTemplate content
def
resolve!
(
project_name:
nil
,
fullname:
nil
,
year:
Time
.
now
.
year
.
to_s
)
# Ensure the string isn't shared with any other instance of LicenseTemplate
new_content
=
content
.
dup
new_content
.
gsub!
(
YEAR_TEMPLATE_REGEX
,
year
)
if
year
.
present?
new_content
.
gsub!
(
PROJECT_TEMPLATE_REGEX
,
project_name
)
if
project_name
.
present?
new_content
.
gsub!
(
FULLNAME_TEMPLATE_REGEX
,
fullname
)
if
fullname
.
present?
@content
=
new_content
self
end
end
spec/finders/license_template_finder_spec.rb
0 → 100644
浏览文件 @
d3490f69
require
'spec_helper'
describe
LicenseTemplateFinder
do
describe
'#execute'
do
subject
(
:result
)
{
described_class
.
new
(
params
).
execute
}
let
(
:categories
)
{
categorised_licenses
.
keys
}
let
(
:categorised_licenses
)
{
result
.
group_by
(
&
:category
)
}
context
'popular: true'
do
let
(
:params
)
{
{
popular:
true
}
}
it
'only returns popular licenses'
do
expect
(
categories
).
to
contain_exactly
(
:Popular
)
expect
(
categorised_licenses
[
:Popular
]).
to
be_present
end
end
context
'popular: false'
do
let
(
:params
)
{
{
popular:
false
}
}
it
'only returns unpopular licenses'
do
expect
(
categories
).
to
contain_exactly
(
:Other
)
expect
(
categorised_licenses
[
:Other
]).
to
be_present
end
end
context
'popular: nil'
do
let
(
:params
)
{
{
popular:
nil
}
}
it
'returns all licenses known by the Licensee gem'
do
from_licensee
=
Licensee
::
License
.
all
.
map
{
|
l
|
l
.
key
}
expect
(
result
.
map
(
&
:id
)).
to
match_array
(
from_licensee
)
end
it
'correctly copies all attributes'
do
licensee
=
Licensee
::
License
.
all
.
first
found
=
result
.
find
{
|
r
|
r
.
key
==
licensee
.
key
}
aggregate_failures
do
%i[key name content nickname url meta featured?]
.
each
do
|
k
|
expect
(
found
.
public_send
(
k
)).
to
eq
(
licensee
.
public_send
(
k
))
end
end
end
end
end
end
spec/models/license_template_spec.rb
0 → 100644
浏览文件 @
d3490f69
require
'spec_helper'
describe
LicenseTemplate
do
describe
'#content'
do
it
'calls a proc exactly once if provided'
do
lazy
=
build_template
(
->
{
'bar'
})
content
=
lazy
.
content
expect
(
content
).
to
eq
(
'bar'
)
expect
(
content
.
object_id
).
to
eq
(
lazy
.
content
.
object_id
)
content
.
replace
(
'foo'
)
expect
(
lazy
.
content
).
to
eq
(
'foo'
)
end
it
'returns a string if provided'
do
lazy
=
build_template
(
'bar'
)
expect
(
lazy
.
content
).
to
eq
(
'bar'
)
end
end
describe
'#resolve!'
do
let
(
:content
)
do
<<~
TEXT
Pretend License
[project]
Copyright (c) [year] [fullname]
TEXT
end
let
(
:expected
)
do
<<~
TEXT
Pretend License
Foo Project
Copyright (c) 1985 Nick Thomas
TEXT
end
let
(
:template
)
{
build_template
(
content
)
}
it
'updates placeholders in a copy of the template content'
do
expect
(
template
.
content
.
object_id
).
to
eq
(
content
.
object_id
)
template
.
resolve!
(
project_name:
"Foo Project"
,
fullname:
"Nick Thomas"
,
year:
"1985"
)
expect
(
template
.
content
).
to
eq
(
expected
)
expect
(
template
.
content
.
object_id
).
not_to
eq
(
content
.
object_id
)
end
end
def
build_template
(
content
)
described_class
.
new
(
id:
'foo'
,
name:
'foo'
,
category: :Other
,
content:
content
)
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录