Sha256: ac38a63045b4bdc840b535ca11771391407a305403475ef309953ffab9c1a73f

Contents?: true

Size: 871 Bytes

Versions: 17

Compression:

Stored size: 871 Bytes

Contents

module DissociatedIntrospection
  class WrapInModules
    # @param [DissociatedIntrospection::RubyCode] ruby_code
    def initialize(ruby_code:)
      @ruby_code = ruby_code
    end

    # @param [String] modules
    # @return [DissociatedIntrospection::RubyCode]
    def call(modules:)
      return ruby_code if modules.nil? || modules.empty?
      wrap_in_modules(modules)
    end

    private

    attr_reader :ruby_code

    def wrap_in_modules(modules)
      ruby_string = ruby_code.source_from_ast
      modules.split("::").reverse.each do |module_name|
        ruby_string = wrap_module(module_name, ruby_string)
      end
      RubyCode.build_from_source(ruby_string, parse_with_comments: ruby_code.comments?)
    end

    def wrap_module(module_name, ruby_string)
      <<-RUBY
    module #{module_name}
      #{ruby_string}
    end
      RUBY
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
dissociated_introspection-0.13.0 lib/dissociated_introspection/wrap_in_modules.rb
dissociated_introspection-0.12.0 lib/dissociated_introspection/wrap_in_modules.rb
dissociated_introspection-0.11.0 lib/dissociated_introspection/wrap_in_modules.rb
dissociated_introspection-0.9.1 lib/dissociated_introspection/wrap_in_modules.rb
dissociated_introspection-0.9.0 lib/dissociated_introspection/wrap_in_modules.rb
dissociated_introspection-0.8.4 lib/dissociated_introspection/wrap_in_modules.rb
dissociated_introspection-0.8.3 lib/dissociated_introspection/wrap_in_modules.rb
dissociated_introspection-0.8.2 lib/dissociated_introspection/wrap_in_modules.rb
dissociated_introspection-0.8.1 lib/dissociated_introspection/wrap_in_modules.rb
dissociated_introspection-0.8.0 lib/dissociated_introspection/wrap_in_modules.rb
dissociated_introspection-0.7.1 lib/dissociated_introspection/wrap_in_modules.rb
dissociated_introspection-0.7.0 lib/dissociated_introspection/wrap_in_modules.rb
dissociated_introspection-0.6.0 lib/dissociated_introspection/wrap_in_modules.rb
dissociated_introspection-0.5.0 lib/dissociated_introspection/wrap_in_modules.rb
dissociated_introspection-0.4.1 lib/dissociated_introspection/wrap_in_modules.rb
dissociated_introspection-0.4.0 lib/dissociated_introspection/wrap_in_modules.rb
dissociated_introspection-0.3.1 lib/dissociated_introspection/wrap_in_modules.rb