source_code 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 080857cf33712695b75867d48f5d7a7cb91d5625
4
+ data.tar.gz: a156f094ec57b044ca6cc38b71596c14f96154b9
5
+ SHA512:
6
+ metadata.gz: f0ae5a82f3699aca26b670ca4c816af8b5312eefc8a1b46eb511d7657f0fd586bca49e6382d2791769393a0fb787e4164e7681b0f4e9c5df1c2fe81cbcdb006a
7
+ data.tar.gz: 826c4981f89378dc8ef28c525d9708720257e5c9cec5740f90c804ec7fa0584ba438e7947afc875fe66227a55bcf23274252556a45ee633fddb75275957ad5c4
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in source_code.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Darren Cauthon
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # SourceCode
2
+
3
+ Adds a ```source_code``` method to Method. This will allow you to view the source code of any Ruby method.
4
+
5
+ So let's say you have a method in a class like this:
6
+
7
+ ```ruby
8
+ class FancyClass
9
+
10
+ def do_something
11
+ 1 + 1
12
+ end
13
+
14
+ end
15
+ ```
16
+
17
+ You've always been able to get a pointer to the method's source code like this:
18
+
19
+ ```ruby
20
+ FancyClass.new.method(:do_something).source_location
21
+ # ['fancy_class.rb', 3]
22
+ ```
23
+
24
+ This gem adds ```.source_code```, so you can see the source like this:
25
+
26
+ ```ruby
27
+ puts FancyClass.new.method(:do_something).source_code
28
+ # def do_something
29
+ # 1 + 1
30
+ # end
31
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,44 @@
1
+ module SourceCode
2
+
3
+ class Method
4
+
5
+ attr_reader :source_location
6
+
7
+ def initialize source_location
8
+ @source_location = source_location
9
+ end
10
+
11
+ def source_code
12
+ lines[first_line...last_line].join
13
+ end
14
+
15
+ private
16
+
17
+ def file
18
+ source_location[0]
19
+ end
20
+
21
+ def lines
22
+ @lines ||= File.read(file).lines
23
+ end
24
+
25
+ def first_line
26
+ source_location[1] - 1
27
+ end
28
+
29
+ def last_line
30
+ index = first_line
31
+ loop do
32
+ index += 1
33
+ break unless indentation_on(first_line) != indentation_on(index)
34
+ end
35
+ index + 1
36
+ end
37
+
38
+ def indentation_on line_number
39
+ lines[line_number].length - lines[line_number].lstrip.length
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,5 @@
1
+ class Method
2
+ def source_code
3
+ SourceCode.extract_source_for source_location
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module SourceCode
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,11 @@
1
+ require_relative "source_code/method"
2
+ require_relative "source_code/monkey_patch"
3
+ require_relative "source_code/version"
4
+
5
+ module SourceCode
6
+
7
+ def self.extract_source_for source_location
8
+ Method.new(source_location).source_code
9
+ end
10
+
11
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'source_code/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "source_code"
8
+ spec.version = SourceCode::VERSION
9
+ spec.authors = ["Darren Cauthon"]
10
+ spec.email = ["darren@cauthon.com"]
11
+ spec.summary = %q{Add a source_location to method, allowing you to see the source code.}
12
+ spec.description = %q{Add a source_location to method, allowing you to see the source code.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,58 @@
1
+ require_relative '../spec_helper'
2
+
3
+ class AClassForTesting
4
+ def hey
5
+ 1 + 1
6
+ end
7
+ end
8
+
9
+ class AnotherFileForTesting
10
+ def do_some_math
11
+ 1 + 1
12
+ 2 + 3
13
+ 4 + 5
14
+ 5 + 10
15
+ end
16
+ end
17
+
18
+ describe :source_location do
19
+
20
+ describe "first example" do
21
+ let(:method) { AClassForTesting.new.method(:hey) }
22
+
23
+ it "should monkey-patch source_code on to Method" do
24
+ method.respond_to?(:source_code).must_equal true
25
+ end
26
+
27
+ it "should report back the contents of the method" do
28
+ method.source_code.must_equal <<EOF
29
+ def hey
30
+ 1 + 1
31
+ end
32
+ EOF
33
+ end
34
+
35
+ end
36
+
37
+ describe "second example" do
38
+
39
+ let(:method) { AnotherFileForTesting.new.method(:do_some_math) }
40
+
41
+ it "should monkey-patch source_code on to Method" do
42
+ method.respond_to?(:source_code).must_equal true
43
+ end
44
+
45
+ it "should report back the contents of the method" do
46
+ method.source_code.must_equal <<EOF
47
+ def do_some_math
48
+ 1 + 1
49
+ 2 + 3
50
+ 4 + 5
51
+ 5 + 10
52
+ end
53
+ EOF
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/spec'
3
+ require_relative '../lib/source_code'
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: source_code
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Darren Cauthon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Add a source_location to method, allowing you to see the source code.
42
+ email:
43
+ - darren@cauthon.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/source_code.rb
54
+ - lib/source_code/method.rb
55
+ - lib/source_code/monkey_patch.rb
56
+ - lib/source_code/version.rb
57
+ - source_code.gemspec
58
+ - spec/source_code/monkey_patch_spec.rb
59
+ - spec/spec_helper.rb
60
+ homepage: ''
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.4.5
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Add a source_location to method, allowing you to see the source code.
84
+ test_files:
85
+ - spec/source_code/monkey_patch_spec.rb
86
+ - spec/spec_helper.rb