class CFF::Model
Model
is the core data structure for a CITATION.cff file. It can be accessed direcly, or via File
.
Model
implements all of the fields listed in the [CFF standard](citation-file-format.github.io/). Complex fields - `authors`, `contact`, `identifiers`, `keywords`, `preferred-citation` and `references` - are documented below. All other fields are simple strings and can be set as such. A field which has not been set will return the empty string. The simple fields are (with defaults in parentheses):
-
`abstract`
-
`cff_version`
-
`commit`
-
`date_released` - Note: returns a `Date` object
-
`doi`
-
`license`
-
`license_url`
-
`message` (If you use this software in your work, please cite it using
the following metadata)
-
`repository`
-
`repository_artifact`
-
`repository_code`
-
`title`
-
`url`
-
`version`
Constants
- DEFAULT_MESSAGE
The default message to use if none is explicitly set.
Public Class Methods
Initialize a new Model
with the supplied title.
# File lib/cff/model.rb, line 68 def initialize(param) if param.is_a?(Hash) @fields = build_model(param) @fields.default = '' else @fields = Hash.new('') @fields['cff-version'] = DEFAULT_SPEC_VERSION @fields['message'] = DEFAULT_MESSAGE @fields['title'] = param end %w[authors contact identifiers keywords references].each do |field| @fields[field] = [] if @fields[field].empty? end yield self if block_given? end
With no associated block, Model.open
is a synonym for ::read
. If the optional code block is given, it will be passed the parsed model as an argument and the Model
will be returned when the block terminates.
# File lib/cff/model.rb, line 101 def self.open(model) cff = Model.read(model) yield cff if block_given? cff end
Public Instance Methods
Set the `date-released` field. If a non-Date object is passed in it will be parsed into a Date.
# File lib/cff/model.rb, line 114 def date_released=(date) date = Date.parse(date) unless date.is_a?(Date) @fields['date-released'] = date end
Return the list of identifiers for this citation. To add a identifier to the list, use:
“` model.identifiers << identifier “`
# File lib/cff/model.rb, line 232
Replace the list of identifiers for this citation.
# File lib/cff/model.rb, line 244
Return the list of keywords for this citation. To add a keyword to the list, use:
“` model.keywords << keyword “`
Keywords will be converted to Strings on output.
# File lib/cff/model.rb, line 251
Replace the list of keywords for this citation.
Keywords will be converted to Strings on output.
# File lib/cff/model.rb, line 265
Return the preferred citation for this citation.
# File lib/cff/model.rb, line 274
Replace the preferred citation for this citation.
# File lib/cff/model.rb, line 281
Return the list of references for this citation. To add a reference to the list, use:
“` model.references << reference “`
# File lib/cff/model.rb, line 288
Replace the list of references for this citation.
# File lib/cff/model.rb, line 300
Output this Model
in an APA-like format. Setting `preferred_citation: true` will honour the `preferred_citation` field in the model if one is present (default).
Note: This method assumes that this Model
is valid when called.
# File lib/cff/model.rb, line 132 def to_apalike(preferred_citation: true) CFF::ApaFormatter.format( model: self, preferred_citation: preferred_citation ) end
Output this Model
in BibTeX format. Setting `preferred_citation: true` will honour the `preferred_citation` field in the model if one is present (default).
Note: This method assumes that this Model
is valid when called.
# File lib/cff/model.rb, line 146 def to_bibtex(preferred_citation: true) CFF::BibtexFormatter.format( model: self, preferred_citation: preferred_citation ) end
Private Instance Methods
# File lib/cff/model.rb, line 162 def build_model(fields) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity build_actor_collection!(fields['authors'] || []) build_actor_collection!(fields['contact'] || []) (fields['identifiers'] || []).map! do |i| Identifier.new(i) end (fields['references'] || []).map! do |r| Reference.new(r) end fields['preferred-citation'] &&= Reference.new(fields['preferred-citation']) # Only attempt an update of the `cff-version` field if it is present. fields['cff-version'] &&= update_cff_version(fields['cff-version']) fields end
# File lib/cff/model.rb, line 154 def fields %w[authors contact identifiers references].each do |field| normalize_modelpart_array!(@fields[field]) end fields_to_hash(@fields) end