-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2fd439f
Showing
11 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/.bundle/ | ||
/.yardoc | ||
/Gemfile.lock | ||
/_yardoc/ | ||
/coverage/ | ||
/doc/ | ||
/pkg/ | ||
/spec/reports/ | ||
/tmp/ | ||
*.bundle | ||
*.so | ||
*.o | ||
*.a | ||
mkmf.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--color | ||
--format progress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in feeling_lucky.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = ["[email protected]"] | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module FeelingLucky | ||
VERSION = "0.0.1" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'feeling_lucky' | ||
|
||
RSpec.configure do |config| | ||
config.order = 'random' | ||
end |