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.
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.
-
#particles ⇒ Hash
Returns and caches the particles of a MatchIndex.
-
#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.
-
#selected_data ⇒ Array
(also: #prefixes)
Caches the particles or prefixes by the languages selected in the config.
Constructor Details
#initialize(list) ⇒ MatchIndex
Takes either a String or any Object and tries to convert it to a hash.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/noble_names/match_index.rb', line 26 def initialize(list) case list when String @data = YAML.load_file(File.(list, Data::DATA_PATH)) @data = @data.values.first else @data = Hash[list] end @lanugages = NobleNames.configuration.languages end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
12 13 14 |
# File 'lib/noble_names/match_index.rb', line 12 def data @data end |
Instance Method Details
#in_particle_list?(word) ⇒ Boolean
Checks weither a word is in the nobility particle list.
53 54 55 56 |
# File 'lib/noble_names/match_index.rb', line 53 def in_particle_list?(word) reindex if @languages != NobleNames.configuration.languages particles.key? word end |
#particles ⇒ Hash
Returns and caches the particles of a MatchIndex.
45 46 47 |
# File 'lib/noble_names/match_index.rb', line 45 def particles @particles ||= Hash[selected_data.collect { |v| [v.downcase, v] }] end |
#prefix?(word) ⇒ String
Checks weither a word has a prefix as defined in the MatchIndexs data and returns it.
77 78 79 80 81 82 83 |
# File 'lib/noble_names/match_index.rb', line 77 def prefix?(word) reindex if @languages != NobleNames.configuration.languages prefixes.each do |pre| return pre if (word =~ Regexp.new(pre)) == 0 end nil end |
#reindex ⇒ Object
Resets the state of the MatchIndex
88 89 90 91 92 93 |
# File 'lib/noble_names/match_index.rb', line 88 def reindex @languages = NobleNames.configuration.languages @selected_data = nil @prefixes = nil @particles = nil end |
#selected_data ⇒ Array Also known as: prefixes
Caches the particles or prefixes by the languages selected in the config.
62 63 64 65 66 67 68 |
# File 'lib/noble_names/match_index.rb', line 62 def selected_data @selected_data ||= @data .select { |l| @languages.include? l.to_sym } .values .flatten end |