Class: BEL::Nanopub::Metadata

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/bel/nanopub/metadata.rb

Constant Summary collapse

BEL_VERSION =
:bel_version
DOCUMENT_HEADER =
:document_header

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ Metadata

Returns a new instance of Metadata.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bel/nanopub/metadata.rb', line 15

def initialize(values = {})
  if values.is_a? Array
    @values = Hash[
      values.map { |item|
        name  = item[:name]  || item['name']
        value = item[:value] || item['value']
        [name.to_sym, value]
      }
    ]
  else
    @values = values
  end

  doc_hdr = @values[:document_header]
  unless doc_hdr.nil?
    @values[:document_header] = Hash[
      doc_hdr.map { |item|
        [item[0].to_sym, item[1]]
      }
    ]
  end

  unless @values.key?(:bel_version)
    @values[:bel_version] = BELParser::Language.default_version
  end

  header = @values[:document_header]
  unless header.nil?
    authors = header[:Authors]
    unless authors.is_a? Array
      authors = [authors]
      header[:Authors] = authors
    end

    licenses = header[:Licenses]
    unless licenses.is_a? Array
      licenses = [licenses]
      header[:Licenses] = licenses
    end
  end
end

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



7
8
9
# File 'lib/bel/nanopub/metadata.rb', line 7

def values
  @values
end

Instance Method Details

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



83
84
85
86
# File 'lib/bel/nanopub/metadata.rb', line 83

def ==(other)
  return false if other.nil?
  @values == other.values
end

#bel_versionObject



57
58
59
# File 'lib/bel/nanopub/metadata.rb', line 57

def bel_version
  @values[BEL_VERSION]
end

#bel_version=(bel_version) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bel/nanopub/metadata.rb', line 61

def bel_version=(bel_version)
  @values[BEL_VERSION] =
    case bel_version
    when BELParser::Language::Specification
      bel_version.version.to_s
    when String
      bel_version
    else
      raise(
        ArgumentError,
        %(expected String, Specification; actual #{bel_version.class}))
    end
end

#document_headerObject



75
76
77
# File 'lib/bel/nanopub/metadata.rb', line 75

def document_header
  @values[DOCUMENT_HEADER] ||= {}
end

#document_header=(document_header) ⇒ Object



79
80
81
# File 'lib/bel/nanopub/metadata.rb', line 79

def document_header=(document_header)
  @values[DOCUMENT_HEADER] = document_header
end

#to_aObject



89
90
91
92
93
94
95
96
# File 'lib/bel/nanopub/metadata.rb', line 89

def to_a
  @values.each_pair.map { |key, value|
    {
      name:  key,
      value: value
    }
  }
end