Sha256: 6062ed627a5c66a12227f74d7a18ecbf02c89e768f1414ca39976953d5dcdbdc

Contents?: true

Size: 927 Bytes

Versions: 4

Compression:

Stored size: 927 Bytes

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)
      arguments = fill_in_missing_names(arguments)
      arguments.map{|type, name| argument_to_string(name, type) }.compact.join(', ')
    end

    def argument_to_string(name, type)
      if type == :req
        name
      elsif type == :rest
        "*#{name}"
      elsif type == :block
        "&#{name}"
      elsif type == :opt
        "#{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

4 entries across 4 versions & 1 rubygems

Version Path
bogus-0.0.3 lib/bogus/method_stringifier.rb
bogus-0.0.3.rc.2 lib/bogus/method_stringifier.rb
bogus-0.0.3.rc.1 lib/bogus/method_stringifier.rb
bogus-0.0.2 lib/bogus/method_stringifier.rb