Class: TokenKit::Config
- Inherits:
-
Object
- Object
- TokenKit::Config
- Defined in:
- lib/tokenkit/config_compat.rb,
lib/tokenkit/config.rb
Overview
Compatibility wrapper that mimics the old Config singleton API This allows us to migrate gradually
Instance Attribute Summary collapse
-
#delimiter ⇒ Object
Returns the value of attribute delimiter.
-
#grapheme_extended ⇒ Object
Returns the value of attribute grapheme_extended.
-
#lowercase ⇒ Object
Returns the value of attribute lowercase.
-
#max_gram ⇒ Object
Returns the value of attribute max_gram.
-
#min_gram ⇒ Object
Returns the value of attribute min_gram.
-
#preserve_patterns ⇒ Object
Returns the value of attribute preserve_patterns.
-
#regex ⇒ Object
Returns the value of attribute regex.
-
#remove_punctuation ⇒ Object
Returns the value of attribute remove_punctuation.
-
#split_on_chars ⇒ Object
Returns the value of attribute split_on_chars.
-
#strategy ⇒ Object
Returns the value of attribute strategy.
Class Method Summary collapse
-
.instance ⇒ Object
Singleton pattern for backward compatibility.
Instance Method Summary collapse
- #apply! ⇒ Object
-
#build_config ⇒ Object
Called by TokenKit.configure to get the built config.
-
#initialize ⇒ Config
constructor
A new instance of Config.
-
#method_missing(method, *args, &block) ⇒ Object
Delegate all accessors to the global config builder.
-
#reset_temp ⇒ Object
Reset temporary builder.
- #respond_to_missing?(method, include_private = false) ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/tokenkit/config.rb', line 9 def initialize @strategy = :unicode @lowercase = true @remove_punctuation = false @preserve_patterns = [] @grapheme_extended = true @min_gram = 2 @max_gram = 10 @delimiter = "/" @split_on_chars = " \t\n\r" end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Delegate all accessors to the global config builder
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/tokenkit/config_compat.rb', line 13 def method_missing(method, *args, &block) if method.to_s.end_with?('=') # Setter - store in temporary builder attr = method.to_s.chomp('=').to_sym @temp_builder ||= TokenKit.config_hash.to_builder @temp_builder.send(method, *args, &block) if @temp_builder.respond_to?(method) else # Getter - get from current config or temp builder if @temp_builder && @temp_builder.respond_to?(method) @temp_builder.send(method) else TokenKit.config_hash.send(method) if TokenKit.config_hash.respond_to?(method) end end end |
Instance Attribute Details
#delimiter ⇒ Object
Returns the value of attribute delimiter.
3 4 5 |
# File 'lib/tokenkit/config.rb', line 3 def delimiter @delimiter end |
#grapheme_extended ⇒ Object
Returns the value of attribute grapheme_extended.
3 4 5 |
# File 'lib/tokenkit/config.rb', line 3 def grapheme_extended @grapheme_extended end |
#lowercase ⇒ Object
Returns the value of attribute lowercase.
3 4 5 |
# File 'lib/tokenkit/config.rb', line 3 def lowercase @lowercase end |
#max_gram ⇒ Object
Returns the value of attribute max_gram.
3 4 5 |
# File 'lib/tokenkit/config.rb', line 3 def max_gram @max_gram end |
#min_gram ⇒ Object
Returns the value of attribute min_gram.
3 4 5 |
# File 'lib/tokenkit/config.rb', line 3 def min_gram @min_gram end |
#preserve_patterns ⇒ Object
Returns the value of attribute preserve_patterns.
3 4 5 |
# File 'lib/tokenkit/config.rb', line 3 def preserve_patterns @preserve_patterns end |
#regex ⇒ Object
Returns the value of attribute regex.
3 4 5 |
# File 'lib/tokenkit/config.rb', line 3 def regex @regex end |
#remove_punctuation ⇒ Object
Returns the value of attribute remove_punctuation.
3 4 5 |
# File 'lib/tokenkit/config.rb', line 3 def remove_punctuation @remove_punctuation end |
#split_on_chars ⇒ Object
Returns the value of attribute split_on_chars.
3 4 5 |
# File 'lib/tokenkit/config.rb', line 3 def split_on_chars @split_on_chars end |
#strategy ⇒ Object
Returns the value of attribute strategy.
3 4 5 |
# File 'lib/tokenkit/config.rb', line 3 def strategy @strategy end |
Class Method Details
.instance ⇒ Object
Singleton pattern for backward compatibility
8 9 10 |
# File 'lib/tokenkit/config_compat.rb', line 8 def self.instance @instance ||= new end |
Instance Method Details
#apply! ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/tokenkit/config.rb', line 21 def apply! config_hash = { "strategy" => strategy.to_s, "lowercase" => lowercase, "remove_punctuation" => remove_punctuation, "preserve_patterns" => preserve_patterns.map { |p| pattern_to_string(p) } } if strategy == :pattern && regex config_hash["regex"] = regex end if strategy == :grapheme config_hash["extended"] = grapheme_extended end if strategy == :edge_ngram || strategy == :ngram config_hash["min_gram"] = min_gram config_hash["max_gram"] = max_gram end if strategy == :path_hierarchy config_hash["delimiter"] = delimiter end if strategy == :char_group config_hash["split_on_chars"] = split_on_chars end TokenKit.configure(config_hash) end |
#build_config ⇒ Object
Called by TokenKit.configure to get the built config
41 42 43 44 45 |
# File 'lib/tokenkit/config_compat.rb', line 41 def build_config builder = @temp_builder || TokenKit.config_hash.to_builder @temp_builder = nil # Clear after building builder end |
#reset_temp ⇒ Object
Reset temporary builder
48 49 50 |
# File 'lib/tokenkit/config_compat.rb', line 48 def reset_temp @temp_builder = nil end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/tokenkit/config_compat.rb', line 29 def respond_to_missing?(method, include_private = false) # Avoid infinite recursion by checking config_hash instead of config return true if [:strategy=, :lowercase=, :remove_punctuation=, :preserve_patterns=, :regex=, :grapheme_extended=, :min_gram=, :max_gram=, :delimiter=, :split_on_chars=, :strategy, :lowercase, :remove_punctuation, :preserve_patterns, :regex, :grapheme_extended, :min_gram, :max_gram, :delimiter, :split_on_chars].include?(method) super end |
#to_h ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/tokenkit/config.rb', line 53 def to_h { strategy: strategy, regex: regex, grapheme_extended: grapheme_extended, min_gram: min_gram, max_gram: max_gram, delimiter: delimiter, split_on_chars: split_on_chars, lowercase: lowercase, remove_punctuation: remove_punctuation, preserve_patterns: preserve_patterns }.compact end |