class Sandrbox::Response

Attributes

array[RW]
class_count[RW]
compressed_array[RW]
def_count[RW]
do_count[RW]
end_count[RW]
expanded_array[RW]
indents[RW]
left_bracket_count[RW]
left_curly_count[RW]
module_count[RW]
old_constants[RW]
results[RW]
right_bracket_count[RW]
right_curly_count[RW]

Public Class Methods

new(array) click to toggle source
# File lib/sandrbox/response.rb, line 6
def initialize(array)
  self.array = array
  [:expanded_array, :compressed_array, :indents, :results].each do |arr|
    self.send("#{arr}=".to_sym, [])
  end
  [:class, :module, :def, :end, :do, :left_curly, :right_curly, :left_bracket, :right_bracket].each do |count|
    self.send("#{count}_count=".to_sym, 0)
  end
  expand
  compress
end

Public Instance Methods

complete?() click to toggle source
# File lib/sandrbox/response.rb, line 84
def complete?
  self.indents.empty?
end
compress() click to toggle source
# File lib/sandrbox/response.rb, line 26
def compress
  self.expanded_array.each do |line|
    uncommented_line = uncomment(line)
    ending = nil
    
    [:class, :module, :def, :end, :do].each do |count|
      if uncommented_line =~ /\s*#{count.to_s}\s*/
        self.send("#{count}_count=".to_sym, self.send("#{count}_count".to_sym) + 1) 
        self.indents.push(count.to_s) unless count == :end
        ending = self.indents.pop if count == :end
      end
    end
    
    {:left_curly => ['{', '}'], :right_curly => ['}', '{'], :left_bracket => ['[', ']'], :right_bracket => [']', '[']}.each do |name, sym|
      if uncommented_line.count(sym.first) > uncommented_line.count(sym.last)
        self.send("#{name}_count=".to_sym, self.send("#{name}_count".to_sym) + 1) 
        self.indents.push(sym.first) if name.to_s.include?('left')
        ending = self.indents.pop if name.to_s.include?('right')
      end
    end
    
    if ending
      self.compressed_array.last << "#{uncommented_line}#{semicolon(ending)}"
    elsif indent_level == 1 || self.compressed_array.empty?
      self.compressed_array << "#{uncommented_line}#{semicolon}"
    elsif indent_level > 1
      self.compressed_array.last << "#{uncommented_line}#{semicolon}"
    else
      self.compressed_array << uncommented_line
    end
  end
  
  return evaluate if complete?
end
evaluate() click to toggle source
# File lib/sandrbox/response.rb, line 61
def evaluate
  preserve_namespace
  self.compressed_array.each_with_index {|line, line_no| self.results << Sandrbox::Value.new(line, line_no)}
  restore_namespace
end
expand() click to toggle source
# File lib/sandrbox/response.rb, line 18
def expand
  self.array.each do |line| 
    next if line.empty?
    self.expanded_array << uncomment(line).split(';').collect(&:strip)
  end
  self.expanded_array.flatten!
end
indent_character() click to toggle source
# File lib/sandrbox/response.rb, line 80
def indent_character
  self.indents.last
end
indent_level() click to toggle source
# File lib/sandrbox/response.rb, line 76
def indent_level
  self.indents.count
end
output() click to toggle source
# File lib/sandrbox/response.rb, line 88
def output
  results.collect(&:to_s)
end
preserve_namespace() click to toggle source
# File lib/sandrbox/response.rb, line 92
def preserve_namespace
  self.old_constants = Object.constants
end
restore_namespace() click to toggle source
# File lib/sandrbox/response.rb, line 96
def restore_namespace
  (Object.constants - self.old_constants).each {|bad_constant| Object.send(:remove_const, bad_constant)}
end
semicolon(char = nil) click to toggle source
# File lib/sandrbox/response.rb, line 67
def semicolon(char = nil)
  char ||= indent_character
  (char == '{' || char == '[') ? '' : ';'
end
uncomment(line) click to toggle source
# File lib/sandrbox/response.rb, line 72
def uncomment(line)
  line.split('#').first.strip
end