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.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/noble_names/match_index.rb', line 25 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.
11 12 13 |
# File 'lib/noble_names/match_index.rb', line 11 def data @data end |
Instance Method Details
#in_particle_list?(word) ⇒ Boolean
Checks weither a word is in the nobility particle list.
52 53 54 55 |
# File 'lib/noble_names/match_index.rb', line 52 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.
44 45 46 |
# File 'lib/noble_names/match_index.rb', line 44 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.
76 77 78 79 80 81 82 |
# File 'lib/noble_names/match_index.rb', line 76 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
87 88 89 90 91 92 |
# File 'lib/noble_names/match_index.rb', line 87 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.
61 62 63 64 65 66 67 |
# File 'lib/noble_names/match_index.rb', line 61 def selected_data @selected_data ||= @data .select { |l| @languages.include? l.to_sym } .values .flatten end |