Class: BEL::Nanopub::Citation

Inherits:
Object
  • Object
show all
Defined in:
lib/bel/nanopub/citation.rb

Overview

A Citation describes a cited reference which Nanopub is derived from.

Constant Summary collapse

MEMBER_ORDER =

The ordering of citation attributes to allow processing of sequential input of citation fields.

The ordered attributes are:

%i(type name id date authors comment)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Citation

Creates a BEL::Nanopub::Citation object from variable arguments.

Examples:

Create an empty BEL::Nanopub::Citation.

Citation.new

Create a minimal BEL::Nanopub::Citation by positional members.

Citation.new('PubMed', 'Biocompatibility study of...', '12102192')

Create a minimal BEL::Nanopub::Citation by positional members in array.

Citation.new(
  ['PubMed', 'Biocompatibility study of...', '12102192']
)

Create a full BEL::Nanopub::Citation by hash using keys for members.

Citation.new(
  {
    :type    => 'PubMed',
    :name    => 'Biocompatibility study of biological tissues fixed
                 by a natural compound (reuterin) produced by
                 Lactobacillus reuteri.',
    :id      => '12102192',
    :date    => '2002-08-23',
    :authors => 'Sung HW|Chen CN|Chang Y|Liang HF',
    :comment => 'This article is primary research.'
  }
)

Parameters:

  • args (Array<Object>)

    Argument array of Object; If the array contains a single value then it is expected to either respond to :each (according to #MEMBER_ORDER) or :each_pair (keys matching that of #MEMBER_ORDER). If the array has multiple values then they are set sequentially according to #MEMBER_ORDER.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bel/nanopub/citation.rb', line 53

def initialize(*args)
  return if !args || args.empty?

  if args.length == 1
    enumerable = args.first
    if enumerable.respond_to? :each_pair
      enumerable.keys.each do |key|
        enumerable[key.to_s.to_sym] = enumerable.delete(key)
      end
      (MEMBER_ORDER & enumerable.keys).each do |member|
        self.send(:"#{member}=", enumerable[member])
      end
    elsif enumerable.respond_to? :each
      MEMBER_ORDER.zip(enumerable.each).each do |member, value|
        self.send(:"#{member}=", value)
      end
    end
  else
    (MEMBER_ORDER & args).each do |member|
      self.send(:"#{member}=", hash[member])
    end
  end
end

Instance Attribute Details

#authorsArray<String>

Returns the Array of String authors.

Returns:

  • (Array<String>)

    array of authors



14
15
16
# File 'lib/bel/nanopub/citation.rb', line 14

def authors
  @authors
end

#commentString

an additional comment related to this resource

Returns:

  • (String)

    the current value of comment



14
15
16
# File 'lib/bel/nanopub/citation.rb', line 14

def comment
  @comment
end

#dateString

the date the resource was published

Returns:

  • (String)

    the current value of date



14
15
16
# File 'lib/bel/nanopub/citation.rb', line 14

def date
  @date
end

#idString

the unique identifier for the cited resource. For example, a “PubMed” type should have a PMID (i.e. 12102192).

Returns:

  • (String)

    the current value of id



14
15
16
# File 'lib/bel/nanopub/citation.rb', line 14

def id
  @id
end

#nameString

the name of the cited resource

Returns:

  • (String)

    the current value of name



14
15
16
# File 'lib/bel/nanopub/citation.rb', line 14

def name
  @name
end

#typeString

the type of resource the cited material appears in (e.g. PubMed, Journal, Book, Online Resource, etc…)

Returns:

  • (String)

    the current value of type



14
15
16
# File 'lib/bel/nanopub/citation.rb', line 14

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



103
104
105
106
107
108
109
# File 'lib/bel/nanopub/citation.rb', line 103

def ==(other)
  return false if other == nil

  id == other.id && type == other.type &&
    name == other.name && authors == other.authors &&
    date == other.date && comment == other.comment
end

#hashObject



99
100
101
# File 'lib/bel/nanopub/citation.rb', line 99

def hash
  [id, type, name, authors, date, comment].hash
end

#to_aObject



122
123
124
125
126
127
# File 'lib/bel/nanopub/citation.rb', line 122

def to_a
  MEMBER_ORDER.reduce([]) { |array, member|
    array << self.send(member)
    array
  }
end

#to_hHash

Returns a Hash of the citation members accoring to #MEMBER_ORDER.

Returns:

  • (Hash)

    citation Hash



115
116
117
118
119
120
# File 'lib/bel/nanopub/citation.rb', line 115

def to_h
  MEMBER_ORDER.reduce({}) { |hash, member|
    hash[member] = self.send(member)
    hash
  }
end

#valid?true, false

Returns whether the citation has enough pertinent information to be considered valid.

Returns:

  • (true, false)

    whether the citation is valid



95
96
97
# File 'lib/bel/nanopub/citation.rb', line 95

def valid?
  type != nil && id != nil && name != nil
end