Sha256: 385b902bc49abe6fc20d1563d79c53a49aa4dd72b5f19f61dad18b5bde642b7c

Contents?: true

Size: 1.3 KB

Versions: 8

Compression:

Stored size: 1.3 KB

Contents

module Lolita
  module Menu
    module Autocomplete
      class FileBuilder

        class << self
          def input_file
            Lolita.menu.autocomplete_input_file
          end

          def output_file
            Lolita.menu.autocomplete_output_file
          end
        end

        attr_reader :file

        def initialize(mode)
          @file = File.open(self.class.output_file,mode)
        end

        def add url,label = nil
          @file.puts "#{url} #{label || url}"
        end

        def reject_lines_with url, label = nil
          lines_arr = @file.readlines
          label ||= url
          lines_arr.reject do |line|
            line_match_url_and_label?(line, url, label)
          end
        end

        def exist?(url, label = nil)
          old_lineno = @file.lineno
          @file.rewind
          label ||= url
          !!@file.detect do |line|
            line_match_url_and_label?(line, url, label)
          end
        end

        def write_lines(*lines)
          lines.each do |line|
            @file.puts(line)
          end
        end

        def finalize!
          @file.close
        end

        private

        def line_match_url_and_label?(line, url, label)
          line = line.gsub($/, "")
          line == "#{url} #{label}"
        end
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

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