Sha256: a5c24edbf51038c4148ba29b5238a4fc25cddc1775e4b243547c037157793cf9

Contents?: true

Size: 1.51 KB

Versions: 7

Compression:

Stored size: 1.51 KB

Contents

module DissociatedIntrospection
  class RubyCode

    # @param [String] source
    # @param [true, false] parse_with_comments
    # @return [DissociatedIntrospection::RubyCode]
    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

    # @param [Ast] ast
    # @param [Array] comments
    # @return [DissociatedIntrospection::RubyCode]
    def self.build_from_ast(ast, comments: [])
      new(source:   nil,
          ast:      ast,
          comments: comments
      )
    end

    # @private
    def self.parse_source_method(parse_with_comments)
      parse_with_comments ? :parse_with_comments : :parse
    end

    # @private
    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

    attr_reader :ast, :comments
    #@private
    def initialize(source:, ast:, comments:)
      @source   = source
      @ast      = ast
      @comments = comments
    end

    def comments?
      !comments.empty?
    end

    # @return [String]
    def source
      @source = @source.nil? ? source_from_ast : @source
    end

    alias_method :to_ruby_str, :source

    # @return [String]
    def source_from_ast
      Unparser.unparse(ast, comments)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
dissociated_introspection-0.7.1 lib/dissociated_introspection/ruby_code.rb
dissociated_introspection-0.7.0 lib/dissociated_introspection/ruby_code.rb
dissociated_introspection-0.6.0 lib/dissociated_introspection/ruby_code.rb
dissociated_introspection-0.5.0 lib/dissociated_introspection/ruby_code.rb
dissociated_introspection-0.4.1 lib/dissociated_introspection/ruby_code.rb
dissociated_introspection-0.4.0 lib/dissociated_introspection/ruby_code.rb
dissociated_introspection-0.3.1 lib/dissociated_introspection/ruby_code.rb