Sha256: 5ee1c511b7ff6a4205bb3bf4b82399c65adf85c082d4b6126ef89d26d73b137d

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require_relative 'options'
module CookbookReader 
  class Parser
    def initialize(argv)
      @options = Options.new(argv)
      @top_list = []
    end
    
    def run
      finder = Finder.from_file(@options.dictionary)
      @options.words_to_find.each do |word|
         anagrams = finder.lookup(word)
         if anagrams
           puts "Anagrams of #{word}: #{anagrams.join(', ')}"
         else
           puts "No anagrams of #{word} in #{@options.dictionary}"
         end
      end
    end

    def parse
      @metadata_list = Array.new
      Dir.glob("#{@options.path_to_search}/**/metadata.rb") do |filename|
        @metadata_list.push read_file filename


      end
    end

    def display_data
      #pp @top_list

      @top_list.each do |element|
        puts  "Cookbook: #{element['name']}\n"
        puts "  Dependancies:"
        element['depends'].each do |name, version|
          puts "    #{name}, #{version}\n"
        end
        puts "\n"
      end
    end

  private
    def read_file(filename)
      @hash = {}

      @depends_list = {}
      File.open(filename) do |fp|
        fp.each do |line|
          key, value = line.chomp.split(" ")
          key.strip!
          value.strip!
          key = key.chomp('"').reverse.chomp('"').reverse
          value = value.chomp('"').reverse.chomp('"').reverse

          if key =~ /depends/
            @hash[key] = add_dependencies(line)
          else
            @hash[key] = value
          end
        end
      end
      @top_list.push @hash

    end

    def add_dependencies(line)
      line.slice! "depends "
      k, v = line.chomp.split(",")
      v = "none" if v.nil?
      k.strip!
      v.strip!
      v = v.chomp('"').reverse.chomp('"').reverse
      v = v.chomp('"').reverse.chomp('"').reverse
      k = k.chomp('"').reverse.chomp('"').reverse
      k.strip!
      v.strip!
      @depends_list[k] = v
      @depends_list
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cookbook-reader-0.1.0 lib/cookbook-reader/parser.rb