Class: RelatonBib::BibItemLocality

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton_bib/bib_item_locality.rb

Overview

Bibliographic item locality.

Direct Known Subclasses

Locality, SourceLocality

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, reference_from, reference_to = nil) ⇒ BibItemLocality

Returns a new instance of BibItemLocality.

Parameters:

  • type (String)
  • referenceFrom (String)
  • referenceTo (String, nil)


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/relaton_bib/bib_item_locality.rb', line 16

def initialize(type, reference_from, reference_to = nil)
  type_ptrn = %r{section|clause|part|paragraph|chapter|page|title|line|
    whole|table|annex|figure|note|list|example|volume|issue|time|anchor|
    locality:[a-zA-Z0-9_]+}x
  unless type&.match? type_ptrn
    Util.warn "Invalid locality type: `#{type}`"
  end

  @type           = type
  @reference_from = reference_from
  @reference_to   = reference_to
end

Instance Attribute Details

#reference_fromString (readonly)

Returns:

  • (String)


8
9
10
# File 'lib/relaton_bib/bib_item_locality.rb', line 8

def reference_from
  @reference_from
end

#reference_toString? (readonly)

Returns:

  • (String, nil)


11
12
13
# File 'lib/relaton_bib/bib_item_locality.rb', line 11

def reference_to
  @reference_to
end

#typeString (readonly)

Returns:

  • (String)


5
6
7
# File 'lib/relaton_bib/bib_item_locality.rb', line 5

def type
  @type
end

Instance Method Details

#to_asciibib(prefix = "", count = 1) ⇒ String

Parameters:

  • prefix (String) (defaults to: "")
  • count (Integeg) (defaults to: 1)

    number of localities

Returns:

  • (String)


46
47
48
49
50
51
52
53
# File 'lib/relaton_bib/bib_item_locality.rb', line 46

def to_asciibib(prefix = "", count = 1)
  pref = prefix.empty? ? prefix : "#{prefix}."
  out = count > 1 ? "#{prefix}::\n" : ""
  out += "#{pref}type:: #{type}\n"
  out += "#{pref}reference_from:: #{reference_from}\n"
  out += "#{pref}reference_to:: #{reference_to}\n" if reference_to
  out
end

#to_bibtex(item) ⇒ Object

Render locality as BibTeX.

Parameters:

  • item (BibTeX::Entry)

    BibTeX entry.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/relaton_bib/bib_item_locality.rb', line 60

def to_bibtex(item)
  case type
  when "chapter" then item.chapter = reference_from
  when "page"
    value = reference_from
    value += "--#{reference_to}" if reference_to
    item.pages = value
  when "volume" then item.volume = reference_from
  when "issue" then item.number = reference_from
  end
end

#to_hashHash

Returns:

  • (Hash)


37
38
39
40
41
# File 'lib/relaton_bib/bib_item_locality.rb', line 37

def to_hash
  hash = { "type" => type, "reference_from" => reference_from }
  hash["reference_to"] = reference_to if reference_to
  hash
end

#to_xml(builder) ⇒ Object

Parameters:

  • builder (Nokogiri::XML::Builder)


30
31
32
33
34
# File 'lib/relaton_bib/bib_item_locality.rb', line 30

def to_xml(builder)
  builder.parent[:type] = type
  builder.referenceFrom reference_from # { reference_from.to_xml(builder) }
  builder.referenceTo reference_to if reference_to
end