Class: Jats::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/jats/node.rb

Direct Known Subclasses

Affiliation, Annotation, Figure, Heading, Person, Text

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml = nil) ⇒ Node

Returns a new instance of Node.



19
20
21
22
# File 'lib/jats/node.rb', line 19

def initialize(xml=nil)
  @xml = xml
  @index = self.class.index_for(type)
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



17
18
19
# File 'lib/jats/node.rb', line 17

def index
  @index
end

#xmlObject (readonly)

Returns the value of attribute xml.



17
18
19
# File 'lib/jats/node.rb', line 17

def xml
  @xml
end

Class Method Details

.from_xml(xml) ⇒ Object



3
4
5
# File 'lib/jats/node.rb', line 3

def self.from_xml(xml)
  new(xml)
end

.index_for(type) ⇒ Object



11
12
13
14
15
# File 'lib/jats/node.rb', line 11

def self.index_for(type)
  @@_indices ||= {}
  @@_indices[type] ||= 0
  @@_indices[type] += 1
end

.reset_indicesObject



7
8
9
# File 'lib/jats/node.rb', line 7

def self.reset_indices
  @@_indices = {}
end

Instance Method Details

#attributesObject



33
34
35
# File 'lib/jats/node.rb', line 33

def attributes
  {}
end

#nameObject Also known as: id



28
29
30
# File 'lib/jats/node.rb', line 28

def name
  "#{type}:#{xml['id'] || index}"
end

#to_hashObject



37
38
39
40
41
42
43
44
# File 'lib/jats/node.rb', line 37

def to_hash
  {
    name => {
      type: type,
      id:   id,
    }.merge(attributes)
  }
end

#typeObject



24
25
26
# File 'lib/jats/node.rb', line 24

def type
  self.class.name.split('::').last.downcase
end