var RubyConsoleDisplay = DUI.Class.create({
    init: function(consoleContainerSelector) {
        this.consoleContainer = $(consoleContainerSelector);
        this.console = this.consoleContainer.children('.console');
    },

    clearDisplay: function() {
        this.console.html('');
    },

    displayRubyScript: function(script) {
        this.appendToConsole('ruby', '>> ' + script.replace(/\n/g, '\n>>  ') + '\n');
    },

    displayResult: function(result) {
        this.appendToConsole('consoleOutput', result.output);
        this.appendToConsole('error', result.error);
        this.appendToConsole('returnedValue', result.returnedValue);
    },

    appendToConsole: function(className, stringValue) {
        if (this.isBlank(stringValue)) {
            return;
        }
        var text = className == 'returnedValue' ? '=> ' + stringValue + '\n' : stringValue;
        this.console.append(this.holderFor(className, text));
    },

    holderFor: function(className, stringValue) {
        var holder = $('<span/>').addClass(className).text(stringValue);
        holder.html(this.escapeLineBreaks(holder.html()));
        return holder;
    },

    isBlank: function(stringValue) {
        return (!stringValue || stringValue == '');
    },

    escapeLineBreaks: function(stringValue) {
        return stringValue.replace(/\n/g, '<br/>');
    }
});
