Class: Sandrbox::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/sandrbox/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ Response

Returns a new instance of Response.



6
7
8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#arrayObject

Returns the value of attribute array.



3
4
5
# File 'lib/sandrbox/response.rb', line 3

def array
  @array
end

#class_countObject

Returns the value of attribute class_count.



4
5
6
# File 'lib/sandrbox/response.rb', line 4

def class_count
  @class_count
end

#compressed_arrayObject

Returns the value of attribute compressed_array.



3
4
5
# File 'lib/sandrbox/response.rb', line 3

def compressed_array
  @compressed_array
end

#def_countObject

Returns the value of attribute def_count.



4
5
6
# File 'lib/sandrbox/response.rb', line 4

def def_count
  @def_count
end

#do_countObject

Returns the value of attribute do_count.



4
5
6
# File 'lib/sandrbox/response.rb', line 4

def do_count
  @do_count
end

#end_countObject

Returns the value of attribute end_count.



4
5
6
# File 'lib/sandrbox/response.rb', line 4

def end_count
  @end_count
end

#expanded_arrayObject

Returns the value of attribute expanded_array.



3
4
5
# File 'lib/sandrbox/response.rb', line 3

def expanded_array
  @expanded_array
end

#indentsObject

Returns the value of attribute indents.



3
4
5
# File 'lib/sandrbox/response.rb', line 3

def indents
  @indents
end

#left_bracket_countObject

Returns the value of attribute left_bracket_count.



4
5
6
# File 'lib/sandrbox/response.rb', line 4

def left_bracket_count
  @left_bracket_count
end

#left_curly_countObject

Returns the value of attribute left_curly_count.



4
5
6
# File 'lib/sandrbox/response.rb', line 4

def left_curly_count
  @left_curly_count
end

#module_countObject

Returns the value of attribute module_count.



4
5
6
# File 'lib/sandrbox/response.rb', line 4

def module_count
  @module_count
end

#old_constantsObject

Returns the value of attribute old_constants.



3
4
5
# File 'lib/sandrbox/response.rb', line 3

def old_constants
  @old_constants
end

#resultsObject

Returns the value of attribute results.



3
4
5
# File 'lib/sandrbox/response.rb', line 3

def results
  @results
end

#right_bracket_countObject

Returns the value of attribute right_bracket_count.



4
5
6
# File 'lib/sandrbox/response.rb', line 4

def right_bracket_count
  @right_bracket_count
end

#right_curly_countObject

Returns the value of attribute right_curly_count.



4
5
6
# File 'lib/sandrbox/response.rb', line 4

def right_curly_count
  @right_curly_count
end

Instance Method Details

#complete?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/sandrbox/response.rb', line 84

def complete?
  self.indents.empty?
end

#compressObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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

#evaluateObject



61
62
63
64
65
# 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

#expandObject



18
19
20
21
22
23
24
# 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_characterObject



80
81
82
# File 'lib/sandrbox/response.rb', line 80

def indent_character
  self.indents.last
end

#indent_levelObject



76
77
78
# File 'lib/sandrbox/response.rb', line 76

def indent_level
  self.indents.count
end

#outputObject



88
89
90
# File 'lib/sandrbox/response.rb', line 88

def output
  results.collect(&:to_s)
end

#preserve_namespaceObject



92
93
94
# File 'lib/sandrbox/response.rb', line 92

def preserve_namespace
  self.old_constants = Object.constants
end

#restore_namespaceObject



96
97
98
# 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) ⇒ Object



67
68
69
70
# File 'lib/sandrbox/response.rb', line 67

def semicolon(char = nil)
  char ||= indent_character
  (char == '{' || char == '[') ? '' : ';'
end

#uncomment(line) ⇒ Object



72
73
74
# File 'lib/sandrbox/response.rb', line 72

def uncomment(line)
  line.split('#').first.strip
end