where_is 0.1.0 → 0.2.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: a012ba070db98cb6a10ef134a01fc5be8b17da90
4
- data.tar.gz: 740bb63ccd485920eefd11928cb52a1bddc0c81c
3
+ metadata.gz: f306ef08ef7d1741687639fb23e8fd2c548aa596
4
+ data.tar.gz: d192d9414cede7645d027df092051882a0b71598
5
5
  SHA512:
6
- metadata.gz: 8cf7fd43576061fbd78b9d90ffbe940a95f36fa245932e06415531e2fc3b6624353c8a263cf086cf4aa8d8e1da657e8beba70e029736c90eb78de16443e6172f
7
- data.tar.gz: c2ba717c045b4e09121fcf621df53e8ec1437a989e332071ab3d62bb8ad672a910222cb29dd0c1c62ee1e9b384a760f24074765b10a2f0888cef7489756b1a3d
6
+ metadata.gz: 574345e79412de82a51dbf8df6c4391931b283854e7eccf869be8a051ea295ab39a77c32b56d3c1083839b7e25d6d7102fe9e975eb805f264c7f54bd723fd07f
7
+ data.tar.gz: 7dcbd32e3a79404686153d5582fa13b9db90e110098090a0bf0ec16109b99da493c8883aa3c70e3fceb883a97922897d599ca6d00e9c9f4c14061a425cb87cad
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+
4
+ Style/PredicateName:
5
+ Enabled: false
6
+
7
+ Style/Documentation:
8
+ Enabled: false
data/README.md CHANGED
@@ -24,10 +24,25 @@ Or install it yourself as:
24
24
  # Where.is takes a class and an optional method
25
25
 
26
26
  Where.is(Where, :is)
27
- # => ["/Users/david/.rvm/gems/ruby-2.2.3/gems/where_is-0.1.0/lib/where_is.rb", 5]
27
+ # => ["/Users/david/.rvm/gems/ruby-2.3.1/gems/where_is-0.1.0/lib/where_is.rb", 5]
28
28
 
29
29
  Where.is(IRB)
30
- # => ["/Users/david/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/irb/inspector.rb", 23]
30
+ # => ["/Users/david/.rvm/rubies/ruby-2.3.1/lib/ruby/2.2.0/irb/inspector.rb", 24]
31
+
32
+
33
+ # Where.are takes a class and an optional method and will return all definitions
34
+ # of the method/class
35
+ Where.are(Where, :is)
36
+
37
+
38
+ Where.are(IRB)
39
+ # => [
40
+ # ["/Users/david/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/irb/inspector.rb", 24],
41
+ # ["/Users/david/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/irb/workspace.rb", 112],
42
+ # ["(eval)", 2],
43
+ # ["/Users/david/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/irb.rb", 350],
44
+ # ["/Users/david/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/irb/init.rb", 16]
45
+ #]
31
46
  ```
32
47
 
33
48
  ## Development
data/Rakefile CHANGED
@@ -1,2 +1,6 @@
1
- require "bundler/gem_tasks"
2
- task :default => :spec
1
+ require 'bundler/gem_tasks'
2
+ require 'rubocop/rake_task'
3
+
4
+ task default: :rubocop
5
+
6
+ RuboCop::RakeTask.new(:rubocop)
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "where_is"
3
+ require 'bundler/setup'
4
+ require 'where_is'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "where_is"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start
@@ -1,3 +1,3 @@
1
1
  module Where
2
- VERSION = "0.1.0"
2
+ VERSION = '0.2.0'.freeze
3
3
  end
data/lib/where_is.rb CHANGED
@@ -1,7 +1,7 @@
1
- require "where_is/version"
1
+ require 'where_is/version'
2
2
 
3
3
  module Where
4
- class <<self
4
+ class << self
5
5
  def is(klass, method = nil)
6
6
  if method
7
7
  begin
@@ -14,6 +14,18 @@ module Where
14
14
  end
15
15
  end
16
16
 
17
+ def are(klass, method = nil)
18
+ if method
19
+ begin
20
+ Where.are_instance_methods(klass, method)
21
+ rescue NameError
22
+ Where.are_methods(klass, method)
23
+ end
24
+ else
25
+ Where.is_class(klass)
26
+ end
27
+ end
28
+
17
29
  def is_proc(proc)
18
30
  source_location(proc)
19
31
  end
@@ -27,62 +39,79 @@ module Where
27
39
  end
28
40
 
29
41
  def are_methods(klass, method_name)
30
- are_via_extractor(:method, klass, method_name)
42
+ methods = are_via_extractor(:method, klass, method_name)
43
+ source_locations = group_and_combine_source_locations(methods)
44
+
45
+ if source_locations.empty?
46
+ raise NameError, "#{klass} has no methods called #{method_name}"
47
+ end
48
+
49
+ source_locations
31
50
  end
32
51
 
33
52
  def are_instance_methods(klass, method_name)
34
- are_via_extractor(:method, klass, method_name)
53
+ methods = are_via_extractor(:instance_method, klass, method_name)
54
+ source_locations = group_and_combine_source_locations(methods)
55
+
56
+ if source_locations.empty?
57
+ raise NameError, "#{klass} has no methods called #{method_name}"
58
+ end
59
+
60
+ source_locations
35
61
  end
36
62
 
37
63
  def is_class(klass)
38
64
  methods = defined_methods(klass)
39
- file_groups = methods.group_by{|sl| sl[0]}
40
- file_counts = file_groups.map do |file, sls|
41
- lines = sls.map{|sl| sl[1]}
42
- count = lines.size
43
- line = lines.min
44
- {file: file, count: count, line: line}
65
+ source_locations = group_and_combine_source_locations(methods)
66
+
67
+ if source_locations.empty?
68
+ raise NameError, "#{klass} has no methods" if methods.empty?
69
+ raise NameError, "#{klass} only has built-in methods " \
70
+ "(#{methods.size} in total)"
45
71
  end
46
- file_counts.sort_by{|fc| fc[:count]}.map{|fc| [fc[:file], fc[:line]]}
72
+
73
+ source_locations
47
74
  end
48
75
 
49
- # Raises ArgumentError if klass does not have any Ruby methods defined in it.
50
76
  def is_class_primarily(klass)
51
- source_locations = is_class(klass)
52
- if source_locations.empty?
53
- methods = defined_methods(klass)
54
- raise ArgumentError, (methods.empty? ?
55
- "#{klass} has no methods" :
56
- "#{klass} only has built-in methods (#{methods.size} in total)"
57
- )
58
- end
59
- source_locations[0]
77
+ is_class(klass)[0]
60
78
  end
61
79
 
62
80
  private
63
81
 
64
82
  def source_location(method)
65
- method.source_location || (
66
- method.to_s =~ /: (.*)>/
67
- $1
68
- )
83
+ method.source_location || method.to_s[/: (.*)>/, 1]
84
+ end
85
+
86
+ def group_and_combine_source_locations(source_locations)
87
+ file_groups = source_locations.group_by { |src_loc| src_loc[0] }.to_a
88
+
89
+ file_groups.map! do |file, src_locs|
90
+ lines = src_locs.map { |sl| sl[1] }
91
+ count = lines.size
92
+ line = lines.min
93
+ { file: file, count: count, line: line }
94
+ end
95
+
96
+ file_groups.sort_by! { |fc| fc[:count] }
97
+ file_groups.map { |fc| [fc[:file], fc[:line]] }
69
98
  end
70
99
 
71
100
  def are_via_extractor(extractor, klass, method_name)
72
- methods = klass.ancestors.map do |ancestor|
73
- method = ancestor.send(extractor, method_name)
74
- if method.owner == ancestor
75
- source_location(method)
76
- else
101
+ klass.ancestors.map do |ancestor|
102
+ begin
103
+ source_location(ancestor.send(extractor, method_name))
104
+ rescue NameError
77
105
  nil
78
106
  end
79
- end
80
- methods.compact
107
+ end.compact
81
108
  end
82
109
 
83
110
  def defined_methods(klass)
84
- methods = klass.methods(false).map{|m| klass.method(m)} +
85
- klass.instance_methods(false).map{|m| klass.instance_method(m)}
111
+ methods = klass.methods(false)
112
+ .map { |m| klass.method(m) }
113
+ methods += klass.instance_methods(false)
114
+ .map { |m| klass.instance_method(m) }
86
115
  methods.map(&:source_location).compact
87
116
  end
88
117
  end
data/where_is.gemspec CHANGED
@@ -4,23 +4,26 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'where_is/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "where_is"
7
+ spec.name = 'where_is'
8
8
  spec.version = Where::VERSION
9
- spec.authors = ["Dave Allie"]
10
- spec.email = ["dave@tanda.co"]
9
+ spec.authors = ['Dave Allie']
10
+ spec.email = ['dave@daveallie.com']
11
11
 
12
- spec.summary = %q{A little Ruby gem for finding the source location where class and methods are defined.}
13
- spec.description = %q{A little Ruby gem for finding the source location where class and methods are defined.}
14
- spec.homepage = "https://github.com/daveallie/where_is"
15
- spec.license = "MIT"
12
+ spec.summary = 'A little Ruby gem for finding the source location' \
13
+ ' where class and methods are defined.'
14
+ spec.description = 'A little Ruby gem for finding the source location' \
15
+ ' where class and methods are defined.'
16
+ spec.homepage = 'https://github.com/daveallie/where_is'
17
+ spec.license = 'MIT'
16
18
 
17
19
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
20
  f.match(%r{^(test|spec|features)/})
19
21
  end
20
- spec.bindir = "exe"
22
+ spec.bindir = 'exe'
21
23
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
- spec.require_paths = ["lib"]
24
+ spec.require_paths = ['lib']
23
25
 
24
- spec.add_development_dependency "bundler", "~> 1.13"
25
- spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency 'bundler', '~> 1.13'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rubocop', '~> 0.45.0'
26
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: where_is
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dave Allie
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-04 00:00:00.000000000 Z
11
+ date: 2016-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,15 +38,30 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.45.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.45.0
41
55
  description: A little Ruby gem for finding the source location where class and methods
42
56
  are defined.
43
57
  email:
44
- - dave@tanda.co
58
+ - dave@daveallie.com
45
59
  executables: []
46
60
  extensions: []
47
61
  extra_rdoc_files: []
48
62
  files:
49
63
  - ".gitignore"
64
+ - ".rubocop.yml"
50
65
  - CODE_OF_CONDUCT.md
51
66
  - Gemfile
52
67
  - LICENSE.txt
@@ -77,10 +92,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
92
  version: '0'
78
93
  requirements: []
79
94
  rubyforge_project:
80
- rubygems_version: 2.4.8
95
+ rubygems_version: 2.5.1
81
96
  signing_key:
82
97
  specification_version: 4
83
98
  summary: A little Ruby gem for finding the source location where class and methods
84
99
  are defined.
85
100
  test_files: []
86
- has_rdoc: