Sha256: 3a58d781d87b2c60bb412e7b2f636f4e94b9ac113f2e84fb17144c29dcdfcdea

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

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

    def argument_values(arguments)
      arguments_as_string(arguments).gsub(" = Bogus::DefaultValue", '')
    end

    def argument_to_string(name, type)
      case type
      when :block then "&#{name}"
      when :key   then "#{name}: #{name}"
      when :opt   then "#{name} = Bogus::DefaultValue"
      when :req   then name
      when :rest  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

1 entries across 1 versions & 1 rubygems

Version Path
bogus-0.0.4 lib/bogus/method_stringifier.rb