Class: Sandrbox::Response
- Inherits:
-
Object
- Object
- Sandrbox::Response
- Defined in:
- lib/sandrbox/response.rb
Instance Attribute Summary collapse
-
#array ⇒ Object
Returns the value of attribute array.
-
#class_count ⇒ Object
Returns the value of attribute class_count.
-
#compressed_array ⇒ Object
Returns the value of attribute compressed_array.
-
#def_count ⇒ Object
Returns the value of attribute def_count.
-
#do_count ⇒ Object
Returns the value of attribute do_count.
-
#end_count ⇒ Object
Returns the value of attribute end_count.
-
#expanded_array ⇒ Object
Returns the value of attribute expanded_array.
-
#indents ⇒ Object
Returns the value of attribute indents.
-
#left_bracket_count ⇒ Object
Returns the value of attribute left_bracket_count.
-
#left_curly_count ⇒ Object
Returns the value of attribute left_curly_count.
-
#module_count ⇒ Object
Returns the value of attribute module_count.
-
#old_constants ⇒ Object
Returns the value of attribute old_constants.
-
#results ⇒ Object
Returns the value of attribute results.
-
#right_bracket_count ⇒ Object
Returns the value of attribute right_bracket_count.
-
#right_curly_count ⇒ Object
Returns the value of attribute right_curly_count.
Instance Method Summary collapse
- #complete? ⇒ Boolean
- #compress ⇒ Object
- #evaluate ⇒ Object
- #expand ⇒ Object
- #indent_character ⇒ Object
- #indent_level ⇒ Object
-
#initialize(array) ⇒ Response
constructor
A new instance of Response.
- #output ⇒ Object
- #preserve_namespace ⇒ Object
- #restore_namespace ⇒ Object
- #semicolon(char = nil) ⇒ Object
- #uncomment(line) ⇒ Object
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 compress end |
Instance Attribute Details
#array ⇒ Object
Returns the value of attribute array.
3 4 5 |
# File 'lib/sandrbox/response.rb', line 3 def array @array end |
#class_count ⇒ Object
Returns the value of attribute class_count.
4 5 6 |
# File 'lib/sandrbox/response.rb', line 4 def class_count @class_count end |
#compressed_array ⇒ Object
Returns the value of attribute compressed_array.
3 4 5 |
# File 'lib/sandrbox/response.rb', line 3 def compressed_array @compressed_array end |
#def_count ⇒ Object
Returns the value of attribute def_count.
4 5 6 |
# File 'lib/sandrbox/response.rb', line 4 def def_count @def_count end |
#do_count ⇒ Object
Returns the value of attribute do_count.
4 5 6 |
# File 'lib/sandrbox/response.rb', line 4 def do_count @do_count end |
#end_count ⇒ Object
Returns the value of attribute end_count.
4 5 6 |
# File 'lib/sandrbox/response.rb', line 4 def end_count @end_count end |
#expanded_array ⇒ Object
Returns the value of attribute expanded_array.
3 4 5 |
# File 'lib/sandrbox/response.rb', line 3 def @expanded_array end |
#indents ⇒ Object
Returns the value of attribute indents.
3 4 5 |
# File 'lib/sandrbox/response.rb', line 3 def indents @indents end |
#left_bracket_count ⇒ Object
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_count ⇒ Object
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_count ⇒ Object
Returns the value of attribute module_count.
4 5 6 |
# File 'lib/sandrbox/response.rb', line 4 def module_count @module_count end |
#old_constants ⇒ Object
Returns the value of attribute old_constants.
3 4 5 |
# File 'lib/sandrbox/response.rb', line 3 def old_constants @old_constants end |
#results ⇒ Object
Returns the value of attribute results.
3 4 5 |
# File 'lib/sandrbox/response.rb', line 3 def results @results end |
#right_bracket_count ⇒ Object
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_count ⇒ Object
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
84 85 86 |
# File 'lib/sandrbox/response.rb', line 84 def complete? self.indents.empty? end |
#compress ⇒ Object
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..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 ⇒ Object
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 |
#expand ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/sandrbox/response.rb', line 18 def self.array.each do |line| next if line.empty? self. << uncomment(line).split(';').collect(&:strip) end self..flatten! end |
#indent_character ⇒ Object
80 81 82 |
# File 'lib/sandrbox/response.rb', line 80 def indent_character self.indents.last end |
#indent_level ⇒ Object
76 77 78 |
# File 'lib/sandrbox/response.rb', line 76 def indent_level self.indents.count end |
#output ⇒ Object
88 89 90 |
# File 'lib/sandrbox/response.rb', line 88 def output results.collect(&:to_s) end |
#preserve_namespace ⇒ Object
92 93 94 |
# File 'lib/sandrbox/response.rb', line 92 def preserve_namespace self.old_constants = Object.constants end |
#restore_namespace ⇒ Object
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 |