modloc 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d828e83257acf60a86178a143df9b1a41d13fd9
4
- data.tar.gz: 152c53cb8b81b6c1ace8752adb33af2468fadea2
3
+ metadata.gz: ca8ae9a543450d386a817f7b42c3e09f575fafc4
4
+ data.tar.gz: 8db499218ab780294c1854bbe19ade874458ce7d
5
5
  SHA512:
6
- metadata.gz: c485f555ccb94195fff4997bc77c5459b449bde83b646715845049461bb9e7906ffc59c77a16f8a264e714df18ea0f8cb981cefdd5221c4e9372cb397bf6d6b7
7
- data.tar.gz: d153366efff7711335250d7e7a60cf4009b356cb103fe98efd909952ef2f9c059c78d5a4831c40a6e2f43575df04e68e6d28611f72983e8fb16cc389bfa8b87a
6
+ metadata.gz: 8a5cf4b1e6b4e7c1f29fa31688f46484c0d92c2b151f66fdeb32f0a0860a13892dcdd0823f54cce4672de07fc3333f4b32e49053e9b38f62028c72c6ae21da8a
7
+ data.tar.gz: 677045b9fa24edccc97cf9c7e5c1634d51e5052fd911e89649671623c52e2e846ca591d28683e0176c4d56c3a0db6dab7999133f91e2e4d2b22446d7a632cd63
@@ -0,0 +1,4 @@
1
+ AllCops:
2
+ Excludes:
3
+ - vendor/bundle/**
4
+ - spec/**
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.1
5
+ cache: bundler
data/README.md CHANGED
@@ -1,7 +1,17 @@
1
1
  # Modloc
2
2
 
3
+ [![Version](http://allthebadges.io/jwaldrip/modloc/badge_fury.png)](http://allthebadges.io/jwaldrip/modloc/badge_fury)
4
+ [![Dependencies](http://allthebadges.io/jwaldrip/modloc/gemnasium.png)](http://allthebadges.io/jwaldrip/modloc/gemnasium)
5
+ [![Build Status](http://allthebadges.io/jwaldrip/modloc/travis.png)](http://allthebadges.io/jwaldrip/modloc/travis)
6
+ [![Coverage](http://allthebadges.io/jwaldrip/modloc/coveralls.png)](http://allthebadges.io/jwaldrip/modloc/coveralls)
7
+ [![Code Climate](http://allthebadges.io/jwaldrip/modloc/code_climate.png)](http://allthebadges.io/jwaldrip/modloc/code_climate)
8
+
9
+ --
10
+
3
11
  Source locations for modules and classes in your app.
4
12
 
13
+ **Works in Ruby >= 2.0.0**
14
+
5
15
  ## Installation
6
16
 
7
17
  Add this line to your application's Gemfile:
data/Rakefile CHANGED
@@ -1 +1,8 @@
1
- require "bundler/gem_tasks"
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => [:spec]
@@ -1,5 +1,8 @@
1
- require "modloc/version"
2
- require "modloc/core_ext/module"
1
+ require 'modloc/version'
2
+ require 'modloc/core_ext/module'
3
3
 
4
+ # Find the source location of any module or class
4
5
  module Modloc
6
+ autoload :Source, 'modloc/source'
7
+ autoload :Locator, 'modloc/locator'
5
8
  end
@@ -1,23 +1,8 @@
1
+ # Extensions to ruby's module class
1
2
  class Module
2
-
3
+ # Returns all the source locations for a given module
4
+ # @return [Array]
3
5
  def source_locations
4
- found_files = $LOADED_FEATURES.select do |file|
5
- File.read(file) =~ source_regex rescue nil
6
- end
7
- found_files.map do |file|
8
- bof_match = File.read(file).match(%r{(.*\n)*#{source_regex.source}})
9
- line = bof_match.to_s.lines.size
10
- [file, line]
11
- end
6
+ Modloc::Locator.new(self).find
12
7
  end
13
-
14
- private
15
-
16
- def source_regex
17
- rxp = name.split('::').map do |name|
18
- "(module |class |::)#{name}"
19
- end.join('(\s*#.*\n)*\s*')
20
- %r{#{rxp}}
21
- end
22
-
23
8
  end
@@ -0,0 +1,41 @@
1
+ module Modloc
2
+ # Locator for a given module or class
3
+ class Locator
4
+ # Create a new locator for a module or class
5
+ # @param mod [Module, Class]
6
+ def initialize(mod)
7
+ @mod = mod
8
+ end
9
+
10
+ # Finds all the locations of a given module or class
11
+ # @return [Array]
12
+ def find
13
+ valid_sources.reduce([]) do |locations, source|
14
+ locations + source.locate_all(source_regex)
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def valid_sources
21
+ ruby_files.select { |file| file.strip =~ source_regex }
22
+ end
23
+
24
+ def ruby_files
25
+ files.select { |file| file.ext == '.rb' }
26
+ end
27
+
28
+ def files
29
+ $LOADED_FEATURES.map { |f| Source.new(f) }
30
+ end
31
+
32
+ def source_regex
33
+ rxp = @mod.name.split('::').each_with_index.map do |name, index|
34
+ group = 'module |class '
35
+ group << '|::' unless index == 0
36
+ "(#{group})#{name}"
37
+ end.join('(\s*#.*\n)*\s*')
38
+ /#{rxp}/
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,46 @@
1
+ module Modloc
2
+ # A Modloc source string
3
+ class Source < String
4
+ autoload :Line, 'modloc/source/line'
5
+
6
+ attr_reader :path, :ext
7
+
8
+ # Creates a new Modloc source object
9
+ # @param file [String]
10
+ def initialize(file)
11
+ @ext = File.extname(file)
12
+ @path = file
13
+ super File.read file
14
+ rescue Errno::ENOENT
15
+ super ''
16
+ end
17
+
18
+ # Returns the sources lines as line objects
19
+ # @return [Array<Modloc::Source::Line>]
20
+ def lines
21
+ super.map { |line| Line.new line }
22
+ end
23
+
24
+ # Locate all the locations matching a given pattern
25
+ # @param pattern [Regexp]
26
+ # @return [Array]
27
+ def locate_all(pattern)
28
+ [].tap do |locations|
29
+ strip.lines.each_with_index.reduce('') do |contents, (line, index)|
30
+ line_num = index + 1
31
+ contents << line
32
+ if contents.match(/#{pattern.source}\n\z/)
33
+ locations << [path, line_num]
34
+ end
35
+ contents
36
+ end
37
+ end
38
+ end
39
+
40
+ # Return the source without comments or whitespace for each line
41
+ # @return [Modloc::Source]
42
+ def strip
43
+ dup.replace lines.map(&:strip).join
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,24 @@
1
+ module Modloc
2
+ # A line string
3
+ class Source::Line < String
4
+ # initialize a new line object
5
+ # @param contents [String]
6
+ def initialize(contents)
7
+ super contents
8
+ end
9
+
10
+ # Strip the line of whitespace and comments
11
+ # @return [Modloc::Source::Line]
12
+ def strip
13
+ val = super.concat "\n"
14
+ val.strip_comment
15
+ end
16
+
17
+ protected
18
+
19
+ # Strips a comment from a line
20
+ def strip_comment
21
+ gsub(/#([^{].*\n)?/, "\n")
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,5 @@
1
+ # Modloc
1
2
  module Modloc
2
- VERSION = "0.0.1"
3
+ # Latest Version
4
+ VERSION = '1.0.0'
3
5
  end
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Modloc::VERSION
9
9
  spec.authors = ["Jason Waldrip"]
10
10
  spec.email = ["jason@waldrip.net"]
11
- spec.summary = "Find the source location of a given module or class"
11
+ spec.summary = "Find the source location of any module or class"
12
12
  spec.homepage = ""
13
13
  spec.license = "MIT"
14
14
 
@@ -19,7 +19,10 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_development_dependency "bundler", "~> 1.5"
21
21
  spec.add_development_dependency "rspec", "~> 2.14"
22
- spec.add_development_dependency "activesupport", "~> 4.0"
23
22
  spec.add_development_dependency "pry", "~> 0.9"
24
23
  spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "yard"
25
+ spec.add_development_dependency "inch"
26
+ spec.add_development_dependency "rubocop"
27
+ spec.add_development_dependency "coveralls"
25
28
  end
@@ -0,0 +1,62 @@
1
+ # Top Level
2
+ class Foo
3
+ end
4
+
5
+ module Bar
6
+ end
7
+
8
+ # Nested
9
+ class Foo
10
+ module Baz
11
+ end
12
+ end
13
+
14
+ class Foo
15
+ class Raz
16
+ end
17
+ end
18
+
19
+ module Bar
20
+ module Baz
21
+ end
22
+ end
23
+
24
+ module Bar
25
+ class Raz
26
+ end
27
+ end
28
+
29
+ # Nested With Comments
30
+ class Foo
31
+ # This is a comment
32
+ module Baz
33
+ end
34
+ end
35
+
36
+ class Foo
37
+ # This is a comment
38
+ class Raz
39
+ end
40
+ end
41
+
42
+ module Bar
43
+ # This is a comment
44
+ module Baz
45
+ end
46
+ end
47
+
48
+ module Bar
49
+ # This is a comment
50
+ class Raz
51
+ end
52
+ end
53
+
54
+ # Namespaced
55
+ module Foo::Taz
56
+ end
57
+
58
+ class Bar::Saz
59
+ end
60
+
61
+ # As a comment
62
+ # class Foo
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+ require 'fixtures/sample'
3
+
4
+ describe Module do
5
+
6
+ def sample_with_line(line)
7
+ dir = File.dirname(__FILE__)
8
+ file = File.join dir, '../../../fixtures/sample.rb'
9
+ expect(File.exist? file).to be_true
10
+ file = File.expand_path file
11
+ [file, line]
12
+ end
13
+
14
+ def lines(*lines)
15
+ lines.map do |line|
16
+ sample_with_line(line)
17
+ end.sort
18
+ end
19
+
20
+ describe '#source_locations' do
21
+ context 'for Foo' do
22
+ it 'should return the proper locations' do
23
+ expect(Foo.source_locations)
24
+ .to eq lines(2, 9, 14, 30, 36)
25
+ end
26
+ end
27
+
28
+ context 'for Bar' do
29
+ it 'should return the proper locations' do
30
+ expect(Bar.source_locations)
31
+ .to eq lines(5, 19, 24, 42, 48)
32
+ end
33
+ end
34
+
35
+ context 'for Foo::Baz' do
36
+ it 'should return the proper locations' do
37
+ expect(Foo::Baz.source_locations)
38
+ .to eq lines(10, 32)
39
+ end
40
+ end
41
+
42
+ context 'for Bar::Baz' do
43
+ it 'should return the proper locations' do
44
+ expect(Bar::Baz.source_locations)
45
+ .to eq lines(20, 44)
46
+ end
47
+ end
48
+
49
+ context 'for Foo::Raz' do
50
+ it 'should return the proper locations' do
51
+ expect(Foo::Raz.source_locations)
52
+ .to eq lines(15, 38)
53
+ end
54
+ end
55
+
56
+ context 'for Bar::Raz' do
57
+ it 'should return the proper locations' do
58
+ expect(Bar::Raz.source_locations)
59
+ .to eq lines(25, 50)
60
+ end
61
+ end
62
+
63
+ context 'for Foo::Taz' do
64
+ it 'should return the proper locations' do
65
+ expect(Foo::Taz.source_locations)
66
+ .to eq lines(55)
67
+ end
68
+ end
69
+
70
+ context 'for Boo::Saz' do
71
+ it 'should return the proper locations' do
72
+ expect(Bar::Saz.source_locations)
73
+ .to eq lines(58)
74
+ end
75
+ end
76
+ end
77
+
78
+ end
@@ -1,9 +1,19 @@
1
- # This file was generated by the `rspec --init` command. Conventionally, all
2
- # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
- # Require this file using `require "spec_helper"` to ensure that it is only
4
- # loaded once.
5
- #
6
- # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
1
+ $LOAD_PATH.unshift File.expand_path File.dirname(__FILE__)
2
+
3
+ require 'simplecov'
4
+ require 'coveralls'
5
+ require 'pry'
6
+
7
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
8
+ SimpleCov::Formatter::HTMLFormatter,
9
+ Coveralls::SimpleCov::Formatter
10
+ ]
11
+ SimpleCov.start do
12
+ add_filter 'spec'
13
+ end
14
+
15
+ require 'modloc'
16
+
7
17
  RSpec.configure do |config|
8
18
  config.treat_symbols_as_metadata_keys_with_true_values = true
9
19
  config.run_all_when_everything_filtered = true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modloc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Waldrip
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-26 00:00:00.000000000 Z
11
+ date: 2014-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -39,35 +39,77 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '2.14'
41
41
  - !ruby/object:Gem::Dependency
42
- name: activesupport
42
+ name: pry
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '4.0'
47
+ version: '0.9'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '4.0'
54
+ version: '0.9'
55
55
  - !ruby/object:Gem::Dependency
56
- name: pry
56
+ name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '0.9'
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '0.9'
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rake
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: inch
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: coveralls
71
113
  requirement: !ruby/object:Gem::Requirement
72
114
  requirements:
73
115
  - - ">="
@@ -89,14 +131,21 @@ extra_rdoc_files: []
89
131
  files:
90
132
  - ".gitignore"
91
133
  - ".rspec"
134
+ - ".rubocop.yml"
135
+ - ".travis.yml"
92
136
  - Gemfile
93
137
  - LICENSE.txt
94
138
  - README.md
95
139
  - Rakefile
96
140
  - lib/modloc.rb
97
141
  - lib/modloc/core_ext/module.rb
142
+ - lib/modloc/locator.rb
143
+ - lib/modloc/source.rb
144
+ - lib/modloc/source/line.rb
98
145
  - lib/modloc/version.rb
99
146
  - modloc.gemspec
147
+ - spec/fixtures/sample.rb
148
+ - spec/lib/modloc/core_ext/module_spec.rb
100
149
  - spec/spec_helper.rb
101
150
  homepage: ''
102
151
  licenses:
@@ -121,7 +170,9 @@ rubyforge_project:
121
170
  rubygems_version: 2.2.2
122
171
  signing_key:
123
172
  specification_version: 4
124
- summary: Find the source location of a given module or class
173
+ summary: Find the source location of any module or class
125
174
  test_files:
175
+ - spec/fixtures/sample.rb
176
+ - spec/lib/modloc/core_ext/module_spec.rb
126
177
  - spec/spec_helper.rb
127
178
  has_rdoc: