Skip to content

Commit

Permalink
Init commit, seems to work well :D
Browse files Browse the repository at this point in the history
  • Loading branch information
Shervanator committed Oct 23, 2014
0 parents commit 2fd439f
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
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
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format progress
4 changes: 4 additions & 0 deletions Gemfile
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
22 changes: 22 additions & 0 deletions LICENSE.txt
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.
31 changes: 31 additions & 0 deletions README.md
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
9 changes: 9 additions & 0 deletions Rakefile
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]
26 changes: 26 additions & 0 deletions feeling_lucky.gemspec
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
74 changes: 74 additions & 0 deletions lib/feeling_lucky.rb
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


3 changes: 3 additions & 0 deletions lib/feeling_lucky/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module FeelingLucky
VERSION = "0.0.1"
end
11 changes: 11 additions & 0 deletions spec/feeling_lucky/feeling_lucky_spec.rb
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
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
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

0 comments on commit 2fd439f

Please sign in to comment.