Class: Relaton::Db

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

Constant Summary collapse

SUPPORTED_GEMS =
%w[isobib ietfbib gbbib iecbib nistbib].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



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

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



64
65
66
67
68
# File 'lib/relaton/db.rb', line 64

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



40
41
42
43
# File 'lib/relaton/db.rb', line 40

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



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

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

#fetched(key) ⇒ Object



56
57
58
59
60
61
# File 'lib/relaton/db.rb', line 56

def fetched(key)
  return @local_db.fetched key if @local_db
  return @db.fetched key if @db

  ""
end

#load_entry(key) ⇒ Hash

Parameters:

  • key (String)

Returns:

  • (Hash)


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

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

#register_gemsObject



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

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.



83
84
85
86
# File 'lib/relaton/db.rb', line 83

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)


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

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