dissociated_introspection 0.7.1 → 0.8.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: 07819f7817b19bf6557dde81cfc9df9c8c792bd3
4
- data.tar.gz: 35b6226a9b5d0b716af84e1d06fa23e9f38e0619
3
+ metadata.gz: 36a8544f5157efb38e2cfe40ec2888847211b761
4
+ data.tar.gz: f70dd03c6c9e52966e95068d59637e70e6a13714
5
5
  SHA512:
6
- metadata.gz: 07f047d77917cac86da4c6c898660a4f20a236783f5006b518d9b5eab69f9134ca26cd3e0b7de3ea379eead0bc9a29fc13a56d870e036fb7a91c26efc9629c3d
7
- data.tar.gz: 4cd70f3162a835d53406d3b3943973bc5b559b7a942fb0c97edc87fe591cf298970ee82d6e9ba9c97fb584fa7cb692915bc07ebc39b36385f05a9edf68251c7a
6
+ metadata.gz: cd41ba99f377e6988fa6884f44ce62b25e83b425c12d59292a971102a9c6a4120b0aa5f37a4bf7fb769a3866e5b36b0d76c44215adf1e30a5fce6eaa362920db
7
+ data.tar.gz: f0fc7cdcaccbedfd83b1992a7cd040c54ff7eeead3860940f5f44e7ce7b25a10bef12e6b9aff8b0761249b17ab8f20f3f0c55ee7864169cf50fcc11d8a66ba92
data/CHANGELOG.md CHANGED
@@ -1,6 +1,16 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## 0.8.0 - 2017-03-24
5
+ ### Enhancement
6
+ - DissociatedIntrospection::RubyClass#class_defs returns same api as #defs
7
+ - DissociatedIntrospection::RubyClass#class_method_calls inspect method name and its arguments ie. attr_reader :name
8
+
9
+ ### Deprecated
10
+ - DissociatedIntrospection::RubyClass#is_class? changed to #class?
11
+ - DissociatedIntrospection::RubyClass#has_parent_class? changed to #parent_class?
12
+ - DissociatedIntrospection::RubyCode#to_ruby_str changed to #source
13
+
4
14
  ## 0.7.1 - 2016-10-20
5
15
  ### Fix
6
16
  - `DissociatedIntrospection::RubyClass#defs` fixed parse issue when class has one method.
data/README.md CHANGED
@@ -44,7 +44,7 @@ new_ruby_class = ruby_class.modify_parent_class(:C)
44
44
  new_ruby_class.parent_class_name
45
45
  # => "C"
46
46
 
47
- new_ruby_class.to_ruby_str
47
+ new_ruby_class.source
48
48
  # => "class A < C\n def method\n end\nend"
49
49
 
50
50
  ruby_class.defs.first.name
@@ -0,0 +1,11 @@
1
+ module DissociatedIntrospection
2
+ Block = Struct.new(:ast, :type)
3
+
4
+ def Block.source
5
+ :source
6
+ end
7
+
8
+ def Block.to_h
9
+ super.merge(source: source)
10
+ end
11
+ end
@@ -83,7 +83,7 @@ module DissociatedIntrospection
83
83
  else
84
84
  file.path
85
85
  end
86
- load_sandbox(OpenStruct.new(read: modified_class_source.to_ruby_str, path: path))
86
+ load_sandbox(OpenStruct.new(read: modified_class_source.source, path: path))
87
87
  end
88
88
 
89
89
  def load_sandbox(file)
@@ -0,0 +1,75 @@
1
+ module DissociatedIntrospection
2
+ class Lambda
3
+ attr_reader :ast
4
+
5
+ def initialize(ast)
6
+ @ast = ast
7
+ end
8
+
9
+ def type
10
+ ast.type
11
+ end
12
+
13
+ def body
14
+ RubyCode.build_from_ast(ast.to_a[2])
15
+ end
16
+
17
+ def arguments
18
+ RubyCode.build_from_ast(ast.to_a[1])
19
+ end
20
+ end
21
+
22
+ class MethodCall
23
+ attr_reader :ruby_code
24
+
25
+ def initialize(ruby_code)
26
+ @ruby_code = ruby_code
27
+ end
28
+
29
+ def arguments
30
+ ruby_code.ast.children[2..-1].map do |c|
31
+ case c.type
32
+ when :sym, :str
33
+ c.to_a.first
34
+ when :block
35
+ Lambda.new(c)
36
+ else
37
+ c
38
+ end
39
+ end
40
+ end
41
+
42
+ def name
43
+ ruby_code.ast.children[1]
44
+ end
45
+
46
+ def to_h
47
+ { name: name, arguments: arguments }
48
+ end
49
+
50
+ class Argument
51
+ attr_reader :ast
52
+
53
+ def initialize(ast)
54
+ @ast = ast
55
+ end
56
+
57
+ def type
58
+ ast.type
59
+ end
60
+
61
+ def value
62
+ case type
63
+ when :block
64
+ Block.new(ast.children)
65
+ else
66
+ ast.children[0]
67
+ end
68
+ end
69
+
70
+ def to_h
71
+ { type: type, value: value }
72
+ end
73
+ end
74
+ end
75
+ end
@@ -1,6 +1,7 @@
1
1
  module DissociatedIntrospection
2
2
  class RubyClass
3
3
  attr_reader :ruby_code
4
+ extend Forwardable
4
5
  using Try
5
6
 
6
7
  def initialize(ruby_code)
@@ -14,19 +15,9 @@ module DissociatedIntrospection
14
15
  end
15
16
  end
16
17
 
17
- def ast
18
- ruby_code.ast
19
- end
20
-
21
- def source
22
- ruby_code.source
23
- end
18
+ def_delegators :ruby_code, :ast, :source, :comments
24
19
 
25
- def comments
26
- ruby_code.comments
27
- end
28
-
29
- def is_class?
20
+ def class?
30
21
  ast.type == :class
31
22
  end
32
23
 
@@ -38,25 +29,25 @@ module DissociatedIntrospection
38
29
  Unparser.unparse(find_class.to_a[1])
39
30
  end
40
31
 
41
- def has_parent_class?
32
+ def parent_class?
42
33
  return false unless find_class
43
34
  find_class.to_a[1].try(:type) == :const
44
35
  end
45
36
 
46
37
  def change_class_name(class_name)
47
- reset_nodes
38
+ nodes = ast.to_a.dup
48
39
  nodes[0] = Parser::CurrentRuby.parse(class_name)
49
40
  new_ast = ast.updated(nil, nodes, nil)
50
41
  self.class.new(ast: new_ast)
51
42
  end
52
43
 
53
44
  def modify_parent_class(parent_class)
54
- reset_nodes
55
- if has_parent_class?
45
+ if parent_class?
56
46
  class_node = find_class.to_a.dup
57
47
  class_node[1] = Parser::CurrentRuby.parse(parent_class.to_s)
58
48
  new_ast = find_class.updated(nil, class_node, nil)
59
49
  else
50
+ nodes = ast.to_a.dup
60
51
  nodes[1] = nodes[0].updated(:const, [nil, parent_class.to_sym])
61
52
  new_ast = ast.updated(nil, nodes, nil)
62
53
  end
@@ -65,11 +56,18 @@ module DissociatedIntrospection
65
56
  end
66
57
 
67
58
  def defs
68
- class_begin.children.select { |n| n.try(:type) == :def }.map do |n|
69
- def_comments = comments.select do |comment|
70
- comment.location.last_line+1 == n.location.first_line
71
- end
72
- Def.new(RubyCode.build_from_ast(n, comments: def_comments))
59
+ class_begin.children.select { |n| n.try(:type) == :def }.map(&method(:create_def))
60
+ end
61
+
62
+ def class_defs
63
+ class_begin.children.select { |n| [:defs, :sclass].include? n.try(:type) }.map do |n|
64
+ new_n = if n.type == :defs # def self.method;end
65
+ n.updated(:def, n.children[1..-1])
66
+ elsif n.type == :sclass # class >> self; def method;end
67
+ n.updated(:def, n.children[1].children, location: n.children[1].location)
68
+ end
69
+
70
+ create_def(new_n)
73
71
  end
74
72
  end
75
73
 
@@ -77,18 +75,16 @@ module DissociatedIntrospection
77
75
  find_class.children.find { |n| n.try(:type) == :begin } || find_class
78
76
  end
79
77
 
80
- def to_ruby_str
81
- source
82
- end
83
-
84
78
  def scrub_inner_classes
85
- self.class.new RubyCode.build_from_ast(scrub_inner_classes_ast,
86
- comments: comments)
79
+ self.class.new RubyCode.build_from_ast(
80
+ scrub_inner_classes_ast,
81
+ comments: comments
82
+ )
87
83
  end
88
84
 
89
85
  def module_nesting
90
86
  ary = []
91
- m = ast
87
+ m = ast
92
88
  while m
93
89
  if (m = depth_first_search(m, :module, :class))
94
90
  name = m.to_a[0].to_a[1]
@@ -99,8 +95,21 @@ module DissociatedIntrospection
99
95
  ary
100
96
  end
101
97
 
98
+ def class_method_calls
99
+ class_begin.children.select { |n| n.try(:type) == :send }.map do |ast|
100
+ MethodCall.new(RubyCode.build_from_ast(ast))
101
+ end
102
+ end
103
+
102
104
  private
103
105
 
106
+ def create_def(n)
107
+ def_comments = comments.select do |comment|
108
+ comment.location.last_line+1 == n.location.first_line
109
+ end
110
+ Def.new(RubyCode.build_from_ast(n, comments: def_comments))
111
+ end
112
+
104
113
  def scrub_inner_classes_ast
105
114
  find_class.updated(find_class.type,
106
115
  class_begin.updated(class_begin.type,
@@ -121,15 +130,7 @@ module DissociatedIntrospection
121
130
  return v if v.is_a?(Parser::AST::Node)
122
131
  end
123
132
  end
124
- return false
125
- end
126
-
127
- def nodes
128
- @nodes ||= ast.to_a.dup
129
- end
130
-
131
- def reset_nodes
132
- @nodes = nil
133
+ false
133
134
  end
134
135
  end
135
136
  end
@@ -18,8 +18,8 @@ module DissociatedIntrospection
18
18
  Unparser.unparse(ruby_code.ast.children[2])
19
19
  end
20
20
 
21
- def to_ruby_str
22
- ruby_code.to_ruby_str
21
+ def source
22
+ ruby_code.source
23
23
  end
24
24
 
25
25
  private
@@ -53,8 +53,6 @@ module DissociatedIntrospection
53
53
  @source = @source.nil? ? source_from_ast : @source
54
54
  end
55
55
 
56
- alias_method :to_ruby_str, :source
57
-
58
56
  # @return [String]
59
57
  def source_from_ast
60
58
  Unparser.unparse(ast, comments)
@@ -1,3 +1,3 @@
1
1
  module DissociatedIntrospection
2
- VERSION = "0.7.1"
2
+ VERSION = "0.8.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dissociated_introspection
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dustin Zeisler
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-20 00:00:00.000000000 Z
11
+ date: 2017-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -101,8 +101,10 @@ files:
101
101
  - bin/setup
102
102
  - dissociated_introspection.gemspec
103
103
  - lib/dissociated_introspection.rb
104
+ - lib/dissociated_introspection/block.rb
104
105
  - lib/dissociated_introspection/eval_sandbox.rb
105
106
  - lib/dissociated_introspection/inspection.rb
107
+ - lib/dissociated_introspection/method_call.rb
106
108
  - lib/dissociated_introspection/recording_parent.rb
107
109
  - lib/dissociated_introspection/ruby_class.rb
108
110
  - lib/dissociated_introspection/ruby_class/def.rb