Class: BEL::Nanopub::References

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

Constant Summary collapse

ANNOTATIONS =
:annotations
NAMESPACES =
:namespaces

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ References

Returns a new instance of References.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bel/nanopub/references.rb', line 17

def initialize(values = {})
  @values = {}

  values.fetch(ANNOTATIONS, []).each do |annotation|
    add_annotation(
      annotation[:keyword],
      annotation[:type],
      annotation[:domain]
    )
  end

  values.fetch(NAMESPACES, []).each do |namespace|
    add_namespace(
      namespace[:keyword],
      namespace[:type],
      namespace[:domain]
    )
  end
end

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



9
10
11
# File 'lib/bel/nanopub/references.rb', line 9

def values
  @values
end

Instance Method Details

#add_annotation(keyword, type, domain) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/bel/nanopub/references.rb', line 61

def add_annotation(keyword, type, domain)
  annotation =
    BELParser::Expression::Model::Annotation.new(
      keyword,
      type.to_sym,
      domain)
  annotations << annotation
  annotations.sort_by! { |a| a.keyword }
end

#add_namespace(keyword, type, domain) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bel/nanopub/references.rb', line 71

def add_namespace(keyword, type, domain)
  case type
  when :uri
    uri = domain
    url = nil
  when :url
    url = domain
    uri = nil
  end

  namespace =
    BELParser::Expression::Model::Namespace.new(
      keyword.to_s,
      uri,
      url)
  namespaces << namespace
  namespaces.sort_by! { |n| n.keyword }
end

#annotationsObject



37
38
39
# File 'lib/bel/nanopub/references.rb', line 37

def annotations
  @values[ANNOTATIONS] ||= []
end

#annotations=(annotations) ⇒ Object



45
46
47
# File 'lib/bel/nanopub/references.rb', line 45

def annotations=(annotations)
  @values[ANNOTATIONS] = annotations
end

#annotations_hashObject



41
42
43
# File 'lib/bel/nanopub/references.rb', line 41

def annotations_hash
  Hash[annotations.map { |ns| ns.values_at(:keyword, :domain) }]
end

#namespacesObject



49
50
51
# File 'lib/bel/nanopub/references.rb', line 49

def namespaces
  @values[NAMESPACES] ||= []
end

#namespaces=(namespaces) ⇒ Object



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

def namespaces=(namespaces)
  @values[NAMESPACES] = namespaces
end

#namespaces_hashObject



53
54
55
# File 'lib/bel/nanopub/references.rb', line 53

def namespaces_hash
  Hash[ namespaces.map { |n| [n.keyword, n] } ]
end

#to_h(hash = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bel/nanopub/references.rb', line 90

def to_h(hash = {})
  hash[ANNOTATIONS] = annotations.map { |anno|
    {
      :keyword => anno.keyword,
      :type    => anno.type,
      :domain  =>
        case anno.domain
        when Regexp
          anno.domain.source
        else
          anno.domain
        end
    }
  }

  hash[NAMESPACES] = namespaces.map { |ns|
    {
      :keyword => ns.keyword,
      :type    => ns.type,
      :domain  => ns.domain
    }
  }

  hash
end