Sha256: 1b6a76c64259a7195397b453bd60d4c7043abb648400ca3ddfcf92eadec8427e

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

require "yaml"
require "active_support"
require "active_support/core_ext"
require "active_support/inflector"

# numeric test
class String
  def number?
    true if Float(self) rescue false
  end
end

module Hasmenu
  class SpellChecker
    include Printer

    def initialize(options)
      @dictd = options[:dicts] || File.join(Dir.pwd, "dict")
      @xcept = options[:except]
      @dicts = []
    end

    def load_dicts
      yamls = Dir.glob("#{@dictd}/*.yml")
      yamls.each do |yaml|
        @dicts << YAML.load_file(yaml)
      end
    end

    def values_of(h)
      h.values.flatten.map do |e|
        if e.is_a?(Hash)
          values_of(e)
        else
          e.to_s.parameterize.split(/[^\w]/)
        end
      end
    end

    def spell_check(path)
      xcept = @xcept || File.basename(File.dirname(path))
      xyaml = File.join(@dictd, "excepts", xcept + ".yml")

      if File.file? xyaml
        except = YAML.load_file(xyaml)
        @dicts << except
      end

      data = YAML.load_file(path)

      diff = values_of(data).flatten.sort.uniq - @dicts.flatten
      diff = diff.compact.delete_if(&:number?)

      print_header path
      puts diff if diff.present?
    end

    def spell_check_all(path)
      Dir.glob(File.join(path, "**", "*.yml")) do |file|
        spell_check file
      end
    end

    def check(path)
      unless File.exist? path
        print_invalid_path
        return
      end

      load_dicts

      if File.file? path
        spell_check path
      elsif File.directory? path
        spell_check_all path
      else
        print_invalid_path
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hasmenu-0.1.7 lib/hasmenu/spellchecker.rb