Class: DissociatedIntrospection::RubyCode

Inherits:
Object
  • Object
show all
Defined in:
lib/dissociated_introspection/ruby_code.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#astObject (readonly)

Returns the value of attribute ast.



38
39
40
# File 'lib/dissociated_introspection/ruby_code.rb', line 38

def ast
  @ast
end

#commentsObject (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

Parameters:

  • ast (Ast)
  • comments (Array) (defaults to: [])

Returns:



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

Parameters:

  • source (String)
  • parse_with_comments (true, false) (defaults to: false)

Returns:



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

Returns:

  • (Boolean)


46
47
48
# File 'lib/dissociated_introspection/ruby_code.rb', line 46

def comments?
  !comments.empty?
end

#sourceString Also known as: to_s

Returns:

  • (String)


51
52
53
# File 'lib/dissociated_introspection/ruby_code.rb', line 51

def source
  @source = @source.nil? ? source_from_ast : @source
end

#source_from_astString

Returns:

  • (String)


57
58
59
# File 'lib/dissociated_introspection/ruby_code.rb', line 57

def source_from_ast
  Unparser.unparse(ast, comments)
end