Sha256: 31bca75354f8f1cdafc1d00f8545bda246e5f7823e451738055d52b89fc51469

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

module DevScripts
  module Support
    class Block < String
      def initialize(args_node, block_node)
        @block_node = block_node

        self << 'do'

        if args_node.children.size > 0
          self << args_node.children.each_with_object(' |').with_index do |(arg, string), index|
            string << arg.children.first.to_s

            string << ', ' if index < args_node.children.size - 1
            string << '|' if index == args_node.children.size - 1
          end
        end

        self << "\n"
        self << '  ' + body
        self << "\n"
        self << 'end'
      end

      private

      attr_reader :block_node

      def args(node)
        node.children.each_with_object(' |').with_index do |(arg, string), index|
          string << arg.children.first.to_s

          string << ', ' if index < node.children.size - 1
          string << "|" if index == node.children.size - 1
        end
      end

      def block_node_method_call?
        block_node.type == :send
      end

      def body
        if block_node_method_call?
          MethodCall.new(block_node)
        else
          if Symbol === block_node.children.first
            ":#{block_node.children.first}"
          else
            block_node.children.first.to_s
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dev_scripts-0.2.1 lib/dev_scripts/support/block.rb
dev_scripts-0.2.0 lib/dev_scripts/support/block.rb
dev_scripts-0.1.9 lib/dev_scripts/support/block.rb
dev_scripts-0.1.7 lib/dev_scripts/support/block.rb