Class: TokenKit::Config

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

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

#delimiterObject

Returns the value of attribute delimiter.



3
4
5
# File 'lib/tokenkit/config.rb', line 3

def delimiter
  @delimiter
end

#grapheme_extendedObject

Returns the value of attribute grapheme_extended.



3
4
5
# File 'lib/tokenkit/config.rb', line 3

def grapheme_extended
  @grapheme_extended
end

#lowercaseObject

Returns the value of attribute lowercase.



3
4
5
# File 'lib/tokenkit/config.rb', line 3

def lowercase
  @lowercase
end

#max_gramObject

Returns the value of attribute max_gram.



3
4
5
# File 'lib/tokenkit/config.rb', line 3

def max_gram
  @max_gram
end

#min_gramObject

Returns the value of attribute min_gram.



3
4
5
# File 'lib/tokenkit/config.rb', line 3

def min_gram
  @min_gram
end

#preserve_patternsObject

Returns the value of attribute preserve_patterns.



3
4
5
# File 'lib/tokenkit/config.rb', line 3

def preserve_patterns
  @preserve_patterns
end

#regexObject

Returns the value of attribute regex.



3
4
5
# File 'lib/tokenkit/config.rb', line 3

def regex
  @regex
end

#remove_punctuationObject

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_charsObject

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

#strategyObject

Returns the value of attribute strategy.



3
4
5
# File 'lib/tokenkit/config.rb', line 3

def strategy
  @strategy
end

Class Method Details

.instanceObject

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_configObject

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_tempObject

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

Returns:

  • (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_hObject



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