mirrors 0.0.2 → 0.0.3

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: 5070f81b2957183b7a938a7b5c2a24e5976f54a3
4
- data.tar.gz: 77f382304b1be839f760abb11395927dc1fb9daf
3
+ metadata.gz: c078859b6c16e0d8d32196f080950c37cb5a5d54
4
+ data.tar.gz: 8c1539195958b54ef2e27db89720e04977a29069
5
5
  SHA512:
6
- metadata.gz: 498e40e9a644d1c2e180a7a87c152066c65a486a837e2d01d07967a41c4320d83d4ce102fa85a27fb62738a5dae075101b1c426bab46467b70278c63b952247c
7
- data.tar.gz: afc5881579ef65d39fe53b34427a86b0c73bbf8805979208b702b051a9c89e427e10ae6517431f77c07c05fb0cd2d8ce8dea022dafb131042910de47f042efbc
6
+ metadata.gz: 1e55a49c465000dcb7d621b70ecabc34edb3dfa1339aa522acb01b2f9817df9db8adea1de824c1502f79fde220b23b8fea25bfe89e7da052602c4b073100a6f4
7
+ data.tar.gz: df2d489ec4fedba86faf0e8a6670e9ea24c704268089cf20eb3e61466c53c8bb1cf30ab18cd321dcd6e83c22ef609c5e29a99fbff26aa823105f225e6eb54deb
@@ -17,6 +17,18 @@ module Mirrors
17
17
  @watches = {}
18
18
  @logger = Logger.new(STDOUT)
19
19
 
20
+ ProjectRootNotFound = Class.new(StandardError)
21
+
22
+ def project_root
23
+ if defined?(Bundler)
24
+ return File.expand_path(Bundler.root)
25
+ end
26
+ if Dir.exist?('.git')
27
+ return File.expand_path(Dir.pwd)
28
+ end
29
+ raise ProjectRootNotFound
30
+ end
31
+
20
32
  def packages
21
33
  packages = {}
22
34
  # Object is the top-level.
@@ -120,12 +132,12 @@ module Mirrors
120
132
 
121
133
  # find the class of obj
122
134
  def basic_class(obj)
123
- Kernel.instance_method(:class).bind(obj).call
135
+ Mirrors.kernel_instance_invoke(obj, :class)
124
136
  end
125
137
 
126
138
  # find the class name of obj
127
139
  def basic_class_name(klass)
128
- Class.instance_method(:name).bind(klass).call
140
+ Mirrors.module_instance_invoke(obj, :name)
129
141
  end
130
142
 
131
143
  def intern_class_mirror(mirror)
@@ -5,7 +5,7 @@ class Class
5
5
  Mirrors.reflect(method(symbol))
6
6
  end
7
7
 
8
- def reflect
9
- Mirrors.reflect(self)
10
- end
8
+ # def mirror
9
+ # Mirrors.reflect(self)
10
+ # end
11
11
  end
@@ -10,6 +10,7 @@ module Mirrors
10
10
 
11
11
  @unbound_module_instance_methods = {}
12
12
  @unbound_class_singleton_methods = {}
13
+ @unbound_kernel_instance_methods = {}
13
14
 
14
15
  def self.module_instance_invoke(receiver, msg)
15
16
  module_instance_method(msg).bind(receiver).call
@@ -26,4 +27,12 @@ module Mirrors
26
27
  def self.class_singleton_method(msg)
27
28
  @unbound_class_singleton_methods[msg] ||= Class.method(msg).unbind
28
29
  end
30
+
31
+ def self.kernel_instance_invoke(receiver, msg)
32
+ kernel_instance_method(msg).bind(receiver).call
33
+ end
34
+
35
+ def self.kernel_instance_method(msg)
36
+ @unbound_kernel_instance_methods[msg] ||= Kernel.instance_method(msg)
37
+ end
29
38
  end
@@ -1,8 +1,8 @@
1
1
  require 'rbconfig'
2
2
  require 'set'
3
3
 
4
- require 'mirrors/hook'
5
4
  require 'mirrors/package_inference/class_to_file_resolver'
5
+ require 'mirrors/invoke'
6
6
 
7
7
  module Mirrors
8
8
  module PackageInference
@@ -121,9 +121,6 @@ module Mirrors
121
121
  end
122
122
 
123
123
  def determine_filename(key, resolver)
124
- if fn = CLASS_DEFINITION_POINTS[key]
125
- return fn
126
- end
127
124
  resolver.resolve(Object.const_get(key))
128
125
  end
129
126
 
@@ -1,3 +1,5 @@
1
+ require 'mirrors/invoke'
2
+
1
3
  module Mirrors
2
4
  module PackageInference
3
5
  class ClassToFileResolver
@@ -7,9 +9,18 @@ module Mirrors
7
9
 
8
10
  def resolve(klass)
9
11
  return nil if klass.nil?
10
- try_fast(klass, klass.name) ||
11
- try_fast(klass.singleton_class, klass.name) ||
12
- try_slow(klass) ||
12
+
13
+ name = begin
14
+ Mirrors.module_instance_invoke(klass, :name)
15
+ rescue TypeError
16
+ # klass is not a class/module, so we can't really determine its
17
+ # origin.
18
+ return nil
19
+ end
20
+
21
+ try_fast(klass, name) ||
22
+ try_fast(klass.singleton_class, name) ||
23
+ try_slow(klass) ||
13
24
  try_slow(klass.singleton_class)
14
25
  end
15
26
 
@@ -47,6 +58,9 @@ module Mirrors
47
58
  meth.source =~ /\A\s+def (self\.)?#{Regexp.quote(meth.name)}/
48
59
  rescue MethodSource::SourceNotFoundError
49
60
  false
61
+ rescue NoMethodError => e
62
+ STDERR.puts "\x1b[31mbug in method_source for #{meth}: #{e.inspect}\x1b[0m"
63
+ false
50
64
  end
51
65
  end
52
66
 
@@ -3,6 +3,10 @@ require 'mirrors/package_inference'
3
3
 
4
4
  module Mirrors
5
5
  class PackageMirror < Mirror
6
+ def self.reflect(name)
7
+ new(name)
8
+ end
9
+
6
10
  def name
7
11
  @subject.sub(/.*:/, '')
8
12
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'mirrors'
3
- s.version = '0.0.2'
3
+ s.version = '0.0.3'
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.licenses = ['MIT']
6
6
  s.authors = ['Burke Libbey']
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mirrors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Burke Libbey
@@ -79,7 +79,6 @@ files:
79
79
  - lib/mirrors/field_mirror/class_variable_mirror.rb
80
80
  - lib/mirrors/field_mirror/constant_mirror.rb
81
81
  - lib/mirrors/field_mirror/instance_variable_mirror.rb
82
- - lib/mirrors/hook.rb
83
82
  - lib/mirrors/index/indexer.rb
84
83
  - lib/mirrors/index/marker.rb
85
84
  - lib/mirrors/invoke.rb
@@ -1,33 +0,0 @@
1
- require 'mirrors/invoke'
2
-
3
- module Mirrors
4
- CLASS_DEFINITION_POINTS = {}
5
-
6
- NoGemfile = Class.new(StandardError)
7
-
8
- class << self
9
- attr_accessor :project_root
10
- end
11
- end
12
-
13
- begin
14
- unless Mirrors.project_root
15
- appline = caller.detect { |line| line !~ /:in `require'/ }
16
- f = File.expand_path(appline.sub(/:\d.*/, ''))
17
- Mirrors.project_root = loop do
18
- f = File.dirname(f)
19
- raise Mirrors::NoGemfile if f == '/'
20
- break f if File.exist?("#{f}/Gemfile")
21
- end
22
- end
23
- end
24
-
25
- class Class
26
- alias_method :__lg_orig_inherited, :inherited
27
- def inherited(subclass)
28
- file = caller[0].sub(/:\d+:in.*/, '')
29
- key = Mirrors.module_instance_invoke(subclass, :inspect)
30
- Mirrors::CLASS_DEFINITION_POINTS[key] = file
31
- __lg_orig_inherited(subclass)
32
- end
33
- end