Class: Relaton::Db

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton/db.rb

Constant Summary collapse

SUPPORTED_GEMS =
%w[
  relaton_iso relaton_ietf relaton_gb relaton_iec relaton_nist relaton_itu
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(global_cache, local_cache) ⇒ Db

Returns a new instance of Db.

Parameters:

  • global_cache (String)

    directory of global DB

  • local_cache (String)

    directory of local DB



15
16
17
18
19
20
21
22
# File 'lib/relaton/db.rb', line 15

def initialize(global_cache, local_cache)
  register_gems
  @registry = Relaton::Registry.instance
  @db = open_cache_biblio(global_cache)
  @local_db = open_cache_biblio(local_cache, global: false)
  @db_name = global_cache
  @local_db_name = local_cache
end

Instance Method Details

#docid_type(code) ⇒ Object

The document identifier class corresponding to the given code



66
67
68
69
70
# File 'lib/relaton/db.rb', line 66

def docid_type(code)
  stdclass = standard_class(code) or return [nil, code]
  _prefix, code = strip_id_wrapper(code, stdclass)
  [@registry.processors[stdclass].idtype, code]
end

#fetch(code, year = nil, opts = {}) ⇒ String

The class of reference requested is determined by the prefix of the code: GB Standard for gbbib, IETF for ietfbib, ISO for isobib, IEC or IEV for iecbib,

Parameters:

  • code (String)

    the ISO standard Code to look up (e.g. “ISO 9000”)

  • year (String) (defaults to: nil)

    the year the standard was published (optional)

  • opts (Hash) (defaults to: {})

    options; restricted to :all_parts if all-parts reference is required

Returns:

  • (String)

    Relaton XML serialisation of reference



42
43
44
45
# File 'lib/relaton/db.rb', line 42

def fetch(code, year = nil, opts = {})
  stdclass = standard_class(code) or return nil
  check_bibliocache(code, year, opts, stdclass)
end

#fetch_std(code, year = nil, stdclass = nil, opts = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/relaton/db.rb', line 47

def fetch_std(code, year = nil, stdclass = nil, opts = {})
  std = nil
  @registry.processors.each do |name, processor|
    std = name if processor.prefix == stdclass
  end
  unless std
    std = standard_class(code) or return nil
  end
  check_bibliocache(code, year, opts, std)
end

#load_entry(key) ⇒ Hash

Parameters:

  • key (String)

Returns:

  • (Hash)


74
75
76
77
78
79
80
# File 'lib/relaton/db.rb', line 74

def load_entry(key)
  unless @local_db.nil?
    entry = @local_db[key]
    return entry if entry
  end
  @db[key]
end

#register_gemsObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/relaton/db.rb', line 24

def register_gems
  puts "[relaton] Info: detecting backends:"
  SUPPORTED_GEMS.each do |b|
    # puts b
    begin
      require b
    rescue LoadError
      puts "[relaton] Error: backend #{b} not present"
    end
  end
end

#save_entry(key, value) ⇒ Object

Parameters:

  • key (String)
  • value (String)

    Bibitem xml serialisation.

Options Hash (value):

  • Bibitem (String)

    xml serialisation.



85
86
87
88
# File 'lib/relaton/db.rb', line 85

def save_entry(key, value)
  @db.nil? || (@db[key] = value)
  @local_db.nil? || (@local_db[key] = value)
end

#to_xmlString

list all entries as a serialization

Returns:

  • (String)


92
93
94
95
96
97
98
99
# File 'lib/relaton/db.rb', line 92

def to_xml
  db = @local_db || @db || return
  Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
    xml.documents do
      xml.parent.add_child db.all.join(" ")
    end
  end.to_xml
end