Class: AnyStyle::Dictionary::GDBM

Inherits:
AnyStyle::Dictionary show all
Defined in:
lib/anystyle/dictionary/gdbm.rb

Instance Attribute Summary collapse

Attributes inherited from AnyStyle::Dictionary

#db, #options

Instance Method Summary collapse

Methods inherited from AnyStyle::Dictionary

create, instance, #populate!, #tag_counts, #tags

Constructor Details

#initialize(options = {}) ⇒ GDBM

Returns a new instance of GDBM.



14
15
16
# File 'lib/anystyle/dictionary/gdbm.rb', line 14

def initialize(options = {})
  super(self.class.defaults.merge(options))
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



12
13
14
# File 'lib/anystyle/dictionary/gdbm.rb', line 12

def env
  @env
end

Instance Method Details

#closeObject



26
27
28
# File 'lib/anystyle/dictionary/gdbm.rb', line 26

def close
  db.close if open?
end

#empty?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/anystyle/dictionary/gdbm.rb', line 34

def empty?
  open? and db.empty?
end

#get(key) ⇒ Object



43
44
45
# File 'lib/anystyle/dictionary/gdbm.rb', line 43

def get(key)
  db[key.to_s].to_i
end

#openObject



18
19
20
21
22
23
24
# File 'lib/anystyle/dictionary/gdbm.rb', line 18

def open
  close
  @db = ::GDBM.new(*options.values_at(:path, :mode, :flags))
  self
ensure
  populate! if empty?
end

#open?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/anystyle/dictionary/gdbm.rb', line 30

def open?
  !(db.nil? || db.closed?)
end

#put(key, value) ⇒ Object



47
48
49
# File 'lib/anystyle/dictionary/gdbm.rb', line 47

def put(key, value)
  db[key.to_s] = value.to_i.to_s
end

#truncateObject



38
39
40
41
# File 'lib/anystyle/dictionary/gdbm.rb', line 38

def truncate
  close
  File.unlink(options[:path])
end