methodsolver 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 88d147d86dee794e8d6efa1c405fced0a0d94661
4
+ data.tar.gz: 8fc6b465cbb619726fec383c511fc1251b6f08b1
5
+ SHA512:
6
+ metadata.gz: f9f798884fa6b87d5c0564169151d6a330499c3f70b2a18b59b41cd06b861491bba74c7ef282e59c72c77c0e4ddc9d8751b282e495d434eb34a9ebbee8d32fbe
7
+ data.tar.gz: b76b6cf03f299ef1813230cfced1008fe509988dcbfca685c4d855d37b7b8deeaba284f836fdec3976fdce48b3dffdf7a68a0bef69edaba470878dc63e166b76
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'pry'
7
+ gem 'rake'
8
+ gem 'rspec'
9
+ end
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Methodsolver
2
+
3
+ Finds ruby methods given a block with placeholder.
4
+
5
+ For example:
6
+
7
+ ```ruby
8
+ solve { 'lettuce'.foo == 7 }
9
+ ```
10
+
11
+ Will find `#length` and `#size`
12
+
13
+ ## Installation
14
+
15
+ Clone this repo and run pry:
16
+
17
+ git clone https://github.com/akuhn/methodsolver.git
18
+ cd methodsolver
19
+ bundle
20
+ bundle exec pry
21
+
22
+ And then execute:
23
+
24
+ ```ruby
25
+ require 'methodsolver'
26
+ solve { 'lettuce'.foo == 7 }
27
+ ```
28
+
29
+ Please refer to `examples/solve.rb` (and the rspec tests) for more examples.
30
+
31
+ ## Usage
32
+
33
+ Use with caution!
34
+
35
+ The solver attempts to executes the block with arbitrary methods found on the reciever. Beware of side effects. Append the symbol of dangerous methods to `Methodsolver::BLACKLIST` in order to blacklist them.
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/akuhn/methodsolver.
46
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "methodsolver"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/examples/solve.rb ADDED
@@ -0,0 +1,63 @@
1
+ require 'methodsolver'
2
+
3
+ words = %w(the quick brown fox jumps over the lazy dog)
4
+
5
+ solve {
6
+ words.dup.foo == 'the'
7
+ }
8
+
9
+ solve {
10
+ words.dup.foo(%w(fox dog)) == %w(the quick brown jumps over the lazy)
11
+ }
12
+
13
+ solve {
14
+ Numeric === words.dup.foo
15
+ }
16
+
17
+ solve {
18
+ Hash === words.dup.foo(&:itself)
19
+ }
20
+
21
+ solve {
22
+ %(the quick brown fox jumps over the lazy dog).foo == words
23
+ }
24
+
25
+ solve {
26
+ %w(the quick brown fox).foo %w(jumps over the lazy dog) == words
27
+ }
28
+
29
+ solve {
30
+ 'hello'.foo == 'Hello'
31
+ }
32
+
33
+ solve {
34
+ Math::PI.foo == 3
35
+ }
36
+
37
+ solve {
38
+ 3.41.foo == 3 && -3.41.foo == -3
39
+ }
40
+
41
+ solve {
42
+ ''.foo == String
43
+ }
44
+
45
+ solve {
46
+ 'hello'.foo == 'olleh'
47
+ }
48
+
49
+ solve {
50
+ 'example.rb'.foo('.rb') == 'example'
51
+ }
52
+
53
+ solve {
54
+ 'example.rb'.foo('example') == '.rb'
55
+ }
56
+
57
+ solve {
58
+ /aura/.foo('restaurant')
59
+ }
60
+
61
+ solve {
62
+ 1.foo(10) == 1..10
63
+ }
@@ -0,0 +1,3 @@
1
+ module Methodsolver
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,54 @@
1
+ require "methodsolver/version"
2
+ require 'method_source'
3
+
4
+ module Methodsolver
5
+
6
+ def self.call(&block)
7
+ raise ArgumentError, 'no block given' unless block_given?
8
+ begin
9
+ Object.class_eval('def method_missing(name, *args); throw :method_missing, [self, name]; end')
10
+ method_missing = catch :method_missing do block.call; nil end
11
+ ensure
12
+ Object.class_eval('remove_method :method_missing')
13
+ end
14
+ raise ArgumentError, 'no missing method found' unless method_missing
15
+ object, message = method_missing
16
+ found = methods_for(object).select do |each|
17
+ begin
18
+ object.class.class_eval("alias #{message.inspect} #{each.inspect}")
19
+ true === block.call
20
+ rescue
21
+ false
22
+ ensure
23
+ object.class.class_eval("remove_method #{message.inspect}")
24
+ end
25
+ end
26
+ return object, found
27
+ end
28
+
29
+ BLACKLIST = [:cycle]
30
+
31
+ def self.methods_for(object)
32
+ object.class.ancestors
33
+ .take_while { |a| Object != a }
34
+ .flat_map { |a| a.instance_methods(all = false) }
35
+ .concat(%w(
36
+ ! != == !~ <=> === =~ class eql? equal?
37
+ instance_of? is_a? kind_of? hash nil? to_s
38
+ ))
39
+ .map(&:to_sym)
40
+ .uniq
41
+ .- BLACKLIST
42
+ end
43
+
44
+ end
45
+
46
+ def solve(&block)
47
+ object, found = Methodsolver.call(&block)
48
+ puts "Found #{found.count} methods for #{block.source.strip rescue 'source not available'}"
49
+ found.map do |symbol|
50
+ method = object.method(symbol)
51
+ puts "- #{method.owner}\e[32m##{method.name}\e[0m"
52
+ end
53
+ puts
54
+ end
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'methodsolver/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "methodsolver"
8
+ spec.version = Methodsolver::VERSION
9
+ spec.authors = ["Adrian Kuhn"]
10
+ spec.email = ["akuhn@iam.unibe.ch"]
11
+
12
+ spec.summary = "Finds ruby methods given a block with placeholder."
13
+ spec.homepage = "https://github.com/akuhn/methodsolver"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "method_source"
21
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: methodsolver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Adrian Kuhn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: method_source
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - akuhn@iam.unibe.ch
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".travis.yml"
37
+ - Gemfile
38
+ - README.md
39
+ - Rakefile
40
+ - bin/console
41
+ - bin/setup
42
+ - examples/solve.rb
43
+ - lib/methodsolver.rb
44
+ - lib/methodsolver/version.rb
45
+ - methodsolver.gemspec
46
+ homepage: https://github.com/akuhn/methodsolver
47
+ licenses: []
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.4.6
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Finds ruby methods given a block with placeholder.
69
+ test_files: []