Class: Docbook::Mirror::Mark

Inherits:
Object
  • Object
show all
Defined in:
lib/docbook/mirror/mark.rb

Direct Known Subclasses

Base

Defined Under Namespace

Classes: Base, Citation, Code, Emphasis, Italic, Link, Strong, Xref

Constant Summary collapse

PM_TYPE =
"mark"
MARKS =
Hash.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type: nil, attrs: {}) ⇒ Mark

Returns a new instance of Mark.



10
11
12
13
# File 'lib/docbook/mirror/mark.rb', line 10

def initialize(type: nil, attrs: {})
  @type = type || self.class::PM_TYPE
  @attrs = attrs || {}
end

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs.



8
9
10
# File 'lib/docbook/mirror/mark.rb', line 8

def attrs
  @attrs
end

#typeObject

Returns the value of attribute type.



8
9
10
# File 'lib/docbook/mirror/mark.rb', line 8

def type
  @type
end

Class Method Details

.from_h(hash) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/docbook/mirror/mark.rb', line 27

def self.from_h(hash)
  return nil unless hash

  type = hash["type"]
  attrs = hash["attrs"] || {}

  mark_class = MARKS[type] || Mark

  # Use class-specific from_h if this class defines its own class method (not inherited)
  if mark_class != Mark && mark_class.singleton_class.method_defined?(
    :from_h, false
  )
    mark_class.from_h(hash)
  else
    mark_class.new(
      attrs: attrs.transform_keys(&:to_sym),
    )
  end
end

Instance Method Details

#to_hObject Also known as: to_hash



15
16
17
18
19
# File 'lib/docbook/mirror/mark.rb', line 15

def to_h
  result = { "type" => type }
  result["attrs"] = attrs.transform_keys(&:to_s) if attrs && !attrs.empty?
  result
end

#to_json(**options) ⇒ Object



23
24
25
# File 'lib/docbook/mirror/mark.rb', line 23

def to_json(**options)
  to_h.to_json(options)
end