Class: ChemScanner::Abbreviation

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

Overview

Abbreviation management Singleton

Constant Summary collapse

Util =
ChemScanner::ConfigurationUtil
CUSTOM_PATH =
"#{CONFIG_PATH}/yaml/custom_abbreviations.yaml".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAbbreviation

Returns a new instance of Abbreviation.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/chem_scanner/configuration/abbreviation.rb', line 17

def initialize
  @predefined = YAML.load_file("#{CONFIG_PATH}/yaml/abbreviations.yaml")
  @solvents = YAML.load_file("#{CONFIG_PATH}/yaml/solvents.yaml")
  @solvents_downcase = Util.hash_downcase(@solvents)
  @predefined.merge!(@solvents)
  @predefined_downcase = Util.hash_downcase(@predefined)

  FileUtils.touch(CUSTOM_PATH) unless File.exist?(CUSTOM_PATH)
  @custom = YAML.load_file(CUSTOM_PATH) || {}
  @custom_downcase = Util.hash_downcase(@custom)
  @custom_fs = File.open(CUSTOM_PATH, "a")
end

Instance Attribute Details

#customObject (readonly)

Returns the value of attribute custom.



11
12
13
# File 'lib/chem_scanner/configuration/abbreviation.rb', line 11

def custom
  @custom
end

#predefinedObject (readonly)

Returns the value of attribute predefined.



11
12
13
# File 'lib/chem_scanner/configuration/abbreviation.rb', line 11

def predefined
  @predefined
end

#solventsObject (readonly)

Returns the value of attribute solvents.



11
12
13
# File 'lib/chem_scanner/configuration/abbreviation.rb', line 11

def solvents
  @solvents
end

Instance Method Details

#add(abb, smi) ⇒ Object



38
39
40
# File 'lib/chem_scanner/configuration/abbreviation.rb', line 38

def add(abb, smi)
  add_hash(abb => smi)
end

#add_hash(hash) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/chem_scanner/configuration/abbreviation.rb', line 42

def add_hash(hash)
  hash.delete_if { |key, _| @custom.has_key?(key) }
  @custom.merge!(hash)
  @custom_downcase.merge!(Util.hash_downcase(hash))

  @custom_fs.puts(hash.to_yaml)
  @custom_fs.fsync

  hash
end

#allObject



30
31
32
# File 'lib/chem_scanner/configuration/abbreviation.rb', line 30

def all
  @predefined.merge(@custom)
end

#get_abbreviation(abb) ⇒ Object



34
35
36
# File 'lib/chem_scanner/configuration/abbreviation.rb', line 34

def get_abbreviation(abb)
  @predefined_downcase.merge(@custom_downcase)[abb.downcase] || ""
end

#remove(abb) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/chem_scanner/configuration/abbreviation.rb', line 53

def remove(abb)
  return nil if @predefined.has_key?(abb)

  removed = @custom.delete(abb)
  return nil if removed.nil?

  sync_custom(@custom)

  [abb]
end