pry-rails 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,5 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ test/app
6
+ gemfiles
@@ -0,0 +1,22 @@
1
+ appraise "rails30" do
2
+ gem "rails", "3.0.15"
3
+ gem "sqlite3"
4
+ end
5
+
6
+ appraise "rails31" do
7
+ gem "rails", "3.1.6"
8
+ gem "sqlite3"
9
+ end
10
+
11
+ appraise "rails32" do
12
+ gem "rails", "3.2.6"
13
+ gem "sqlite3"
14
+ end
15
+
16
+ appraise "rails4" do
17
+ gem "rails",
18
+ :git => "git@github.com:rails/rails.git"
19
+ gem "active_record_deprecated_finders",
20
+ :git => "git@github.com:rails/active_record_deprecated_finders"
21
+ gem "sqlite3"
22
+ end
data/LICENCE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Robin Wenglewski
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -1 +1,43 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
1
3
  require "bundler/gem_tasks"
4
+ require "appraisal"
5
+
6
+ include FileUtils
7
+
8
+ desc 'Create test Rails app'
9
+ task :init_test_app => 'appraisal:install' do
10
+ # Remove and generate test app using Rails 3.0
11
+ rm_rf 'test/app'
12
+ system 'env BUNDLE_GEMFILE=gemfiles/rails30.gemfile bundle exec rails new test/app'
13
+
14
+ # Copy test routes file into place
15
+ cp 'test/routes.rb', 'test/app/config/routes.rb'
16
+
17
+ # Remove rjs line from environment, since it's gone in versions >= 3.1
18
+ env_contents = File.readlines('test/app/config/environments/development.rb')
19
+ File.open('test/app/config/environments/development.rb', 'w') do |f|
20
+ f.puts env_contents.reject { |l| l =~ /rjs/ }.join("\n")
21
+ end
22
+
23
+ # Generate a few models
24
+ cd 'test/app'
25
+ system 'env BUNDLE_GEMFILE=../../gemfiles/rails30.gemfile bundle exec rails g model Pokemon name:string caught:binary species:string abilities:string'
26
+ system 'env BUNDLE_GEMFILE=../../gemfiles/rails30.gemfile bundle exec rails g model Hacker social_ability:integer'
27
+ system 'env BUNDLE_GEMFILE=../../gemfiles/rails30.gemfile bundle exec rails g model Beer name:string type:string rating:integer ibu:integer abv:integer'
28
+ system 'env BUNDLE_GEMFILE=../../gemfiles/rails30.gemfile bundle exec rake db:migrate'
29
+
30
+ # Replace generated models
31
+ cd '../..'
32
+ cp_r 'test/models', 'test/app/app/models'
33
+ end
34
+
35
+ desc 'Start the Rails server'
36
+ task :server do
37
+ exec 'cd test/app && rails server'
38
+ end
39
+
40
+ desc 'Start the Rails console'
41
+ task :console do
42
+ exec 'cd test/app && rails console'
43
+ end
data/Readme.md CHANGED
@@ -15,6 +15,75 @@ Add this line to your gemfile:
15
15
 
16
16
  `bundle install` and enjoy pry.
17
17
 
18
+ # Usage
19
+
20
+ ```
21
+ $ rails console
22
+ [1] pry(main)> show-routes
23
+ pokemon POST /pokemon(.:format) pokemons#create
24
+ new_pokemon GET /pokemon/new(.:format) pokemons#new
25
+ edit_pokemon GET /pokemon/edit(.:format) pokemons#edit
26
+ GET /pokemon(.:format) pokemons#show
27
+ PUT /pokemon(.:format) pokemons#update
28
+ DELETE /pokemon(.:format) pokemons#destroy
29
+ beer POST /beer(.:format) beers#create
30
+ new_beer GET /beer/new(.:format) beers#new
31
+ edit_beer GET /beer/edit(.:format) beers#edit
32
+ GET /beer(.:format) beers#show
33
+ PUT /beer(.:format) beers#update
34
+ DELETE /beer(.:format) beers#destroy
35
+ [2] pry(main)> show-routes --grep beer
36
+ beer POST /beer(.:format) beers#create
37
+ new_beer GET /beer/new(.:format) beers#new
38
+ edit_beer GET /beer/edit(.:format) beers#edit
39
+ GET /beer(.:format) beers#show
40
+ PUT /beer(.:format) beers#update
41
+ DELETE /beer(.:format) beers#destroy
42
+ [3] pry(main)> show-routes --grep new
43
+ new_pokemon GET /pokemon/new(.:format) pokemons#new
44
+ new_beer GET /beer/new(.:format) beers#new
45
+ [4] pry(main)> show-models
46
+ Beer
47
+ id: integer
48
+ name: string
49
+ type: string
50
+ rating: integer
51
+ ibu: integer
52
+ abv: integer
53
+ created_at: datetime
54
+ updated_at: datetime
55
+ belongs_to hacker
56
+ Hacker
57
+ id: integer
58
+ social_ability: integer
59
+ created_at: datetime
60
+ updated_at: datetime
61
+ has_many pokemons
62
+ has_many beers
63
+ Pokemon
64
+ id: integer
65
+ name: string
66
+ caught: binary
67
+ species: string
68
+ abilities: string
69
+ created_at: datetime
70
+ updated_at: datetime
71
+ belongs_to hacker
72
+ has_many beers through hacker
73
+ ```
74
+
75
+ # Developing
76
+
77
+ To initialize the dev environment for pry-rails, run
78
+ `rake init_test_app`.
79
+
80
+ To test any changes across Rails 3.0, 3.1, 3.2, and 4.0, run
81
+ `rake appraisal console` and `rake appraisal server`.
82
+
83
+ For a specific version of Rails, you can use `rake appraisal:rails30`,
84
+ `rake appraisal:rails31`, `rake appraisal:rails32`, and `rake
85
+ appraisal:rails4`.
86
+
18
87
  # Alternative
19
88
 
20
89
  If you want to enable pry everywhere, make sure to check out [pry everywhere](http://lucapette.com/pry/pry-everywhere/).
@@ -1,26 +1,11 @@
1
- require "pry-rails/version"
1
+ # encoding: UTF-8
2
2
 
3
- module PryRails
4
- begin
5
- require 'pry'
3
+ require 'pry'
4
+ require 'pry-rails/version'
6
5
 
7
- if (defined?(::Rails::Console) and ::Rails::VERSION::MAJOR >= 3)
8
- class Railtie < ::Rails::Railtie
9
- silence_warnings do
10
- ::Rails::Console::IRB = Pry
6
+ if defined?(Rails)
7
+ require 'pry-rails/railtie'
8
+ require "pry-rails/commands"
11
9
 
12
- unless defined?(Pry::ExtendCommandBundle)
13
- Pry::ExtendCommandBundle = Module.new
14
- end
15
-
16
- if ::Rails::VERSION::MINOR >= 2
17
- require "rails/console/app"
18
- require "rails/console/helpers"
19
- TOPLEVEL_BINDING.eval('self').extend ::Rails::ConsoleMethods
20
- end
21
- end
22
- end
23
- end
24
- rescue LoadError
25
- end
10
+ Pry.commands.import PryRails::Commands
26
11
  end
@@ -0,0 +1,160 @@
1
+ module PryRails
2
+ Commands = Pry::CommandSet.new do
3
+ create_command "show-routes", "Print out all defined routes in match order, with names." do
4
+ group "Rails"
5
+
6
+ def options(opt)
7
+ opt.banner unindent <<-USAGE
8
+ Usage: show-routes [-G]
9
+
10
+ show-routes displays the current Rails app's routes.
11
+ USAGE
12
+
13
+ opt.on :G, "grep", "Filter output by regular expression", :argument => true
14
+ end
15
+
16
+ def process
17
+ Rails.application.reload_routes!
18
+ all_routes = Rails.application.routes.routes
19
+
20
+ all_routes = begin
21
+ begin
22
+ # rails 4
23
+ require 'action_dispatch/routing/inspector'
24
+ inspector = ActionDispatch::Routing::RoutesInspector.new
25
+ rescue LoadError => e
26
+ # rails 3.2
27
+ require 'rails/application/route_inspector'
28
+ inspector = Rails::Application::RouteInspector.new
29
+ end
30
+ inspector.format(all_routes)
31
+ rescue LoadError => e
32
+ # rails 3.0 and 3.1. cribbed from
33
+ # https://github.com/rails/rails/blob/3-1-stable/railties/lib/rails/tasks/routes.rake
34
+ routes = all_routes.collect do |route|
35
+
36
+ reqs = route.requirements.dup
37
+ reqs[:to] = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/
38
+ reqs = reqs.empty? ? "" : reqs.inspect
39
+
40
+ {:name => route.name.to_s, :verb => route.verb.to_s, :path => route.path, :reqs => reqs}
41
+ end
42
+
43
+ # Skip the route if it's internal info route
44
+ routes.reject! { |r| r[:path] =~ %r{/rails/info/properties|^/assets} }
45
+
46
+ name_width = routes.map{ |r| r[:name].length }.max
47
+ verb_width = routes.map{ |r| r[:verb].length }.max
48
+ path_width = routes.map{ |r| r[:path].length }.max
49
+
50
+ routes.map do |r|
51
+ "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}"
52
+ end
53
+ end
54
+
55
+ output.puts all_routes.grep(Regexp.new(opts[:G] || ".")).join "\n"
56
+ end
57
+ end
58
+
59
+ create_command "show-models", "Print out all defined models, with attribrutes." do
60
+ group "Rails"
61
+
62
+ def options(opt)
63
+ opt.banner unindent <<-USAGE
64
+ Usage: show-models
65
+
66
+ show-models displays the current Rails app's models.
67
+ USAGE
68
+
69
+ opt.on :G, "grep", "Color output red by regular expression", :argument => true
70
+ end
71
+
72
+ def process
73
+ Rails.application.eager_load!
74
+
75
+ models = ActiveRecord::Base.descendants.map do |mod|
76
+ model_string = mod.to_s + "\n"
77
+ if mod.table_exists?
78
+ model_string << mod.columns.map { |col| " #{col.name}: #{col.type.to_s}" }.join("\n")
79
+ else
80
+ model_string << " Table doesn't exist"
81
+ end
82
+ mod.reflections.each do |model,ref|
83
+ model_string << "\n #{ref.macro.to_s} #{model}"
84
+ model_string << " through #{ref.options[:through]}" unless ref.options[:through].nil?
85
+ end
86
+ model_string
87
+ end.join("\n")
88
+
89
+ models.gsub!(Regexp.new(opts[:G] || ".", Regexp::IGNORECASE)) { |s| text.red(s) } unless opts[:G].nil?
90
+
91
+ output.puts models
92
+ end
93
+ end
94
+
95
+ create_command "show-middleware" do
96
+ group "Rails"
97
+
98
+ def options(opt)
99
+ opt.banner unindent <<-USAGE
100
+ Usage: show-middleware [-G]
101
+
102
+ show-middleware shows the Rails app's middleware.
103
+
104
+ If this pry REPL is attached to a Rails server, the entire middleware
105
+ stack is displayed. Otherwise, only the middleware Rails knows about is
106
+ printed.
107
+ USAGE
108
+
109
+ opt.on :G, "grep", "Filter output by regular expression", :argument => true
110
+ end
111
+
112
+ def process
113
+ # assumes there is only one Rack::Server instance
114
+ server = nil
115
+ ObjectSpace.each_object(Rack::Server) do |object|
116
+ server = object
117
+ end
118
+
119
+ middlewares = []
120
+
121
+ if server
122
+ stack = server.instance_variable_get("@wrapped_app")
123
+ middlewares << stack.class.to_s
124
+
125
+ while stack.instance_variable_defined?("@app") do
126
+ stack = stack.instance_variable_get("@app")
127
+ # Rails 3.0 uses the Application class rather than the application
128
+ # instance itself, so we grab the instance.
129
+ stack = Rails.application if stack == Rails.application.class
130
+ middlewares << stack.class.to_s if stack != Rails.application
131
+ end
132
+ else
133
+ middleware_names = Rails.application.middleware.map do |middleware|
134
+ # After Rails 3.0, the middleware are wrapped in a special class
135
+ # that responds to #name.
136
+ if middleware.respond_to?(:name)
137
+ middleware.name
138
+ else
139
+ middleware.inspect
140
+ end
141
+ end
142
+ middlewares.concat middleware_names
143
+ end
144
+ middlewares << Rails.application.class.to_s
145
+ print_middleware middlewares.grep(Regexp.new(opts[:G] || "."))
146
+ end
147
+
148
+ def print_middleware(middlewares)
149
+ middlewares.each do |middleware|
150
+ string = if middleware == Rails.application.class.to_s
151
+ "run #{middleware}.routes"
152
+ else
153
+ "use #{middleware}"
154
+ end
155
+ output.puts string
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: UTF-8
2
+
3
+ module PryRails
4
+ class Railtie < Rails::Railtie
5
+ console do
6
+ if Rails::VERSION::MAJOR == 3
7
+ Rails::Console::IRB = Pry
8
+
9
+ unless defined? Pry::ExtendCommandBundle
10
+ Pry::ExtendCommandBundle = Module.new
11
+ end
12
+ end
13
+
14
+ if Rails::VERSION::MAJOR == 4
15
+ Rails.application.config.console = Pry
16
+ end
17
+
18
+ if (Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR >= 2) ||
19
+ Rails::VERSION::MAJOR == 4
20
+ require "rails/console/app"
21
+ require "rails/console/helpers"
22
+ TOPLEVEL_BINDING.eval('self').extend ::Rails::ConsoleMethods
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  module PryRails
2
- VERSION = "0.1.6"
4
+ VERSION = "0.2.0"
3
5
  end
@@ -18,8 +18,6 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- # specify any dependencies here; for example:
22
- # s.add_development_dependency "rspec"
23
- # s.add_runtime_dependency "rest-client"
24
21
  s.add_dependency "pry"
22
+ s.add_development_dependency "appraisal"
25
23
  end
@@ -0,0 +1,3 @@
1
+ class Beer < ActiveRecord::Base
2
+ belongs_to :hacker
3
+ end
@@ -0,0 +1,4 @@
1
+ class Hacker < ActiveRecord::Base
2
+ has_many :pokemons
3
+ has_many :beers
4
+ end
@@ -0,0 +1,4 @@
1
+ class Pokemon < ActiveRecord::Base
2
+ belongs_to :hacker
3
+ has_many :beers, through: :hacker
4
+ end
@@ -0,0 +1,4 @@
1
+ App::Application.routes.draw do
2
+ resource :pokemon, :beer
3
+ get 'pry' => proc { binding.pry; [200, {}, ['']] }
4
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pry-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-09 00:00:00.000000000Z
12
+ date: 2012-08-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pry
16
- requirement: &70306781798900 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,28 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70306781798900
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: appraisal
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
25
46
  description:
26
47
  email:
27
48
  - robin@wenglewski.de
@@ -30,12 +51,20 @@ extensions: []
30
51
  extra_rdoc_files: []
31
52
  files:
32
53
  - .gitignore
54
+ - Appraisals
33
55
  - Gemfile
56
+ - LICENCE
34
57
  - Rakefile
35
58
  - Readme.md
36
59
  - lib/pry-rails.rb
60
+ - lib/pry-rails/commands.rb
61
+ - lib/pry-rails/railtie.rb
37
62
  - lib/pry-rails/version.rb
38
63
  - pry-rails.gemspec
64
+ - test/models/beer.rb
65
+ - test/models/hacker.rb
66
+ - test/models/pokemon.rb
67
+ - test/routes.rb
39
68
  homepage: https://github.com/rweng/pry-rails
40
69
  licenses: []
41
70
  post_install_message:
@@ -56,8 +85,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
85
  version: '0'
57
86
  requirements: []
58
87
  rubyforge_project:
59
- rubygems_version: 1.8.15
88
+ rubygems_version: 1.8.24
60
89
  signing_key:
61
90
  specification_version: 3
62
91
  summary: Use Pry as your rails console
63
- test_files: []
92
+ test_files:
93
+ - test/models/beer.rb
94
+ - test/models/hacker.rb
95
+ - test/models/pokemon.rb
96
+ - test/routes.rb