Sha256: b77f5e96be8132addd87ba2cf26a0b24192a6495834900f2a57444bcc7855de5

Contents?: true

Size: 1.52 KB

Versions: 14

Compression:

Stored size: 1.52 KB

Contents

# encoding: utf-8

module Synvert::Core
  # Rewriter::Helper provides some helper methods to make it easier to write a snippet.
  module Rewriter::Helper
    # Add receiver to code if necessary.
    #
    # @param code [String] old code
    # @return [String] new code
    #
    # @example
    #
    #   add_receiver_if_necessary("{{message}} {{arguments}}")
    #
    #   if current_node doesn't have a receiver, it returns "{{message}} {{arguments}}"
    #   if current_node has a receiver, it returns "{{receiver}}.{{message}} {{arguments}}"
    def add_receiver_if_necessary(code)
      if node.receiver
        "{{receiver}}.#{code}"
      else
        code
      end
    end

    # Add arguments with parenthesis if necessary.
    #
    # @return [String] return `({{arguments}})` if node.arguments present, otherwise return nothing.
    #
    # @example
    #
    #   add_arguments_with_parenthesis_if_necessary
    #
    #   if current_node doesn't have an argument, it returns ""
    #   if current_node has argument, it returns "({{arguments}})"
    def add_arguments_with_parenthesis_if_necessary
      if node.arguments.size > 0
        "({{arguments}})"
      else
        ""
      end
    end

    # Remove leading and trailing brackets.
    #
    # @param code [String] old code
    # @return [String] new code
    #
    # @example
    #
    #   strip_brackets("(1..100)") #=> "1..100"
    def strip_brackets(code)
      code.sub(/^\((.*)\)$/) { $1 }
          .sub(/^\[(.*)\]$/) { $1 }
          .sub(/^{(.*)}$/) { $1 }
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
synvert-core-0.13.2 lib/synvert/core/rewriter/helper.rb
synvert-core-0.13.1 lib/synvert/core/rewriter/helper.rb
synvert-core-0.13.0 lib/synvert/core/rewriter/helper.rb
synvert-core-0.12.0 lib/synvert/core/rewriter/helper.rb
synvert-core-0.11.1 lib/synvert/core/rewriter/helper.rb
synvert-core-0.11.0 lib/synvert/core/rewriter/helper.rb
synvert-core-0.10.0 lib/synvert/core/rewriter/helper.rb
synvert-core-0.9.1 lib/synvert/core/rewriter/helper.rb
synvert-core-0.9.0 lib/synvert/core/rewriter/helper.rb
synvert-core-0.8.2 lib/synvert/core/rewriter/helper.rb
synvert-core-0.8.1 lib/synvert/core/rewriter/helper.rb
synvert-core-0.8.0 lib/synvert/core/rewriter/helper.rb
synvert-core-0.7.5 lib/synvert/core/rewriter/helper.rb
synvert-core-0.7.4 lib/synvert/core/rewriter/helper.rb