dissociated_introspection 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +102 -0
- data/Rakefile +25 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/dissociated_introspection.gemspec +30 -0
- data/lib/dissociated_introspection/active_support.rb +145 -0
- data/lib/dissociated_introspection/eval_sandbox.rb +30 -0
- data/lib/dissociated_introspection/inspection.rb +75 -0
- data/lib/dissociated_introspection/recording_parent.rb +51 -0
- data/lib/dissociated_introspection/ruby_class.rb +110 -0
- data/lib/dissociated_introspection/version.rb +3 -0
- data/lib/dissociated_introspection.rb +7 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 205b26f995b028def8b6f7e9990298bea6499ec9
|
4
|
+
data.tar.gz: 32114c410a315ea1dc522411e32d15a1be83d84a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 42254bffa089cd012a6928b6688fcefed4e7eaf978cef54dde317ec6a92e757fb6c3a5d594b41afce8b034ff5f18ff324b76ea219cba1cc875850aecd92b3244
|
7
|
+
data.tar.gz: 06e325d343beb0fc68ebf1142ed5e088c519c9be1c2a778ececabe67a0df8201ddc322132b0ec5ce5dcf0649dd964b8ac7999c4225d1dfbeff0f231145c450a8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Dustin Zeisler
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# Dissociated Introspection
|
2
|
+
[![Build Status](https://travis-ci.org/zeisler/dissociated_introspection.svg?branch=master)](https://travis-ci.org/zeisler/dissociated_introspection)
|
3
|
+
[![Code Climate](https://codeclimate.com/github/zeisler/dissociated_introspection/badges/gpa.svg)](https://codeclimate.com/github/zeisler/dissociated_introspection)
|
4
|
+
[![Test Coverage](https://codeclimate.com/github/zeisler/dissociated_introspection/badges/coverage.svg)](https://codeclimate.com/github/zeisler/dissociated_introspection/coverage)
|
5
|
+
|
6
|
+
Introspect methods, parameters, class macros, and constants without loading a parent class or any other dependencies.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'dissociated_introspection'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install dissociated_introspection
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
### Static Analysis
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
ruby_class_as_str <<-RUBY
|
30
|
+
class A < B::C
|
31
|
+
def method1(arg)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
RUBY
|
35
|
+
|
36
|
+
ruby_class = DissociatedIntrospection::RubyClass.new(source: ruby_class_as_str)
|
37
|
+
ruby_class.class_name
|
38
|
+
# => "A"
|
39
|
+
|
40
|
+
ruby_class.parent_class_name
|
41
|
+
# => "B::C"
|
42
|
+
|
43
|
+
new_ruby_class = ruby_class.modify_parent_class(:C)
|
44
|
+
new_ruby_class.parent_class_name
|
45
|
+
# => "C"
|
46
|
+
|
47
|
+
new_ruby_class.to_ruby_str
|
48
|
+
# => "class A < C\n def method\n end\nend"
|
49
|
+
|
50
|
+
ruby_class.defs.first.name
|
51
|
+
# => :method1
|
52
|
+
|
53
|
+
ruby_class.defs.first.arguments
|
54
|
+
# => "arg"
|
55
|
+
```
|
56
|
+
|
57
|
+
### Sandboxed Analysis
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
# app/model/user.rb
|
61
|
+
class User < ActiveRecord::Base
|
62
|
+
attr_accessor :baz
|
63
|
+
scope :recent_users, -> { 'some SQL' }
|
64
|
+
include UserHelpers
|
65
|
+
def display_name
|
66
|
+
"#{first_name} #{last_name}"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
ActiveRecord does not need to be loaded, it will be replaced with an alternate.
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
inspection = DissociatedIntrospection::Inspection.new(file: user_model_file)
|
75
|
+
|
76
|
+
inspection.class_macros
|
77
|
+
# => [{ attr_accessor: [[:baz]]},
|
78
|
+
{ scope: [[:recent_users], #<Proc:0x0> ] },
|
79
|
+
{ include: [[#<Module:0x0>]]}]
|
80
|
+
|
81
|
+
inspection.missing_constants
|
82
|
+
# => { UserHelpers: #<Module:0x0> }
|
83
|
+
|
84
|
+
# Removes meta-programmed methods from ActiveRecord
|
85
|
+
inspection.get_class.instance_methods(false)
|
86
|
+
# => [ :method1 ]
|
87
|
+
|
88
|
+
```
|
89
|
+
|
90
|
+
## Development
|
91
|
+
|
92
|
+
Run `bin/console` for an interactive prompt that will allow you to experiment.
|
93
|
+
|
94
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
95
|
+
|
96
|
+
## Contributing
|
97
|
+
|
98
|
+
1. Fork it ( https://github.com/[my-github-username]/dissociated_introspection/fork )
|
99
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
100
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
101
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
102
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
task :default => 'specs'
|
4
|
+
|
5
|
+
task :specs do
|
6
|
+
if defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" && RUBY_VERSION >= "1.9"
|
7
|
+
module Kernel
|
8
|
+
alias :__at_exit :at_exit
|
9
|
+
def at_exit(&block)
|
10
|
+
__at_exit do
|
11
|
+
exit_status = $!.status if $!.is_a?(SystemExit)
|
12
|
+
block.call
|
13
|
+
exit exit_status if exit_status
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
sh "bundle exec rspec --seed #{random_seed}"
|
19
|
+
end
|
20
|
+
|
21
|
+
def random_seed
|
22
|
+
seed = rand(99999)
|
23
|
+
puts "Seed: #{seed}"
|
24
|
+
seed
|
25
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "dissociated_introspection"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dissociated_introspection/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dissociated_introspection"
|
8
|
+
spec.version = DissociatedIntrospection::VERSION
|
9
|
+
spec.authors = ["Dustin Zeisler"]
|
10
|
+
spec.email = ["dustin@zeisler.net"]
|
11
|
+
|
12
|
+
spec.summary = %q{Introspect methods, parameters, class macros, and constants without loading a parent class or any other dependencies.}
|
13
|
+
spec.description = %q{Introspect methods, parameters, class macros, and constants without loading a parent class or any other dependencies.}
|
14
|
+
spec.homepage = "https://github.com/zeisler/dissociated_introspection"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.required_ruby_version = '>= 2.1'
|
23
|
+
|
24
|
+
spec.add_runtime_dependency "parser", "~> 2.2"
|
25
|
+
spec.add_runtime_dependency "unparser", '~> 0.2'
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.3"
|
30
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
module ActiveSupport
|
3
|
+
module Try
|
4
|
+
module Core
|
5
|
+
def try(*a, &b)
|
6
|
+
if a.empty? || respond_to?(a.first)
|
7
|
+
if a.empty? && block_given?
|
8
|
+
if b.arity.zero?
|
9
|
+
instance_eval(&b)
|
10
|
+
else
|
11
|
+
yield self
|
12
|
+
end
|
13
|
+
else
|
14
|
+
public_send(*a, &b)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
refine Object do
|
21
|
+
include Core
|
22
|
+
end
|
23
|
+
|
24
|
+
refine Delegator do
|
25
|
+
include Core
|
26
|
+
end
|
27
|
+
|
28
|
+
refine NilClass do
|
29
|
+
def try(*args)
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Object
|
36
|
+
##
|
37
|
+
# :method: try
|
38
|
+
#
|
39
|
+
# :call-seq:
|
40
|
+
# try(*a, &b)
|
41
|
+
#
|
42
|
+
# Invokes the public method whose name goes as first argument just like
|
43
|
+
# +public_send+ does, except that if the receiver does not respond to it the
|
44
|
+
# call returns +nil+ rather than raising an exception.
|
45
|
+
#
|
46
|
+
# This method is defined to be able to write
|
47
|
+
#
|
48
|
+
# @person.try(:name)
|
49
|
+
#
|
50
|
+
# instead of
|
51
|
+
#
|
52
|
+
# @person.name if @person
|
53
|
+
#
|
54
|
+
# +try+ calls can be chained:
|
55
|
+
#
|
56
|
+
# @person.try(:spouse).try(:name)
|
57
|
+
#
|
58
|
+
# instead of
|
59
|
+
#
|
60
|
+
# @person.spouse.name if @person && @person.spouse
|
61
|
+
#
|
62
|
+
# +try+ will also return +nil+ if the receiver does not respond to the method:
|
63
|
+
#
|
64
|
+
# @person.try(:non_existing_method) # => nil
|
65
|
+
#
|
66
|
+
# instead of
|
67
|
+
#
|
68
|
+
# @person.non_existing_method if @person.respond_to?(:non_existing_method) # => nil
|
69
|
+
#
|
70
|
+
# +try+ returns +nil+ when called on +nil+ regardless of whether it responds
|
71
|
+
# to the method:
|
72
|
+
#
|
73
|
+
# nil.try(:to_i) # => nil, rather than 0
|
74
|
+
#
|
75
|
+
# Arguments and blocks are forwarded to the method if invoked:
|
76
|
+
#
|
77
|
+
# @posts.try(:each_slice, 2) do |a, b|
|
78
|
+
# ...
|
79
|
+
# end
|
80
|
+
#
|
81
|
+
# The number of arguments in the signature must match. If the object responds
|
82
|
+
# to the method the call is attempted and +ArgumentError+ is still raised
|
83
|
+
# in case of argument mismatch.
|
84
|
+
#
|
85
|
+
# If +try+ is called without arguments it yields the receiver to a given
|
86
|
+
# block unless it is +nil+:
|
87
|
+
#
|
88
|
+
# @person.try do |p|
|
89
|
+
# ...
|
90
|
+
# end
|
91
|
+
#
|
92
|
+
# You can also call try with a block without accepting an argument, and the block
|
93
|
+
# will be instance_eval'ed instead:
|
94
|
+
#
|
95
|
+
# @person.try { upcase.truncate(50) }
|
96
|
+
#
|
97
|
+
# Please also note that +try+ is defined on +Object+. Therefore, it won't work
|
98
|
+
# with instances of classes that do not have +Object+ among their ancestors,
|
99
|
+
# like direct subclasses of +BasicObject+.
|
100
|
+
|
101
|
+
##
|
102
|
+
# :method: try!
|
103
|
+
#
|
104
|
+
# :call-seq:
|
105
|
+
# try!(*a, &b)
|
106
|
+
#
|
107
|
+
# Same as #try, but raises a NoMethodError exception if the receiver is
|
108
|
+
# not +nil+ and does not implement the tried method.
|
109
|
+
#
|
110
|
+
# "a".try!(:upcase) # => "A"
|
111
|
+
# nil.try!(:upcase) # => nil
|
112
|
+
# 123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Fixnum
|
113
|
+
end
|
114
|
+
|
115
|
+
class Delegator
|
116
|
+
##
|
117
|
+
# :method: try
|
118
|
+
#
|
119
|
+
# :call-seq:
|
120
|
+
# try(a*, &b)
|
121
|
+
#
|
122
|
+
# See Object#try
|
123
|
+
|
124
|
+
##
|
125
|
+
# :method: try!
|
126
|
+
#
|
127
|
+
# :call-seq:
|
128
|
+
# try!(a*, &b)
|
129
|
+
#
|
130
|
+
# See Object#try!
|
131
|
+
end
|
132
|
+
|
133
|
+
class NilClass
|
134
|
+
# Calling +try+ on +nil+ always returns +nil+.
|
135
|
+
# It becomes especially helpful when navigating through associations that may return +nil+.
|
136
|
+
#
|
137
|
+
# nil.try(:name) # => nil
|
138
|
+
#
|
139
|
+
# Without +try+
|
140
|
+
# @person && @person.children.any? && @person.children.first.name
|
141
|
+
#
|
142
|
+
# With +try+
|
143
|
+
# @person.try(:children).try(:first).try(:name)
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module DissociatedIntrospection
|
2
|
+
class EvalSandbox
|
3
|
+
|
4
|
+
def initialize(file:, recording_parent: recording_parent_default, module_namespace: Module.new)
|
5
|
+
@file = file
|
6
|
+
@recording_parent = recording_parent
|
7
|
+
@module_namespace = module_namespace
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
def call
|
12
|
+
module_namespace.module_eval(recording_parent.read, recording_parent.path)
|
13
|
+
module_namespace.module_eval(file.read, file.path)
|
14
|
+
module_namespace.const_get(module_namespace.constants.last)
|
15
|
+
end
|
16
|
+
|
17
|
+
def constants
|
18
|
+
module_namespace.constants
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
attr_reader :file, :recording_parent, :module_namespace
|
24
|
+
|
25
|
+
def recording_parent_default
|
26
|
+
File.new(File.join(File.dirname(__FILE__), "recording_parent.rb"))
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
module DissociatedIntrospection
|
4
|
+
class Inspection
|
5
|
+
|
6
|
+
def initialize(file:, parent_class_replacement: :RecordingParent)
|
7
|
+
@file = file
|
8
|
+
@parent_class_replacement = parent_class_replacement
|
9
|
+
end
|
10
|
+
|
11
|
+
def get_class
|
12
|
+
@get_class ||= _get_class
|
13
|
+
end
|
14
|
+
|
15
|
+
def class_macros(type=nil)
|
16
|
+
# FIXME
|
17
|
+
return get_class.__missing_class_macros__ if type.nil?
|
18
|
+
end
|
19
|
+
|
20
|
+
def extended_modules
|
21
|
+
find_class_macro_by_type(:extend) { |a| add_method_name_wo_parent a.first }
|
22
|
+
end
|
23
|
+
|
24
|
+
def included_modules
|
25
|
+
find_class_macro_by_type(:include) { |a| add_method_name_wo_parent a.first }
|
26
|
+
end
|
27
|
+
|
28
|
+
def prepend_modules
|
29
|
+
find_class_macro_by_type(:prepend) { |a| add_method_name_wo_parent a.first }
|
30
|
+
end
|
31
|
+
|
32
|
+
def missing_constants
|
33
|
+
get_class.__missing_constants__
|
34
|
+
end
|
35
|
+
|
36
|
+
def parsed_source
|
37
|
+
@parsed_source ||= RubyClass.new(source: file.read)
|
38
|
+
end
|
39
|
+
|
40
|
+
def sandbox_module
|
41
|
+
@sandbox_module ||= Module.new
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def add_method_name_wo_parent(_module)
|
47
|
+
def _module.referenced_name
|
48
|
+
n = name.split("::")
|
49
|
+
return n[2..-1].join("::") if n.first =~ /#<Module:.*>/
|
50
|
+
return n[1..-1].join("::")
|
51
|
+
end
|
52
|
+
_module
|
53
|
+
end
|
54
|
+
|
55
|
+
def find_class_macro_by_type(type)
|
56
|
+
get_class.__missing_class_macros__.select { |h| h.keys.first == type }.map { |h| yield(h.values.first.first) }
|
57
|
+
end
|
58
|
+
|
59
|
+
def _get_class
|
60
|
+
modified_class_source = parsed_source.modify_parent_class(parent_class_replacement)
|
61
|
+
path = if file.is_a? Pathname
|
62
|
+
file.to_s
|
63
|
+
else
|
64
|
+
file.path
|
65
|
+
end
|
66
|
+
load_sandbox(OpenStruct.new(read: modified_class_source.to_ruby_str, path: path))
|
67
|
+
end
|
68
|
+
|
69
|
+
def load_sandbox(file)
|
70
|
+
@klass ||= EvalSandbox.new(file: file, module_namespace: sandbox_module).call
|
71
|
+
end
|
72
|
+
|
73
|
+
attr_reader :parent_class_replacement, :file
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
class RecordingParent < BasicObject
|
2
|
+
|
3
|
+
class << self
|
4
|
+
|
5
|
+
def method_missing(m, *args, &block)
|
6
|
+
__missing_class_macros__.push({m => [args, block].compact})
|
7
|
+
end
|
8
|
+
|
9
|
+
def __missing_class_macros__
|
10
|
+
@__missing_class_macros__ ||= []
|
11
|
+
end
|
12
|
+
|
13
|
+
module ConstMissing
|
14
|
+
def const_missing(const_sym)
|
15
|
+
const = self.const_set(const_sym, Module.new)
|
16
|
+
const.extend ConstMissing
|
17
|
+
const.module_eval(<<-RUBY, __FILE__, __LINE__+1)
|
18
|
+
def self.name
|
19
|
+
"#{name.gsub(/#<Module:.*>::/, '')}::#{const_sym}"
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.inspect
|
23
|
+
name
|
24
|
+
end
|
25
|
+
RUBY
|
26
|
+
RecordingParent.__missing_constants__[const_sym] = const
|
27
|
+
const
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
include ConstMissing
|
32
|
+
|
33
|
+
def __missing_constants__
|
34
|
+
# This file and it's class variables are reinitialized within a new module namespace on every run.
|
35
|
+
@@__missing_constants__ ||= {}
|
36
|
+
end
|
37
|
+
|
38
|
+
def listen_to_defined_macros(*methods)
|
39
|
+
methods.each do |m|
|
40
|
+
module_eval(<<-RUBY, __FILE__)
|
41
|
+
def self.#{m}(*args, &block)
|
42
|
+
__missing_class_macros__.push({ __method__ => [args, block].compact })
|
43
|
+
end
|
44
|
+
RUBY
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
listen_to_defined_macros :attr_reader, :attr_writer, :attr_accessor, :prepend, :include, :extend
|
50
|
+
end
|
51
|
+
|
@@ -0,0 +1,110 @@
|
|
1
|
+
module DissociatedIntrospection
|
2
|
+
class RubyClass
|
3
|
+
using ActiveSupport::Try
|
4
|
+
|
5
|
+
def initialize(source: nil, ast: nil)
|
6
|
+
@source = source
|
7
|
+
@ast = ast
|
8
|
+
if source.nil? && ast.nil?
|
9
|
+
raise ArgumentError.new "#{self.class.name} require either source or ast to be given as named arguments."
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def is_class?
|
14
|
+
ast.type == :class
|
15
|
+
end
|
16
|
+
|
17
|
+
def class_name
|
18
|
+
Unparser.unparse(find_class.to_a[0])
|
19
|
+
end
|
20
|
+
|
21
|
+
def parent_class_name
|
22
|
+
Unparser.unparse(find_class.to_a[1])
|
23
|
+
end
|
24
|
+
|
25
|
+
def has_parent_class?
|
26
|
+
return false if find_class.nil?
|
27
|
+
find_class.to_a[1].try(:type) == :const
|
28
|
+
end
|
29
|
+
|
30
|
+
def change_class_name(class_name)
|
31
|
+
reset_nodes
|
32
|
+
nodes[0] = Parser::CurrentRuby.parse(class_name)
|
33
|
+
new_ast = ast.updated(nil, nodes, nil)
|
34
|
+
self.class.new(ast: new_ast)
|
35
|
+
end
|
36
|
+
|
37
|
+
def modify_parent_class(parent_class)
|
38
|
+
reset_nodes
|
39
|
+
if has_parent_class?
|
40
|
+
class_node = find_class.to_a.dup
|
41
|
+
class_node[1] = Parser::CurrentRuby.parse(parent_class.to_s)
|
42
|
+
new_ast = find_class.updated(nil, class_node, nil)
|
43
|
+
else
|
44
|
+
nodes[1] = nodes[0].updated(:const, [nil, parent_class.to_sym])
|
45
|
+
new_ast = ast.updated(nil, nodes, nil)
|
46
|
+
end
|
47
|
+
self.class.new(ast: new_ast)
|
48
|
+
end
|
49
|
+
|
50
|
+
class Def
|
51
|
+
|
52
|
+
def initialize(ast:)
|
53
|
+
@ast = ast
|
54
|
+
end
|
55
|
+
|
56
|
+
def name
|
57
|
+
ast.children[0]
|
58
|
+
end
|
59
|
+
|
60
|
+
def arguments
|
61
|
+
Unparser.unparse(ast.children[1])
|
62
|
+
end
|
63
|
+
|
64
|
+
def body
|
65
|
+
Unparser.unparse(ast.children[2])
|
66
|
+
end
|
67
|
+
|
68
|
+
def to_ruby_str
|
69
|
+
Unparser.unparse(ast)
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
attr_reader :ast
|
74
|
+
end
|
75
|
+
|
76
|
+
def defs
|
77
|
+
class_begin.children.select { |n| n.try(:type) == :def }.map{|n| Def.new(ast: n)}
|
78
|
+
end
|
79
|
+
|
80
|
+
def class_begin
|
81
|
+
find_class.children.find{|n| n.try(:type) == :begin}
|
82
|
+
end
|
83
|
+
|
84
|
+
def to_ruby_str
|
85
|
+
Unparser.unparse(ast)
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
attr_reader :source
|
91
|
+
|
92
|
+
def find_class
|
93
|
+
return ast if ast.try(:type) == :class
|
94
|
+
ast.to_a.select { |n|n.try(:type) == :class }.first
|
95
|
+
end
|
96
|
+
|
97
|
+
def ast
|
98
|
+
@ast ||= Parser::CurrentRuby.parse(source)
|
99
|
+
end
|
100
|
+
|
101
|
+
def nodes
|
102
|
+
@nodes ||= ast.to_a.dup
|
103
|
+
end
|
104
|
+
|
105
|
+
def reset_nodes
|
106
|
+
@nodes = nil
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
@@ -0,0 +1,7 @@
|
|
1
|
+
require 'parser/current'
|
2
|
+
require 'unparser'
|
3
|
+
require 'dissociated_introspection/version'
|
4
|
+
require 'dissociated_introspection/active_support'
|
5
|
+
require 'dissociated_introspection/eval_sandbox'
|
6
|
+
require 'dissociated_introspection/ruby_class'
|
7
|
+
require 'dissociated_introspection/inspection'
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dissociated_introspection
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dustin Zeisler
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: parser
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: unparser
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.3'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.3'
|
83
|
+
description: Introspect methods, parameters, class macros, and constants without loading
|
84
|
+
a parent class or any other dependencies.
|
85
|
+
email:
|
86
|
+
- dustin@zeisler.net
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- CODE_OF_CONDUCT.md
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- dissociated_introspection.gemspec
|
102
|
+
- lib/dissociated_introspection.rb
|
103
|
+
- lib/dissociated_introspection/active_support.rb
|
104
|
+
- lib/dissociated_introspection/eval_sandbox.rb
|
105
|
+
- lib/dissociated_introspection/inspection.rb
|
106
|
+
- lib/dissociated_introspection/recording_parent.rb
|
107
|
+
- lib/dissociated_introspection/ruby_class.rb
|
108
|
+
- lib/dissociated_introspection/version.rb
|
109
|
+
homepage: https://github.com/zeisler/dissociated_introspection
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '2.1'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.2.5
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Introspect methods, parameters, class macros, and constants without loading
|
133
|
+
a parent class or any other dependencies.
|
134
|
+
test_files: []
|