Class: DissociatedIntrospection::RubyCode
- Inherits:
-
Object
- Object
- DissociatedIntrospection::RubyCode
- Defined in:
- lib/dissociated_introspection/ruby_code.rb
Instance Attribute Summary collapse
-
#ast ⇒ Object
readonly
Returns the value of attribute ast.
-
#comments ⇒ Object
readonly
Returns the value of attribute comments.
Class Method Summary collapse
- .build_from_ast(ast, comments: []) ⇒ DissociatedIntrospection::RubyCode
- .build_from_source(source, parse_with_comments: false) ⇒ DissociatedIntrospection::RubyCode
- .create_ast(parse_with_comments, source) ⇒ Object
- .parse_source_method(parse_with_comments) ⇒ Object
Instance Method Summary collapse
- #comments? ⇒ Boolean
-
#initialize(source:, ast:, comments:) ⇒ RubyCode
constructor
A new instance of RubyCode.
- #source ⇒ String (also: #to_s)
- #source_from_ast ⇒ String
Constructor Details
#initialize(source:, ast:, comments:) ⇒ RubyCode
Returns a new instance of RubyCode.
40 41 42 43 44 |
# File 'lib/dissociated_introspection/ruby_code.rb', line 40 def initialize(source:, ast:, comments:) @source = source @ast = ast @comments = comments end |
Instance Attribute Details
#ast ⇒ Object (readonly)
Returns the value of attribute ast.
38 39 40 |
# File 'lib/dissociated_introspection/ruby_code.rb', line 38 def ast @ast end |
#comments ⇒ Object (readonly)
Returns the value of attribute comments.
38 39 40 |
# File 'lib/dissociated_introspection/ruby_code.rb', line 38 def comments @comments end |
Class Method Details
.build_from_ast(ast, comments: []) ⇒ DissociatedIntrospection::RubyCode
16 17 18 19 20 21 |
# File 'lib/dissociated_introspection/ruby_code.rb', line 16 def self.build_from_ast(ast, comments: []) new(source: nil, ast: ast, comments: comments ) end |
.build_from_source(source, parse_with_comments: false) ⇒ DissociatedIntrospection::RubyCode
6 7 8 9 10 11 |
# File 'lib/dissociated_introspection/ruby_code.rb', line 6 def self.build_from_source(source, parse_with_comments: false) ast, comments = create_ast(parse_with_comments, source) new(source: source, ast: ast, comments: comments) end |
.create_ast(parse_with_comments, source) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/dissociated_introspection/ruby_code.rb', line 29 def self.create_ast(parse_with_comments, source) a = Parser::CurrentRuby.public_send(self.parse_source_method(parse_with_comments), source) if parse_with_comments [a[0], a[1]] else [a, []] end end |
.parse_source_method(parse_with_comments) ⇒ Object
24 25 26 |
# File 'lib/dissociated_introspection/ruby_code.rb', line 24 def self.parse_source_method(parse_with_comments) parse_with_comments ? :parse_with_comments : :parse end |
Instance Method Details
#comments? ⇒ Boolean
46 47 48 |
# File 'lib/dissociated_introspection/ruby_code.rb', line 46 def comments? !comments.empty? end |
#source ⇒ String Also known as: to_s
51 52 53 |
# File 'lib/dissociated_introspection/ruby_code.rb', line 51 def source @source = @source.nil? ? source_from_ast : @source end |
#source_from_ast ⇒ String
57 58 59 |
# File 'lib/dissociated_introspection/ruby_code.rb', line 57 def source_from_ast Unparser.unparse(ast, comments) end |