Class: Kotoshu::Suggestions::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/suggestions/context.rb

Overview

Context object passed to suggestion strategies. Encapsulates the state and parameters for suggestion generation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word:, dictionary:, max_results: 10, **options) ⇒ Context

Returns a new instance of Context.



10
11
12
13
14
15
# File 'lib/kotoshu/suggestions/context.rb', line 10

def initialize(word:, dictionary:, max_results: 10, **options)
  @word = word
  @dictionary = dictionary
  @max_results = max_results
  @options = options
end

Instance Attribute Details

#dictionaryObject (readonly)

Returns the value of attribute dictionary.



8
9
10
# File 'lib/kotoshu/suggestions/context.rb', line 8

def dictionary
  @dictionary
end

#max_resultsObject (readonly)

Returns the value of attribute max_results.



8
9
10
# File 'lib/kotoshu/suggestions/context.rb', line 8

def max_results
  @max_results
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/kotoshu/suggestions/context.rb', line 8

def options
  @options
end

#wordObject (readonly)

Returns the value of attribute word.



8
9
10
# File 'lib/kotoshu/suggestions/context.rb', line 8

def word
  @word
end

Instance Method Details

#has_option?(key) ⇒ Boolean

Check if an option is present.

Parameters:

  • key (Symbol)

    The option key

Returns:

  • (Boolean)

    True if option exists



30
31
32
# File 'lib/kotoshu/suggestions/context.rb', line 30

def has_option?(key)
  @options.key?(key)
end

#inspectString Also known as: to_s

Inspect the context.

Returns:

  • (String)

    Inspection string



49
50
51
# File 'lib/kotoshu/suggestions/context.rb', line 49

def inspect
  "Context(word: '#{@word}', max_results: #{@max_results})"
end

#option(key, default = nil) ⇒ Object

Get an option value.

Parameters:

  • key (Symbol)

    The option key

  • default (Object) (defaults to: nil)

    Default value if not found

Returns:

  • (Object)

    The option value



22
23
24
# File 'lib/kotoshu/suggestions/context.rb', line 22

def option(key, default = nil)
  @options.fetch(key, default)
end

#to_hHash

Convert context to hash.

Returns:

  • (Hash)

    Context as hash



37
38
39
40
41
42
43
44
# File 'lib/kotoshu/suggestions/context.rb', line 37

def to_h
  {
    word: @word,
    dictionary: @dictionary,
    max_results: @max_results,
    options: @options
  }
end