Sha256: 6be82e9d826edc0ee32cace4a729d9c8edbeffd8f4a12ad7f9f0c9568d662ea9

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 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 :keyreq then default ? "#{name}:" : "#{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

2 entries across 2 versions & 1 rubygems

Version Path
bogus-0.1.6 lib/bogus/fakes/method_stringifier.rb
bogus-0.1.5 lib/bogus/fakes/method_stringifier.rb