Class: ChemScanner::Superatom

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/chem_scanner/configuration/superatom.rb

Overview

Abbreviation management Singleton

Constant Summary collapse

Util =
ChemScanner::ConfigurationUtil
PREDEFINED_PATH =
"#{CONFIG_PATH}/superatom.txt".freeze
CUSTOM_PATH =
"#{CONFIG_PATH}/custom_superatom.txt".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSuperatom

Returns a new instance of Superatom.



18
19
20
21
22
23
24
25
# File 'lib/chem_scanner/configuration/superatom.rb', line 18

def initialize
  FileUtils.touch(CUSTOM_PATH) unless File.exist?(CUSTOM_PATH)
  @custom = Util.read_superatom(CUSTOM_PATH) || {}
  @predefined = Util.read_superatom(PREDEFINED_PATH)
  @all = @custom.merge(@predefined)

  @custom_fs = File.open(CUSTOM_PATH, "a")
end

Instance Attribute Details

#allObject (readonly)

Returns the value of attribute all.



13
14
15
# File 'lib/chem_scanner/configuration/superatom.rb', line 13

def all
  @all
end

#customObject (readonly)

Returns the value of attribute custom.



13
14
15
# File 'lib/chem_scanner/configuration/superatom.rb', line 13

def custom
  @custom
end

#predefinedObject (readonly)

Returns the value of attribute predefined.



13
14
15
# File 'lib/chem_scanner/configuration/superatom.rb', line 13

def predefined
  @predefined
end

Instance Method Details

#add(satom, smi) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/chem_scanner/configuration/superatom.rb', line 31

def add(satom, smi)
  return if predefined.has_key?(satom)

  added_hash = {}

  sym = satom.to_sym
  @custom[sym] = smi
  @all[sym] = smi
  added_hash[satom] = smi

  lines = Util.hash_to_lines(added_hash)
  @custom_fs.puts(lines)
  @custom_fs.fsync

  added_hash
end

#get_superatom(superatom) ⇒ Object



27
28
29
# File 'lib/chem_scanner/configuration/superatom.rb', line 27

def get_superatom(superatom)
  @all[superatom.to_sym] || ""
end

#remove(satom) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chem_scanner/configuration/superatom.rb', line 48

def remove(satom)
  return nil if predefined.has_key?(satom)

  sym = satom.to_sym
  is_removed = @custom.delete(sym)
  removed = is_removed.nil? ? nil : satom
  @all.delete(sym)

  sync_custom

  [removed].compact
end

#sync_customObject



61
62
63
64
65
66
67
68
# File 'lib/chem_scanner/configuration/superatom.rb', line 61

def sync_custom
  @custom_fs.close
  File.open(CUSTOM_PATH, "w+") do |file|
    file.puts(Util.hash_to_lines(custom))
  end

  @custom_fs = File.open(CUSTOM_PATH, "a")
end