Class: I18nFlow::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_flow/search.rb,
lib/i18n_flow/search.rb

Defined Under Namespace

Classes: Item

Constant Summary collapse

SCORE_KEY_CS_MATCH =
10
SCORE_KEY_CI_MATCH =
9
SCORE_CONTENT_CS_MATCH =
8
SCORE_CONTENT_CI_MATCH =
6

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository:, pattern:, include_all: false) ⇒ Search

Returns a new instance of Search.



38
39
40
41
42
43
44
45
46
# File 'lib/i18n_flow/search.rb', line 38

def initialize(
  repository:,
  pattern:,
  include_all: false
)
  @repository  = repository
  @pattern     = pattern
  @include_all = include_all
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



31
32
33
# File 'lib/i18n_flow/search.rb', line 31

def pattern
  @pattern
end

#repositoryObject (readonly)

Returns the value of attribute repository.



30
31
32
# File 'lib/i18n_flow/search.rb', line 30

def repository
  @repository
end

Instance Method Details

#include_all?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/i18n_flow/search.rb', line 64

def include_all?
  !!@include_all
end

#indexed_resultsObject



68
69
70
# File 'lib/i18n_flow/search.rb', line 68

def indexed_results
  @indexed_results ||= Hash.new { |h, k| h[k] = [] }
end

#pattern_downcaseObject



72
73
74
# File 'lib/i18n_flow/search.rb', line 72

def pattern_downcase
  @pattern_downcase ||= pattern.downcase
end

#resultsObject



58
59
60
61
62
# File 'lib/i18n_flow/search.rb', line 58

def results
  @results ||= indexed_results
    .sort_by { |k, rs| -rs.map(&:score).max }
    .to_h
end

#search!Object



48
49
50
51
52
53
54
55
56
# File 'lib/i18n_flow/search.rb', line 48

def search!
  repository.asts_by_scope.each do |scope, locale_trees|
    asts = locale_trees
      .map { |locale, tree| tree[locale] }
      .compact

    search_on(asts)
  end
end