cff 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8b6e59d2c810ff7f55fac883fa8febd7bda2928c
4
- data.tar.gz: 3e93472087c65a0bf7d57ef038e60ffd48438647
3
+ metadata.gz: ba4e9410b991789174bfa5cff1b448600aceb46a
4
+ data.tar.gz: ac13ec2b66c63edeaa5eb721b88fec6a4b7fd0da
5
5
  SHA512:
6
- metadata.gz: 7e9af9c7e01db4ffda0371ce140e7c8ed338c2d419cf44d786b093e54e479b1e8dc74d17e7581e98a9298ac10a16c559d802788d764a4ce9792f798c253ad684
7
- data.tar.gz: 59aaa4928118dfb01fd7617c7b6722bb0a7db3274af945926391cd82325de025ca1a88545972621f08368d4b9765642aecd39d1f8a5f2f3862e92aac2cea587d
6
+ metadata.gz: 807b056534179b4ae578259ef0c8fcc0a6a07d2782b4b134f9a790e99a682a82c9c632008ac8e76afbdd238b060f13c583b30bdb8b1c30c30a6c471230bf9661
7
+ data.tar.gz: 2caf814fc16b88fc4d9d70ce664d9a233e258c4460cce6547cca02fe424a5ee4d8d0fcc0bc2309045477901130f0cad97f5fbfce9aaba3b0067ecc4113c1a1b8
@@ -5,6 +5,7 @@ sudo: false
5
5
  cache: bundler
6
6
 
7
7
  rvm:
8
+ - 2.2.9
8
9
  - 2.3.6
9
10
  - 2.4.3
10
11
  - 2.5.0
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  A Ruby library for manipulating CITATION.cff files.
5
5
 
6
+ [![Gem Version](https://badge.fury.io/rb/cff.svg)](https://badge.fury.io/rb/cff)
6
7
  [![Build Status](https://travis-ci.org/hainesr/ruby-cff.svg?branch=master)](https://travis-ci.org/hainesr/ruby-cff)
7
8
  [![Maintainability](https://api.codeclimate.com/v1/badges/7eaa3890f17664e10bc6/maintainability)](https://codeclimate.com/github/hainesr/ruby-cff/maintainability)
8
9
  [![Coverage Status](https://coveralls.io/repos/github/hainesr/ruby-cff/badge.svg)](https://coveralls.io/github/hainesr/ruby-cff)
@@ -17,9 +18,12 @@ See the [CITATION.cff documentation](https://citation-file-format.github.io/) fo
17
18
 
18
19
  ```ruby
19
20
  cff = CFF::Model.new("Ruby CFF Library")
20
- cff.version = 0.1.0
21
+ cff.version = "0.1.0"
21
22
  cff.date_released = Date.today
22
23
  cff.authors << CFF::Person.new("Robert", "Haines")
24
+ cff.license = "Apache-2.0"
25
+ cff.keywords << "ruby" << "credit" << "citation"
26
+ cff.repository_artifact = "https://rubygems.org/gems/cff"
23
27
 
24
28
  CFF::File.write("CITATION.cff", cff)
25
29
  ```
@@ -31,10 +35,16 @@ cff-version: 1.0.3
31
35
  message: If you use this software in your work, please cite it using the following metadata
32
36
  title: Ruby CFF Library
33
37
  version: 0.1.0
34
- date-released: 2018-02-18
38
+ date-released: 2018-02-24
39
+ license: Apache-2.0
40
+ repository-artifact: https://rubygems.org/gems/cff
35
41
  authors:
36
42
  - family-names: Haines
37
43
  given-names: Robert
44
+ keywords:
45
+ - ruby
46
+ - credit
47
+ - citation
38
48
  ```
39
49
 
40
50
  ### Licence
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.summary = "A Ruby library for manipulating CITATION.cff files."
26
26
  spec.description = "See https://citation-file-format.github.io/ for more info."
27
27
  spec.homepage = "https://github.com/hainesr/ruby-cff"
28
+ spec.license = "Apache-2.0"
28
29
 
29
30
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
30
31
  f.match(%r{^(test|spec|features)/})
@@ -33,6 +34,8 @@ Gem::Specification.new do |spec|
33
34
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
35
  spec.require_paths = ["lib"]
35
36
 
37
+ spec.required_ruby_version = ">= 2.2.0"
38
+
36
39
  spec.add_development_dependency "bundler", "~> 1.16"
37
40
  spec.add_development_dependency "rake", "~> 10.0"
38
41
  spec.add_development_dependency "minitest", "~> 5.0"
data/lib/cff.rb CHANGED
@@ -16,6 +16,8 @@ require "date"
16
16
  require "yaml"
17
17
 
18
18
  require "cff/version"
19
+ require "cff/util"
20
+ require "cff/model-part"
19
21
  require "cff/person"
20
22
  require "cff/entity"
21
23
  require "cff/model"
@@ -18,17 +18,62 @@ module CFF
18
18
  # An Entity can represent different types of entities, e.g., a publishing
19
19
  # company, or conference. Like a Person, an Entity might have a number of
20
20
  # roles, such as author, contact, editor, etc.
21
- class Entity
21
+ class Entity < ModelPart
22
22
 
23
- attr_reader :fields # :nodoc:
23
+ ALLOWED_FIELDS = [
24
+ 'address',
25
+ 'city',
26
+ 'country',
27
+ 'email',
28
+ 'date-end',
29
+ 'date-start',
30
+ 'fax',
31
+ 'location',
32
+ 'name',
33
+ 'orcid',
34
+ 'post-code',
35
+ 'region',
36
+ 'tel',
37
+ 'website'
38
+ ].freeze # :nodoc:
24
39
 
25
40
  # :call-seq:
26
41
  # new(name) -> Entity
27
42
  #
28
43
  # Create a new Entity with the supplied name.
29
- def initialize(name)
30
- @fields = Hash.new('')
31
- @fields['name'] = name
44
+ def initialize(param)
45
+ if Hash === param
46
+ super(param)
47
+ else
48
+ @fields = Hash.new('')
49
+ @fields['name'] = param
50
+ end
51
+ end
52
+
53
+ # :call-seq:
54
+ # date_end = date
55
+ #
56
+ # Set the `date-end` field. If a non-Date object is passed in it will
57
+ # be parsed into a Date.
58
+ def date_end=(date)
59
+ unless Date === date
60
+ date = Date.parse(date)
61
+ end
62
+
63
+ @fields['date-end'] = date
64
+ end
65
+
66
+ # :call-seq:
67
+ # date_start = date
68
+ #
69
+ # Set the `date-start` field. If a non-Date object is passed in it will
70
+ # be parsed into a Date.
71
+ def date_start=(date)
72
+ unless Date === date
73
+ date = Date.parse(date)
74
+ end
75
+
76
+ @fields['date-start'] = date
32
77
  end
33
78
 
34
79
  end
@@ -60,9 +60,7 @@ module CFF
60
60
  end
61
61
 
62
62
  def method_missing(name, *args) # :nodoc:
63
- super unless Model::ALLOWED_METHODS.include?(name)
64
-
65
- @model.send name, args
63
+ @model.send name, *args
66
64
  end
67
65
 
68
66
  end
@@ -0,0 +1,41 @@
1
+ # Copyright (c) 2018 Robert Haines.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ #
16
+ module CFF
17
+
18
+ # :stopdoc:
19
+ class ModelPart
20
+ include Util
21
+
22
+ attr_reader :fields
23
+
24
+ def initialize(fields)
25
+ @fields = fields
26
+ end
27
+
28
+ def method_missing(name, *args) # :nodoc:
29
+ n = method_to_field(name.id2name)
30
+ super unless self.class::ALLOWED_FIELDS.include?(n.chomp('='))
31
+
32
+ if n.end_with?('=')
33
+ @fields[n.chomp('=')] = args[0] || ''
34
+ else
35
+ @fields[n]
36
+ end
37
+ end
38
+
39
+ end
40
+ # :startdoc:
41
+ end
@@ -19,15 +19,24 @@ module CFF
19
19
  # accessed direcly, or via File.
20
20
  class Model
21
21
 
22
- ALLOWED_METHODS = [
23
- :cff_version,
24
- :date_released,
25
- :message,
26
- :message=,
27
- :title,
28
- :title=,
29
- :version
30
- ] # :nodoc:
22
+ include Util
23
+
24
+ ALLOWED_FIELDS = [
25
+ 'abstract',
26
+ 'cff-version',
27
+ 'commit',
28
+ 'date-released',
29
+ 'doi',
30
+ 'license',
31
+ 'license-url',
32
+ 'message',
33
+ 'repository',
34
+ 'repository-artifact',
35
+ 'repository-code',
36
+ 'title',
37
+ 'url',
38
+ 'version'
39
+ ].freeze # :nodoc:
31
40
 
32
41
  # The default message to use if none is explicitly set.
33
42
  DEFAULT_MESSAGE = "If you use this software in your work, please cite it using the following metadata"
@@ -37,16 +46,18 @@ module CFF
37
46
  #
38
47
  # Initialize a new Model with the supplied title.
39
48
  def initialize(param)
49
+ @authors = []
50
+ @contact = []
51
+ @keywords = []
52
+
40
53
  if Hash === param
41
- @fields = param
54
+ build_model(param)
42
55
  else
43
56
  @fields = Hash.new('')
44
57
  @fields['cff-version'] = DEFAULT_SPEC_VERSION
45
58
  @fields['message'] = DEFAULT_MESSAGE
46
59
  @fields['title'] = param
47
60
  end
48
-
49
- @authors = []
50
61
  end
51
62
 
52
63
  # :call-seq:
@@ -64,6 +75,21 @@ module CFF
64
75
  @authors
65
76
  end
66
77
 
78
+ # :call-seq:
79
+ # contact -> Array
80
+ #
81
+ # Return the list of contacts for this citation. To add a contact to the
82
+ # list, use:
83
+ #
84
+ # ```
85
+ # model.contact << contact
86
+ # ```
87
+ #
88
+ # Contacts can be a Person or Entity.
89
+ def contact
90
+ @contact
91
+ end
92
+
67
93
  # :call-seq:
68
94
  # date_released = date
69
95
  #
@@ -77,6 +103,21 @@ module CFF
77
103
  @fields['date-released'] = date
78
104
  end
79
105
 
106
+ # :call-seq:
107
+ # keywords -> Array
108
+ #
109
+ # Return the list of keywords for this citation. To add a keyword to the
110
+ # list, use:
111
+ #
112
+ # ```
113
+ # model.keywords << keyword
114
+ # ```
115
+ #
116
+ # Keywords will be converted to Strings on output to a CFF file.
117
+ def keywords
118
+ @keywords
119
+ end
120
+
80
121
  # :call-seq:
81
122
  # version = version
82
123
  #
@@ -87,17 +128,17 @@ module CFF
87
128
 
88
129
  def to_yaml # :nodoc:
89
130
  fields = @fields.dup
90
- fields['authors'] = @authors.reject do |a|
91
- !a.respond_to?(:fields)
92
- end.map { |a| a.fields }
131
+ fields['authors'] = array_field_to_yaml(@authors) unless @authors.empty?
132
+ fields['contact'] = array_field_to_yaml(@contact) unless @contact.empty?
133
+ fields['keywords'] = @keywords.map { |k| k.to_s } unless @keywords.empty?
93
134
 
94
135
  YAML.dump fields, :line_width => -1, :indentation => 2
95
136
  end
96
137
 
97
138
  def method_missing(name, *args) # :nodoc:
98
- super unless ALLOWED_METHODS.include?(name)
99
-
100
139
  n = method_to_field(name.id2name)
140
+ super unless ALLOWED_FIELDS.include?(n.chomp('='))
141
+
101
142
  if n.end_with?('=')
102
143
  @fields[n.chomp('=')] = args[0] || ''
103
144
  else
@@ -107,8 +148,24 @@ module CFF
107
148
 
108
149
  private
109
150
 
110
- def method_to_field(name)
111
- name.gsub('_', '-')
151
+ def build_model(fields)
152
+ build_entity_collection(@authors, fields['authors'])
153
+ build_entity_collection(@contact, fields['contact'])
154
+ @keywords = fields['keywords']
155
+
156
+ @fields = delete_from_hash(fields, 'authors', 'contact', 'keywords')
157
+ end
158
+
159
+ def build_entity_collection(field, source)
160
+ source.each do |s|
161
+ field << (s.has_key?('given-names') ? Person.new(s) : Entity.new(s))
162
+ end
163
+ end
164
+
165
+ def array_field_to_yaml(field)
166
+ field.reject do |f|
167
+ !f.respond_to?(:fields)
168
+ end.map { |f| f.fields }
112
169
  end
113
170
 
114
171
  end
@@ -17,18 +17,38 @@ module CFF
17
17
 
18
18
  # A Person represents a person in a CITATION.cff file. A Person might have a
19
19
  # number of roles, such as author, contact, editor, etc.
20
- class Person
20
+ class Person < ModelPart
21
21
 
22
- attr_reader :fields # :nodoc:
22
+ ALLOWED_FIELDS = [
23
+ 'address',
24
+ 'affiliation',
25
+ 'city',
26
+ 'country',
27
+ 'email',
28
+ 'family-names',
29
+ 'fax',
30
+ 'given-names',
31
+ 'name-particle',
32
+ 'name-suffix',
33
+ 'orcid',
34
+ 'post-code',
35
+ 'region',
36
+ 'tel',
37
+ 'website'
38
+ ].freeze # :nodoc:
23
39
 
24
40
  # :call-seq:
25
41
  # new(given_name, family_name) -> Person
26
42
  #
27
43
  # Create a new Person with the supplied given and family names.
28
- def initialize(given, family)
29
- @fields = Hash.new('')
30
- @fields['family-names'] = family
31
- @fields['given-names'] = given
44
+ def initialize(param, *more)
45
+ if Hash === param
46
+ super(param)
47
+ else
48
+ @fields = Hash.new('')
49
+ @fields['family-names'] = more[0]
50
+ @fields['given-names'] = param
51
+ end
32
52
  end
33
53
 
34
54
  end
@@ -0,0 +1,41 @@
1
+ # Copyright (c) 2018 Robert Haines.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ #
16
+ module CFF
17
+
18
+ # Utility methods useful throughout the rest of the CFF library.
19
+ module Util
20
+
21
+ # :call-seq:
22
+ # delete_from_hash(hash, keys...) -> Hash
23
+ #
24
+ # Returns a hash that includes everything but the given keys.
25
+ def delete_from_hash(hash, *keys)
26
+ h = hash.dup
27
+ keys.each { |key| h.delete(key) }
28
+ h
29
+ end
30
+
31
+ # :call-seq:
32
+ # method_to_field(name) -> converted name
33
+ #
34
+ # Return the supplied name with underscores converted to dashes.
35
+ def method_to_field(name)
36
+ name.gsub('_', '-')
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -15,6 +15,6 @@
15
15
  #
16
16
  module CFF
17
17
  # :nodoc:
18
- VERSION = "0.1.0"
18
+ VERSION = "0.2.0"
19
19
  DEFAULT_SPEC_VERSION = "1.0.3"
20
20
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Haines
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-18 00:00:00.000000000 Z
11
+ date: 2018-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -116,11 +116,14 @@ files:
116
116
  - lib/cff.rb
117
117
  - lib/cff/entity.rb
118
118
  - lib/cff/file.rb
119
+ - lib/cff/model-part.rb
119
120
  - lib/cff/model.rb
120
121
  - lib/cff/person.rb
122
+ - lib/cff/util.rb
121
123
  - lib/cff/version.rb
122
124
  homepage: https://github.com/hainesr/ruby-cff
123
- licenses: []
125
+ licenses:
126
+ - Apache-2.0
124
127
  metadata: {}
125
128
  post_install_message:
126
129
  rdoc_options: []
@@ -130,7 +133,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
130
133
  requirements:
131
134
  - - ">="
132
135
  - !ruby/object:Gem::Version
133
- version: '0'
136
+ version: 2.2.0
134
137
  required_rubygems_version: !ruby/object:Gem::Requirement
135
138
  requirements:
136
139
  - - ">="