duck_puncher 2.2.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6a46a207adebfd5c36e0f4d5dc51e9dd3b02d5a5
4
- data.tar.gz: ad067c6ed3a67e38f95ff2185d51fffe2a9c11d2
3
+ metadata.gz: ee22023f5904a77f13fdc09ac72eefa9170938ea
4
+ data.tar.gz: 28fb5cc8000d7de6240b6cf6c41e476861c4e255
5
5
  SHA512:
6
- metadata.gz: 61703bbb58544430f7bfe069abd9f4433d5c886d835864c1bcdbe0bcd6f889a33421cd91e0c0193e11640fd3fb93fb8b38b088d5521c0d1db4a7e5c69f29017e
7
- data.tar.gz: 7b2d75382bbd9a1395da6736023077e95691d3a5df02f6129b918eca944f1ddafbce4a3ec7e92cf1a64ca94a9a7e6e05757b7accee1e9ea941d7bbca60839f60
6
+ metadata.gz: fb0141394a399c457e4650f607ed66c7c924aa9c7e15030faafd1c20a18d812b620bc13864bcda8904720aa1218f65d915fdf8601af4e6dc4c412ebb722a0523
7
+ data.tar.gz: 9f86ea17b513e5e3e0e4da59c6665c398e6f26b8229b9d58f5f012578147577ad7755ef1775626f75ab69063159abf45b00acb03c323ff583905c52c2fbe98ec
data/README.md CHANGED
@@ -27,6 +27,7 @@ These are the ducks I can punch:
27
27
  >> Pry.start
28
28
  [1] pry(main)>
29
29
  Method#to_instruct => `Benchmark.method(:measure).to_instruct` returns the Ruby VM instruction sequence for the method
30
+ Method#to_source => `Benchmark.method(:measure).to_source` returns the method definition as a string
30
31
 
31
32
 
32
33
  ## Install
data/bin/console CHANGED
@@ -4,7 +4,7 @@ require 'bundler/setup'
4
4
  require 'pp'
5
5
  require 'duck_puncher'
6
6
  require 'irb'
7
-
7
+ require_relative '../test/fixtures/wut'
8
8
  DuckPuncher.punch_all!
9
9
 
10
10
  IRB.start
@@ -5,6 +5,10 @@ module DuckPuncher
5
5
  RubyVM::InstructionSequence.new(definition.lines.join).disasm if definition.lines.any?
6
6
  end
7
7
 
8
+ def to_source
9
+ Definition.new(self).to_s
10
+ end
11
+
8
12
  class Definition
9
13
  def initialize(method_handle)
10
14
  @file_path, @line_num = *method_handle.source_location
@@ -34,6 +38,14 @@ module DuckPuncher
34
38
  @lines
35
39
  end
36
40
 
41
+ def to_s
42
+ if lines.any?
43
+ lines.join.gsub /^\s{#{find_indent_size(lines.first)}}/, ''
44
+ else
45
+ ''
46
+ end
47
+ end
48
+
37
49
  def find_indent_size(line)
38
50
  line[/(\s*)/].size
39
51
  end
@@ -1,3 +1,3 @@
1
1
  module DuckPuncher
2
- VERSION = '2.2.0'.freeze
2
+ VERSION = '2.3.0'.freeze
3
3
  end
@@ -9,13 +9,6 @@ class MethodTest < MiniTest::Test
9
9
  @subject = Wut.new
10
10
  end
11
11
 
12
- # Called after every test method runs. Can be used to tear
13
- # down fixture information.
14
-
15
- def teardown
16
- # Do nothing
17
- end
18
-
19
12
  def test_to_instruct
20
13
  assert_match /RubyVM::InstructionSequence:to_a/, @subject.method(:to_a).to_instruct
21
14
  assert_match /newarray/, @subject.method(:to_a).to_instruct
@@ -27,4 +20,12 @@ class MethodTest < MiniTest::Test
27
20
  assert_match /RubyVM::InstructionSequence:to_f/, @subject.method(:to_f).to_instruct
28
21
  assert_match /getconstant\s*:INFINITY/, @subject.method(:to_f).to_instruct
29
22
  end
23
+
24
+ def test_to_source
25
+ assert_equal "def to_a\n ['w'] + ['u'] + ['t']\nend\n", @subject.method(:to_a).to_source
26
+ end
27
+
28
+ def test_to_source_with_no_source
29
+ assert_equal '', @subject.method(:object_id).to_source
30
+ end
30
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duck_puncher
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Buckley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-22 00:00:00.000000000 Z
11
+ date: 2016-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler