Sha256: 8e7d61cc41c7ffea35d0be84c17f0ada84c36cc91bb1c0e9e3f5bfe9c110a2ca

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

module Bogus
  class MethodStringifier

    def stringify(method, body)
      <<-RUBY
      def #{method.name}(#{arguments_as_string(method.parameters)})
        #{body}
      end
      RUBY
    end

    def arguments_as_string(arguments)
      stringify_arguments(arguments, DefaultValue)
    end

    def argument_values(arguments)
      stringify_arguments(arguments)
    end

    private

    def stringify_arguments(arguments, default = nil)
      fill_in_missing_names(arguments).map do |type, name|
        argument_to_string(name, type, default)
      end.join(', ')
    end

    def argument_to_string(name, type, default)
      case type
      when :block then "&#{name}"
      when :key then default ? "#{name}: #{default}" : "#{name}: #{name}"
      when :opt then default ? "#{name} = #{default}" : name
      when :req then name
      when :rest then "*#{name}"
      when :keyrest then "**#{name}"
      else raise "unknown argument type: #{type}"
      end
    end

    def fill_in_missing_names(arguments)
      noname_count = 0
      arguments.map do |type, name|
        unless name
          name = "_noname_#{noname_count}"
          noname_count += 1
        end
        [type, name]
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bogus-0.1.4 lib/bogus/fakes/method_stringifier.rb
bogus-0.1.3 lib/bogus/method_stringifier.rb
bogus-0.1.2 lib/bogus/method_stringifier.rb
bogus-0.1.1 lib/bogus/method_stringifier.rb
bogus-0.1.0 lib/bogus/method_stringifier.rb