binding_dumper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e688f7e05163db57719d103f756e5d0ad3b0599b
4
+ data.tar.gz: 17e649afd03e0c544276555bbbaa6d8189badd12
5
+ SHA512:
6
+ metadata.gz: feae8c99272d793699b5e2681de70c0f3ae28d3290c260bc2691d195d284bbe8d18ce9888dbff0c4de779b7d964b6faec9070d97510e775606c5d2bcdaab4652
7
+ data.tar.gz: 6613157503c4958cc4f5a34417a65024ce96bb79aa870b6b772ed6d3b4148a57d9dd23799bd2ed452027fa4891a0965e1ea92e3659e174c88e535057a329ffeb
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .rspec
11
+ .ruby-version
12
+ .ruby-gemset
13
+ .rvmrc
14
+ /spec/dummy/log/*
15
+ /spec/dummy/tmp/*
16
+ /spec/dummy/db/*.sqlite3
17
+ /spec/dummy/binding_dumper
@@ -0,0 +1,13 @@
1
+ env:
2
+ - RAILS_ENV=test
3
+ before_install: gem install bundler -v 1.10.6
4
+ language: ruby
5
+ rvm:
6
+ - 2.2.3
7
+ - 2.2.2
8
+ - 2.1.7
9
+ - 2.1.6
10
+ - 2.0.0
11
+ - ruby-1.9.3-p551
12
+ before_script:
13
+ - bin/dummy_rake db:create db:migrate
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in binding_dumper.gemspec
4
+ gemspec
5
+
6
+ gem 'rails-dummy'
7
+ gem 'sqlite3'
8
+ gem 'pry'
9
+ gem 'rspec-rails'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Ilya Bylich
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,76 @@
1
+ # BindingDumper
2
+
3
+ [![Build Status](https://travis-ci.org/iliabylich/binding_dumper.svg?branch=master)](https://travis-ci.org/iliabylich/binding_dumper)
4
+ [![Code Climate](https://codeclimate.com/github/iliabylich/binding_dumper/badges/gpa.svg)](https://codeclimate.com/github/iliabylich/binding_dumper)
5
+ [![Inline docs](http://inch-ci.org/github/iliabylich/binding_dumper.svg?branch=master)](http://inch-ci.org/github/iliabylich/binding_dumper)
6
+
7
+ A gem for dumping a whole binding and restoring it later. After restoring you can use `pry` to perform delayed debugging.
8
+
9
+ **WARNING** this gem is not ready for production yet, please, use it only in development environment.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'binding_dumper'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install binding_dumper
26
+
27
+ ## Usage
28
+
29
+ To dump a binding run
30
+
31
+ ``` ruby
32
+ binding.dump
33
+ # => "a lot of strange output here, that's fine"
34
+ ```
35
+
36
+ Ideally, you should persist an output somewhere (like in the database). Let's say we have a Rails model called `DumpedBinding` with `data` field:
37
+
38
+ ``` ruby
39
+ StoredBinding.create(data: binding.dump)
40
+ ```
41
+
42
+ Make your server to execute the code above (just put it to any controller's action), go to the console, and run:
43
+ ``` ruby
44
+ b = Bidning.load(StoredBinding.last.data)
45
+ b.pry
46
+ ```
47
+
48
+ And enjoy!
49
+
50
+ ## Requirements
51
+
52
+ + Ruby >= 1.9.3 (see travis.yml for supported versions)
53
+
54
+ ## Examples
55
+
56
+ The simplest one is in the file `test.rb`.
57
+
58
+ A bit more complex example with Rails environment is in `spec/dummy/app/controllers/users_controller.rb`
59
+
60
+ ## Development
61
+
62
+ Clone the repo, run `bundle install`.
63
+
64
+ To run all tests using current ruby version, run `rspec` or `rake`.
65
+
66
+ To run all tests with ALL supported ruby versions, run `bin/multitest` and follow the output.
67
+
68
+ To run dummy app, run `bin/dummy_rails s` (`bin/dummy_rails c` for console).
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it ( https://github.com/[my-github-username]/binding_dumper/fork )
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rails/dummy/tasks'
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: [:spec]
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "binding_dumper"
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
+ Bundler.require
15
+ Pry.start
@@ -0,0 +1,3 @@
1
+ #!/bin/bash
2
+ BIN_PATH="`dirname \"$0\"`"
3
+ $BIN_PATH/../spec/dummy/bin/rails $@
@@ -0,0 +1,4 @@
1
+ #!/bin/bash
2
+ BIN_PATH="`dirname \"$0\"`"
3
+ DUMMY_PATH=$BIN_PATH/../spec/dummy
4
+ $DUMMY_PATH/bin/rake -f $DUMMY_PATH/Rakefile $@
@@ -0,0 +1,32 @@
1
+ #!/bin/bash --login
2
+ # Usage
3
+ #
4
+ # bin/multitest
5
+ # SILENT=true bin/multitest
6
+ #
7
+
8
+ RUBY_VERSIONS=(2.2.3 2.2.2 2.1.7 2.1.6 2.0.0 ruby-1.9.3-p551)
9
+ RUBY_GEMSET=$(cat .ruby-gemset)
10
+
11
+ for CURRENT_RUBY_VERSION in "${RUBY_VERSIONS[@]}"
12
+ do
13
+ echo "Testing with ruby $CURRENT_RUBY_VERSION"
14
+
15
+ if [ -z "$SILENT" ]
16
+ then
17
+ rvm use $CURRENT_RUBY_VERSION --install --binary --fuzzy
18
+ rvm use --create $CURRENT_RUBY_VERSION@$RUBY_GEMSET
19
+
20
+ gem install bundler -v 1.10.6
21
+ bundle install
22
+ else
23
+ rvm use $CURRENT_RUBY_VERSION --install --binary --fuzzy > /dev/null
24
+ rvm use --create $CURRENT_RUBY_VERSION@$RUBY_GEMSET > /dev/null
25
+
26
+ gem install bundler -v 1.10.6 > /dev/null
27
+ bundle install > /dev/null
28
+ fi
29
+
30
+ bin/dummy_rake db:create db:migrate
31
+ rake
32
+ done
@@ -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
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'binding_dumper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "binding_dumper"
8
+ spec.version = BindingDumper::VERSION
9
+ spec.authors = ["Ilya Bylich"]
10
+ spec.email = ["ibylich@gmail.com"]
11
+
12
+ spec.summary = %q{A gem for dumping a whole binding.}
13
+ spec.homepage = "https://github.com/iliabylich/binding_dumper"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.9"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency 'rspec'
24
+ end
@@ -0,0 +1,28 @@
1
+ require "binding_dumper/version"
2
+
3
+ module BindingDumper
4
+ module Dumpers
5
+ autoload :Abstract, 'binding_dumper/dumpers/abstract'
6
+ autoload :PrimitiveDumper, 'binding_dumper/dumpers/primitive_dumper'
7
+ autoload :ArrayDumper, 'binding_dumper/dumpers/array_dumper'
8
+ autoload :HashDumper, 'binding_dumper/dumpers/hash_dumper'
9
+ autoload :ObjectDumper, 'binding_dumper/dumpers/object_dumper'
10
+ autoload :ClassDumper, 'binding_dumper/dumpers/class_dumper'
11
+ autoload :ProcDumper, 'binding_dumper/dumpers/proc_dumper'
12
+ autoload :MagicDumper, 'binding_dumper/dumpers/magic_dumper'
13
+ autoload :ExistingObjectDumper, 'binding_dumper/dumpers/existing_object_dumper'
14
+ end
15
+
16
+ autoload :Memories, 'binding_dumper/memories'
17
+ autoload :MagicObjects, 'binding_dumper/magic_objects'
18
+ autoload :UniversalDumper, 'binding_dumper/universal_dumper'
19
+
20
+ module CoreExt
21
+ autoload :BindingExt, 'binding_dumper/core_ext/binding_ext'
22
+ autoload :LocalBindingPatchBuilder, 'binding_dumper/core_ext/local_binding_patch_builder'
23
+ autoload :MagicContextPatchBuilder, 'binding_dumper/core_ext/magic_context_patch_builder'
24
+ end
25
+ end
26
+
27
+ Binding.send(:include, BindingDumper::CoreExt::BindingExt)
28
+ Binding.send(:extend, BindingDumper::CoreExt::BindingExt::ClassMethods)
@@ -0,0 +1,67 @@
1
+ # Module with patches for Binding class
2
+ #
3
+ # @example
4
+ # dump = binding.dump
5
+ # # => 'string representation of binding'
6
+ # Binding.load(dump)
7
+ # # => #<Binding>
8
+ #
9
+ module BindingDumper
10
+ module CoreExt
11
+ module BindingExt
12
+
13
+ # Returns mapping
14
+ # { local variable name => local variable }
15
+ #
16
+ # @return [Hash]
17
+ #
18
+ def lvars_to_dump
19
+ eval('local_variables').each_with_object({}) do |lvar_name, result|
20
+ result[lvar_name] = eval(lvar_name.to_s)
21
+ end
22
+ end
23
+
24
+ # Returns all the data that represents a context
25
+ # (including context, local variables, file, line and method name)
26
+ #
27
+ # @return [Hash]
28
+ #
29
+ def data_to_dump
30
+ context = eval('self')
31
+ result = {
32
+ context: context,
33
+ method: eval('__method__'),
34
+ file: eval('__FILE__'),
35
+ line: eval('__LINE__'),
36
+ lvars: lvars_to_dump
37
+ }
38
+ end
39
+
40
+ # Dumps a binding and returns a dump
41
+ #
42
+ # @return [String]
43
+ #
44
+ # @yield [String] (if block given)
45
+ #
46
+ def dump(&block)
47
+ dumped = UniversalDumper.dump(data_to_dump)
48
+ block.call(dumped) if block_given?
49
+ dumped
50
+ end
51
+
52
+ module ClassMethods
53
+ # Loads a binding from dump
54
+ #
55
+ # @return [Binding]
56
+ #
57
+ def load(dumped)
58
+ undumped = UniversalDumper.load(dumped)
59
+
60
+ mod = MagicContextPatchBuilder.new(undumped).patch
61
+
62
+ undumped[:context].extend(mod)._local_binding
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,102 @@
1
+ # Class responsible for building patch for local binding
2
+ #
3
+ # @example
4
+ # data = {
5
+ # file: '/path/to/file.rb',
6
+ # line: 17,
7
+ # method: 'do_something'
8
+ # }
9
+ # patch = BindingDumper::CoreExt::LocalBindingPatchBuilder.new(data).patch
10
+ # patched_binding = binding.extend(patch)
11
+ #
12
+ # patched_binding.eval('__FILE__')
13
+ # # => '/path/to/file.rb'
14
+ # patched_binding.eval('__LINE__')
15
+ # # => 17
16
+ # patched_binding.eval('__method__')
17
+ # # => 'do_something'
18
+ #
19
+ class BindingDumper::CoreExt::LocalBindingPatchBuilder
20
+ attr_reader :undumped
21
+
22
+ # @param undumped [Hash]
23
+ #
24
+ def initialize(undumped)
25
+ @undumped = undumped
26
+ end
27
+
28
+ # Returns module that is ready for patching existing binding
29
+ #
30
+ # @return [Module]
31
+ #
32
+ def patch
33
+ deps = [
34
+ file_method_patch,
35
+ line_method_patch,
36
+ method_method_patch,
37
+ eval_method_patch
38
+ ]
39
+ Module.new do
40
+ include *deps
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ # Returns a module with patch for __FILE__ evaluation
47
+ #
48
+ # @return [Module]
49
+ #
50
+ def file_method_patch
51
+ undumped = self.undumped
52
+ Module.new do
53
+ define_method(:_file) { undumped[:file] }
54
+ end
55
+ end
56
+
57
+ # Returns a module with patch for __LINE__ evaluation
58
+ #
59
+ # @return [Module]
60
+ #
61
+ def line_method_patch
62
+ undumped = self.undumped
63
+ Module.new do
64
+ define_method(:_line) { undumped[:line] }
65
+ end
66
+ end
67
+
68
+ # Returns a module with patch for __method__ evaluation
69
+ #
70
+ # @return [Module]
71
+ #
72
+ def method_method_patch
73
+ undumped = self.undumped
74
+ Module.new do
75
+ define_method(:_method) { undumped[:method] }
76
+ end
77
+ end
78
+
79
+ # Returns a module with patch of 'eval' method, so:
80
+ # 1. __FILE__ returns undumped[:file]
81
+ # 2. __LINE__ returns undumped[:line]
82
+ # 3. __method__ returns undumoed[:method]
83
+ #
84
+ # @return [Module]
85
+ #
86
+ def eval_method_patch
87
+ Module.new do
88
+ define_method :eval do |data, *args|
89
+ case data
90
+ when /__FILE__/
91
+ _file
92
+ when /__LINE__/
93
+ _line
94
+ when /__method__/
95
+ _method
96
+ else
97
+ Binding.instance_method(:eval).bind(self).call(data, *args)
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end