diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3fdc2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +/.bundle/ +/.yardoc +/Gemfile.lock +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ +*.bundle +*.so +*.o +*.a +mkmf.log diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..5f16476 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--format progress diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..faa149d --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in feeling_lucky.gemspec +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..f975525 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2014 Shervin Aflatooni + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..65bb4d9 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# FeelingLucky + +TODO: Write a gem description + +## Installation + +Add this line to your application's Gemfile: + +```ruby +gem 'feeling_lucky' +``` + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install feeling_lucky + +## Usage + +TODO: Write usage instructions here + +## Contributing + +1. Fork it ( https://github.com/[my-github-username]/feeling_lucky/fork ) +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create a new Pull Request diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..fc15a26 --- /dev/null +++ b/Rakefile @@ -0,0 +1,9 @@ +require "bundler/gem_tasks" + +require "rspec/core/rake_task" +RSpec::Core::RakeTask.new('spec') do |t| + t.verbose = true +end + + +task :default => [:spec] diff --git a/feeling_lucky.gemspec b/feeling_lucky.gemspec new file mode 100644 index 0000000..9adf450 --- /dev/null +++ b/feeling_lucky.gemspec @@ -0,0 +1,26 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'feeling_lucky/version' + +Gem::Specification.new do |spec| + spec.name = "feeling_lucky" + spec.version = FeelingLucky::VERSION + spec.authors = ["Shervin Aflatooni"] + spec.email = ["shervinaflatooni@gmail.com"] + spec.summary = %q{Hate NoMethodErrors? Are you feeling lucky?.} + spec.description = %q{Do you hate spelling mistakes? Did you forget what you called that method or attribute? Feeling lucky will fix that for you!} + spec.homepage = "" + spec.license = "MIT" + + spec.files = `git ls-files -z`.split("\x0") + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] + + spec.add_dependency "interception" + + spec.add_development_dependency "bundler", "~> 1.7" + spec.add_development_dependency "rake", "~> 10.0" + spec.add_development_dependency "rspec" +end diff --git a/lib/feeling_lucky.rb b/lib/feeling_lucky.rb new file mode 100644 index 0000000..d1c1770 --- /dev/null +++ b/lib/feeling_lucky.rb @@ -0,0 +1,74 @@ +require 'feeling_lucky/version' + +module Levenshtein + # This code is based directly on the Text gem implementation + # Returns a value representing the "cost" of transforming str1 into str2 + # taken from https://github.com/threedaymonk/text/blob/master/lib/text/levenshtein.rb + def distance(str1, str2) + n = str1.length + m = str2.length + return m if n.zero? + return n if m.zero? + + d = (0..m).to_a + x = nil + + str1.each_char.with_index(1) do |char1, i| + str2.each_char.with_index do |char2, j| + cost = (char1 == char2) ? 0 : 1 + x = min3( + d[j+1] + 1, # insertion + i + 1, # deletion + d[j] + cost # substitution + ) + d[j] = i + i = x + end + d[m] = x + end + + x + end + module_function :distance + + private + + def min3(a, b, c) # :nodoc: + if a < b && a < c + a + elsif b < c + b + else + c + end + end + module_function :min3 +end + +def threshold(word) + (word.size * 0.3).ceil +end + +class Object + def method_missing(meth, *args, &block) + all_methods = self.methods + self.singleton_methods + meth_str = meth.to_s + threshold = threshold(meth_str) + good_methods = all_methods.select {|word| Levenshtein.distance(meth_str, word.to_s) <= threshold } + if good_methods.length > 0 + puts "#{self.class}:: for #{meth} picked #{good_methods.first}" + begin + self.send(good_methods.first, *args, &block) + rescue Exception => e + super + end + else + super + end + end +end + +module FeelingLucky +end + + diff --git a/lib/feeling_lucky/version.rb b/lib/feeling_lucky/version.rb new file mode 100644 index 0000000..43963be --- /dev/null +++ b/lib/feeling_lucky/version.rb @@ -0,0 +1,3 @@ +module FeelingLucky + VERSION = "0.0.1" +end diff --git a/spec/feeling_lucky/feeling_lucky_spec.rb b/spec/feeling_lucky/feeling_lucky_spec.rb new file mode 100644 index 0000000..7e46219 --- /dev/null +++ b/spec/feeling_lucky/feeling_lucky_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe 'Feeling Lucky' do + it 'doesnt effect working methods' do + expect("hello".upcase).to eq("HELLO") + end + + it 'corrects spelling mistakes' do + expect("hello".uppcase).to eq("HELLO") + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..eac18d0 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,5 @@ +require 'feeling_lucky' + +RSpec.configure do |config| + config.order = 'random' +end