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):

the following metadata)

Constants

DEFAULT_MESSAGE

The default message to use if none is explicitly set.

Public Class Methods

new(title) → Model click to toggle source
new(title) { |model| block } → Model

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
open(String) → Model click to toggle source
open(String) { |cff| block } → Model

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
read(String) → Model click to toggle source

Read a CFF Model from a String and parse it for subsequent manipulation.

# File lib/cff/model.rb, line 90
def self.read(model)
  new(YAML.safe_load(model, permitted_classes: [Date, Time]))
end

Public Instance Methods

authors → Array click to toggle source

Return the list of authors for this citation. To add an author to the list, use:

“` model.authors << author “`

Authors can be a Person or Entity.

# File lib/cff/model.rb, line 186
    
authors = array_of_authors → Array click to toggle source

Replace the list of authors for this citation.

Authors can be a Person or Entity.

# File lib/cff/model.rb, line 200
    
contact → Array click to toggle source

Return the list of contacts for this citation. To add a contact to the list, use:

“` model.contact << contact “`

Contacts can be a Person or Entity.

# File lib/cff/model.rb, line 209
    
contact = array_of_contacts → Array click to toggle source

Replace the list of contacts for this citation.

Contacts can be a Person or Entity.

# File lib/cff/model.rb, line 223
    
date_released = date click to toggle source

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
identifiers → Array click to toggle source

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
    
identifiers = array_of_identifiers → Array click to toggle source

Replace the list of identifiers for this citation.

# File lib/cff/model.rb, line 244
    
keywords → Array click to toggle source

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
    
keywords = array_of_keywords → Array click to toggle source

Replace the list of keywords for this citation.

Keywords will be converted to Strings on output.

# File lib/cff/model.rb, line 265
    
preferred_citation → Reference click to toggle source

Return the preferred citation for this citation.

# File lib/cff/model.rb, line 274
    
preferred_citation = Reference click to toggle source

Replace the preferred citation for this citation.

# File lib/cff/model.rb, line 281
    
references → Array click to toggle source

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
    
references = array_of_references → Array click to toggle source

Replace the list of references for this citation.

# File lib/cff/model.rb, line 300
  
to_apalike(preferred_citation: true) → String click to toggle source

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
to_bibtex(preferred_citation: true) → String click to toggle source

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

build_model(fields) click to toggle source
# 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
fields() click to toggle source
# 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