dissociated_introspection 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d52a5954cb2475630f4bc5b5bf6f7c2da5d7f725
4
- data.tar.gz: 40245585bd9ace7f23b553c96f74338999c475ca
3
+ metadata.gz: d9a42a861612c49b10e198aa675c5ea86b5802d5
4
+ data.tar.gz: 3ce894b45ba7bdda24ad8d4120a07ce24bde4206
5
5
  SHA512:
6
- metadata.gz: 3e5bdfb47e27e052dc08aa9318100964115918c90cbd52d1697bc45c8e2fbd1a6c57704bda52c58fdf626d17abd99951ebef9c93c2846c1a5db7657f2851ebe6
7
- data.tar.gz: 2c56974c1bcf78f779ff30778ad92dff8c469900cb554927345550904daf0c55ead6bdba9c4135042b51117fb6d06fced33c784718c3d3b1095ffbc7dc0bbc02
6
+ metadata.gz: 19ad771d2a31f1efe5ed4466d6461371edc78ab44b6820999f1e7987fbeb65b8a583339be0256e7c7208de6f0a8dcdfb450ff91eb52631b24cd9b7fa495f861f
7
+ data.tar.gz: 071c6aedfffab0a6b1eed7a887dd69b14e3d54561c0c7f8197402e3b514c5bb4d4f04486380298382c3c3d8fe6788693309c91feccf840a5f7b98c95c727e01e
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --format documentation
2
2
  --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ ## 0.3.1 - 2015-01-17
5
+ ### Enhancement
6
+ - `DissociatedIntrospection::RubyClass` now can take a `DissociatedIntrospection::RubyCode` which is build with `#build_from_ast`, `#build_from_source`.
7
+ - `DissociatedIntrospection::WrapInModules` given a instance of `DissociatedIntrospection::RubyCode` it will nest any depth of module namespacing.
@@ -3,5 +3,8 @@ require 'unparser'
3
3
  require 'dissociated_introspection/version'
4
4
  require 'dissociated_introspection/try'
5
5
  require 'dissociated_introspection/eval_sandbox'
6
+ require 'dissociated_introspection/wrap_in_modules'
7
+ require 'dissociated_introspection/ruby_code'
6
8
  require 'dissociated_introspection/ruby_class'
9
+ require 'dissociated_introspection/ruby_class/def'
7
10
  require 'dissociated_introspection/inspection'
@@ -1,13 +1,29 @@
1
1
  module DissociatedIntrospection
2
2
  class RubyClass
3
+ attr_reader :ruby_code
3
4
  using Try
4
5
 
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
6
+ def initialize(ruby_code)
7
+ @ruby_code = if ruby_code.is_a?(Hash) && ruby_code.has_key?(:source)
8
+ RubyCode.build_from_source(ruby_code[:source])
9
+ elsif ruby_code.is_a?(Hash) && ruby_code.has_key?(:ast)
10
+ RubyCode.build_from_ast(ruby_code[:ast],
11
+ comments: ruby_code.fetch(:comments, []))
12
+ else
13
+ ruby_code
14
+ end
15
+ end
16
+
17
+ def ast
18
+ ruby_code.ast
19
+ end
20
+
21
+ def source
22
+ ruby_code.source
23
+ end
24
+
25
+ def comments
26
+ ruby_code.comments
11
27
  end
12
28
 
13
29
  def is_class?
@@ -44,80 +60,38 @@ module DissociatedIntrospection
44
60
  nodes[1] = nodes[0].updated(:const, [nil, parent_class.to_sym])
45
61
  new_ast = ast.updated(nil, nodes, nil)
46
62
  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
63
 
72
- private
73
- attr_reader :ast
64
+ self.class.new(RubyCode.build_from_ast(new_ast, comments: comments))
74
65
  end
75
66
 
76
67
  def defs
77
- class_begin.children.select { |n| n.try(:type) == :def }.map{|n| Def.new(ast: n)}
68
+ class_begin.children.select { |n| n.try(:type) == :def }.map { |n| Def.new(ast: n) }
78
69
  end
79
70
 
80
71
  def class_begin
81
- find_class.children.find{|n| n.try(:type) == :begin}
72
+ find_class.children.find { |n| n.try(:type) == :begin }
82
73
  end
83
74
 
84
75
  def to_ruby_str
85
- Unparser.unparse(ast)
76
+ source
86
77
  end
87
78
 
88
79
  def scrub_inner_classes
89
- self.class.new(ast: find_class.updated(find_class.type, class_begin.updated(class_begin.type, class_begin.children.reject { |n| n.try(:type) == :class })))
90
- end
91
-
92
- def wrap_in_modules(modules)
93
- return self if modules.nil? || modules.empty?
94
- ruby_string = to_ruby_str
95
- modules.split("::").reverse.each do |module_name|
96
- ruby_string = wrap_module(module_name, ruby_string)
97
- end
98
- wrapped_ast = Parser::CurrentRuby.parse(ruby_string)
99
- self.class.new(ast: wrapped_ast)
80
+ self.class.new RubyCode.build_from_ast(scrub_inner_classes_ast,
81
+ comments: comments)
100
82
  end
101
83
 
102
84
  private
103
85
 
104
- def wrap_module(module_name, ruby)
105
- <<-RUBY
106
- module #{module_name}
107
- #{ruby}
108
- end
109
- RUBY
86
+ def scrub_inner_classes_ast
87
+ find_class.updated(find_class.type,
88
+ class_begin.updated(class_begin.type,
89
+ class_begin.children.reject { |n| n.try(:type) == :class }))
110
90
  end
111
91
 
112
- attr_reader :source
113
-
114
92
  def find_class
115
93
  return ast if ast.try(:type) == :class
116
- ast.to_a.select { |n|n.try(:type) == :class }.first
117
- end
118
-
119
- def ast
120
- @ast ||= Parser::CurrentRuby.parse(source)
94
+ ast.to_a.select { |n| n.try(:type) == :class }.first
121
95
  end
122
96
 
123
97
  def nodes
@@ -0,0 +1,29 @@
1
+ module DissociatedIntrospection
2
+ class RubyClass
3
+ class Def
4
+
5
+ def initialize(ast:)
6
+ @ast = ast
7
+ end
8
+
9
+ def name
10
+ ast.children[0]
11
+ end
12
+
13
+ def arguments
14
+ Unparser.unparse(ast.children[1])
15
+ end
16
+
17
+ def body
18
+ Unparser.unparse(ast.children[2])
19
+ end
20
+
21
+ def to_ruby_str
22
+ Unparser.unparse(ast)
23
+ end
24
+
25
+ private
26
+ attr_reader :ast
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,63 @@
1
+ module DissociatedIntrospection
2
+ class RubyCode
3
+
4
+ # @param [String] source
5
+ # @param [true, false] parse_with_comments
6
+ # @return [DissociatedIntrospection::RubyCode]
7
+ def self.build_from_source(source, parse_with_comments: false)
8
+ ast, comments = create_ast(parse_with_comments, source)
9
+ new(source: source,
10
+ ast: ast,
11
+ comments: comments)
12
+ end
13
+
14
+ # @param [Ast] ast
15
+ # @param [Array] comments
16
+ # @return [DissociatedIntrospection::RubyCode]
17
+ def self.build_from_ast(ast, comments: [])
18
+ new(source: nil,
19
+ ast: ast,
20
+ comments: comments
21
+ )
22
+ end
23
+
24
+ # @private
25
+ def self.parse_source_method(parse_with_comments)
26
+ parse_with_comments ? :parse_with_comments : :parse
27
+ end
28
+
29
+ # @private
30
+ def self.create_ast(parse_with_comments, source)
31
+ a = Parser::CurrentRuby.public_send(self.parse_source_method(parse_with_comments), source)
32
+ if parse_with_comments
33
+ [a[0], a[1]]
34
+ else
35
+ [a, []]
36
+ end
37
+ end
38
+
39
+ attr_reader :ast, :comments
40
+ #@private
41
+ def initialize(source:, ast:, comments:)
42
+ @source = source
43
+ @ast = ast
44
+ @comments = comments
45
+ end
46
+
47
+ def comments?
48
+ !comments.empty?
49
+ end
50
+
51
+ # @return [String]
52
+ def source
53
+ @source = @source.nil? ? source_from_ast : @source
54
+ end
55
+
56
+ alias_method :to_ruby_str, :source
57
+
58
+ # @return [String]
59
+ def source_from_ast
60
+ Unparser.unparse(ast, comments)
61
+ end
62
+ end
63
+ end
@@ -1,3 +1,3 @@
1
1
  module DissociatedIntrospection
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -0,0 +1,35 @@
1
+ module DissociatedIntrospection
2
+ class WrapInModules
3
+ # @param [DissociatedIntrospection::RubyCode] ruby_code
4
+ def initialize(ruby_code:)
5
+ @ruby_code = ruby_code
6
+ end
7
+
8
+ # @param [String] modules
9
+ # @return [DissociatedIntrospection::RubyCode]
10
+ def call(modules:)
11
+ return ruby_code if modules.nil? || modules.empty?
12
+ wrap_in_modules(modules)
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :ruby_code
18
+
19
+ def wrap_in_modules(modules)
20
+ ruby_string = ruby_code.source_from_ast
21
+ modules.split("::").reverse.each do |module_name|
22
+ ruby_string = wrap_module(module_name, ruby_string)
23
+ end
24
+ RubyCode.build_from_source(ruby_string, parse_with_comments: ruby_code.comments?)
25
+ end
26
+
27
+ def wrap_module(module_name, ruby_string)
28
+ <<-RUBY
29
+ module #{module_name}
30
+ #{ruby_string}
31
+ end
32
+ RUBY
33
+ end
34
+ end
35
+ 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.2.0
4
+ version: 0.3.1
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-01-05 00:00:00.000000000 Z
11
+ date: 2016-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -91,6 +91,7 @@ files:
91
91
  - ".gitignore"
92
92
  - ".rspec"
93
93
  - ".travis.yml"
94
+ - CHANGELOG.md
94
95
  - CODE_OF_CONDUCT.md
95
96
  - Gemfile
96
97
  - LICENSE.txt
@@ -104,8 +105,11 @@ files:
104
105
  - lib/dissociated_introspection/inspection.rb
105
106
  - lib/dissociated_introspection/recording_parent.rb
106
107
  - lib/dissociated_introspection/ruby_class.rb
108
+ - lib/dissociated_introspection/ruby_class/def.rb
109
+ - lib/dissociated_introspection/ruby_code.rb
107
110
  - lib/dissociated_introspection/try.rb
108
111
  - lib/dissociated_introspection/version.rb
112
+ - lib/dissociated_introspection/wrap_in_modules.rb
109
113
  homepage: https://github.com/zeisler/dissociated_introspection
110
114
  licenses:
111
115
  - MIT
@@ -132,3 +136,4 @@ specification_version: 4
132
136
  summary: Introspect methods, parameters, class macros, and constants without loading
133
137
  a parent class or any other dependencies.
134
138
  test_files: []
139
+ has_rdoc: