Class: NobleNames::MatchIndex
- Inherits:
-
Object
- Object
- NobleNames::MatchIndex
- Defined in:
- lib/noble_names/match_index.rb
Overview
A MatchIndex holds the data necessary for finding prefixes and particles in Strings and checks them for it. MatchIndexs use Hashes for finding particles to guarantee constant performance in big particle lists. A MatchIndex has a lot of mutable state to cache as much matching information as possible.
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#particles ⇒ Hash
readonly
Returns and caches the particles of a MatchIndex.
-
#selected_data ⇒ Array
(also: #prefixes)
readonly
Caches the particles or prefixes by the languages selected in the config.
Instance Method Summary collapse
-
#in_particle_list?(word) ⇒ Boolean
Checks weither a word is in the nobility particle list.
-
#initialize(list) ⇒ MatchIndex
constructor
Takes either a String or any Object and tries to convert it to a hash.
-
#prefix?(word) ⇒ String
Checks weither a word has a prefix as defined in the MatchIndexs data and returns it.
-
#reindex ⇒ Object
Resets the state of the MatchIndex.
Constructor Details
#initialize(list) ⇒ MatchIndex
Takes either a String or any Object and tries to convert it to a hash.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/noble_names/match_index.rb', line 27 def initialize(list) case list when String @data = YAML.load_file(File.(list, Data::DATA_PATH)) @data = @data.values.first else @data = list.to_h end @lanugages = NobleNames.configuration.languages end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
13 14 15 |
# File 'lib/noble_names/match_index.rb', line 13 def data @data end |
#particles ⇒ Hash
Returns and caches the particles of a MatchIndex.
46 47 48 |
# File 'lib/noble_names/match_index.rb', line 46 def particles @particles ||= selected_data.to_h { |v| [v.downcase, v] } end |
#selected_data ⇒ Array Also known as: prefixes
Caches the particles or prefixes by the languages selected in the config.
63 64 65 66 67 68 69 |
# File 'lib/noble_names/match_index.rb', line 63 def selected_data @selected_data ||= data .select { |l| languages.include? l.to_sym } .values .flatten end |
Instance Method Details
#in_particle_list?(word) ⇒ Boolean
Checks weither a word is in the nobility particle list.
54 55 56 57 |
# File 'lib/noble_names/match_index.rb', line 54 def in_particle_list?(word) reindex if languages != NobleNames.configuration.languages particles.key? word end |
#prefix?(word) ⇒ String
Checks weither a word has a prefix as defined in the MatchIndexs data and returns it.
78 79 80 81 82 83 84 |
# File 'lib/noble_names/match_index.rb', line 78 def prefix?(word) reindex if languages != NobleNames.configuration.languages prefixes.each do |pre| return pre unless (word =~ Regexp.new("^#{pre}")).nil? end nil end |
#reindex ⇒ Object
Resets the state of the MatchIndex
89 90 91 92 93 94 |
# File 'lib/noble_names/match_index.rb', line 89 def reindex self.languages = NobleNames.configuration.languages self.selected_data = nil self.prefixes = nil self.particles = nil end |