Module: Rosette::Core::PhraseIndexPolicy

Included in:
Phrase
Defined in:
lib/rosette/core/extractor/phrase/phrase_index_policy.rb

Overview

Defines the logic for which field should be used to index a phrase. Phrases can be indexed either by key or meta key. The logic in this module is designed to determine which of these fields to use for a particular phrase, taking into consideration nil and blank values. By default, phrases are indexed by meta key. If however a phrase has a nil or blank meta key, the key should be used as the index value instead.

Must be mixed into an object that responds to Phrase methods, specifically key and meta_key.

Examples:

class MyPhrase
  include PhraseIndexPolicy

  attr_accessor :key, :meta_key

  def initialize(key, meta_key)
    @key = key; @meta_key = meta_key
  end
end

p = MyPhrase.new('foo', 'bar')
p.index_key    # => :meta_key
p.index_value  # => 'bar'

p = MyPhrase.new('foo', nil)
p.index_key    # => :key
p.index_value  # => 'foo'

p = MyPhrase.new(nil, nil)
p.index_key    # => :key
p.index_value  # => ''

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.index_key(key, meta_key) ⇒ Symbol

Determines which key should be used for indexing.

Parameters:

  • key (String)

    The phrase key.

  • meta_key (String)

    The phrase meta key.

Returns:

  • (Symbol)

    either :key or :meta_key.


47
48
49
50
51
52
53
# File 'lib/rosette/core/extractor/phrase/phrase_index_policy.rb', line 47

def self.index_key(key, meta_key)
  if !meta_key || meta_key.empty?
    :key
  else
    :meta_key
  end
end

.index_value(key, meta_key) ⇒ String

Determines which value should be used for indexing.

Parameters:

  • key (String)

    The phrase key.

  • meta_key (String)

    The phrase meta_key.

Returns:

  • (String)

    either the given key or meta key, or an empty string if the value is nil. In other words, if the value at #index_key is nil, returns an empty string.


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rosette/core/extractor/phrase/phrase_index_policy.rb', line 62

def self.index_value(key, meta_key)
  value = case index_key(key, meta_key)
    when :key then key
    else meta_key
  end

  case value
    when NilClass
      ''
    else
      value
  end
end

Instance Method Details

#index_keySymbol

Determines which key should be used for indexing.

Returns:

  • (Symbol)

    either :key or :meta_key.


79
80
81
# File 'lib/rosette/core/extractor/phrase/phrase_index_policy.rb', line 79

def index_key
  PhraseIndexPolicy.index_key(key, meta_key)
end

#index_valueString

Determines which value should be used for indexing.

Returns:

  • (String)

    either the given key or meta key, or an empty string if the value is nil. In other words, if the value at #index_key is nil, returns an empty string.


88
89
90
# File 'lib/rosette/core/extractor/phrase/phrase_index_policy.rb', line 88

def index_value
  PhraseIndexPolicy.index_value(key, meta_key)
end