Skip to content

Commit 82f7acb

Browse files
committed
Store catalogues globally
This might cause a slight RAM bloating, but it allows for efficient comparisons without overriding `hash` and `eql?`
1 parent 31ffbd6 commit 82f7acb

4 files changed

Lines changed: 15 additions & 9 deletions

File tree

lib/strain_code/catalogue.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ def catalogues_source
1313
end
1414

1515
def catalogue_hash(code)
16-
hsh = catalogues_source['catalogues'].find do |i|
16+
catalogues_source['catalogues'].find do |i|
1717
i['codes'].include? code.to_s.upcase
1818
end
1919
end
2020

2121
def catalogue(code)
2222
hsh = catalogue_hash(code)
23-
new(hsh) if hsh
23+
@catalogue ||= {}
24+
@catalogue[hsh] ||= new(hsh) if hsh
2425
end
2526
end
2627

2728
attr_accessor :codes, :country_code, :name, :name_en, :organization
2829
attr_accessor :url, :url_pattern
2930

30-
def initialize(code)
31-
code = self.class.catalogue_hash(code) unless code.is_a?(Hash)
32-
code.each { |k, v| self.send("#{k}=", v) unless k =~ /^_/ } if code
31+
def initialize(hash)
32+
hash.each { |k, v| self.send("#{k}=", v) unless k =~ /^_/ }
3333
end
3434
end

lib/strain_code/parser.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ def parse(numbers)
2121
end
2222

2323
def number(number)
24-
StrainCode::StrainNumber.new(StrainCode::Parser.clean(number))
24+
StrainCode::StrainNumber.strain_number(number)
2525
end
2626

2727
def catalogue(code)
28-
c = StrainCode::Catalogue.new(code)
29-
c.codes ? c : nil
28+
c = StrainCode::Catalogue.catalogue(code)
29+
c if c&.codes
3030
end
3131
end
3232
end

lib/strain_code/strain_number.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
class StrainCode::StrainNumber
22
attr_accessor :ori_number, :code, :accession
33

4+
class << self
5+
def strain_number(number)
6+
new(StrainCode::Parser.clean(number))
7+
end
8+
end
9+
410
def initialize(number)
511
@ori_number = number.to_s.strip
612
@code, @accession = ori_number.split(/[ :_]+/, 2)

lib/strain_code/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class StrainCode
2-
VERSION = '0.1.1'
2+
VERSION = '0.2.0'
33
VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
44
VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
55
VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:

0 commit comments

Comments
 (0)