Sha256: c523d734e4a4d77adea72fb0dda9d1c47ed448a54f2c0ca6fe361e74acebf1a1

Contents?: true

Size: 1.03 KB

Versions: 6

Compression:

Stored size: 1.03 KB

Contents

module Lolita
  module Menu
    module Autocomplete
      class Collector

        def initialize(term)
          @links = []
          @term = term
          collect_from_file
        end

        def collect_from_file
          if File.exist?(Lolita::Menu::Autocomplete::FileBuilder.output_file)
            File.open(Lolita::Menu::Autocomplete::FileBuilder.output_file, "r").readlines.each{ |line|
              item = line.to_s.split(/\s/)
              first_item = item.shift
              item = [first_item, item.join(" ")]
              term_regexp = Regexp.new(@term.to_s.gsub("\\",""))
              if item.first.match(term_regexp) || item.last.match(term_regexp)
                @links << item
              end
            }
          end
        end

        def to_json
          result = []
          @links.each_with_index{|item, index|
            result << {:id => index, :label => "#{item.last} #{item[1] != item[0] ? "(#{item[0]})" : ""}", :value => item.first}
          }
          result
        end

      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lolita-menu-0.4.2 lib/lolita-menu/autocomplete/collector.rb
lolita-menu-0.4.1 lib/lolita-menu/autocomplete/collector.rb
lolita-menu-0.4.0 lib/lolita-menu/autocomplete/collector.rb
lolita-menu-0.3.6 lib/lolita-menu/autocomplete/collector.rb
lolita-menu-0.3.5 lib/lolita-menu/autocomplete/collector.rb
lolita-menu-0.3.4 lib/lolita-menu/autocomplete/collector.rb