Skip to content
Browse files

refactor to make intent more clear and improve code climate score

  • Loading branch information...
1 parent 77f7b68 commit 2edc2754abd59dda4c7b6586bfaf7a21673dd9ea @wbailey committed Feb 15, 2014
Showing with 82 additions and 22 deletions.
  1. +1 −0 Gemfile
  2. +3 −0 Gemfile.lock
  3. +24 −0 Guardfile
  4. +3 −3 examples/ascii_table.rb
  5. +2 −2 examples/table.rb
  6. +49 −17 lib/command_line_reporter/table.rb
View
1 Gemfile
@@ -10,6 +10,7 @@ group :test do
gem "rspec"
gem "simplecov"
gem "guard"
+ gem "guard-rspec"
end
group :development do
View
3 Gemfile.lock
@@ -56,6 +56,8 @@ GEM
lumberjack (~> 1.0)
pry (>= 0.9.12)
thor (>= 0.18.1)
+ guard-rspec (1.2.1)
+ guard (>= 1.1)
heckle (1.4.3)
ParseTree (>= 2.0.0)
ZenTest (>= 3.5.2)
@@ -147,6 +149,7 @@ DEPENDENCIES
flog
gollum
guard
+ guard-rspec
heckle
kramdown
pry
View
24 Guardfile
@@ -0,0 +1,24 @@
+# A sample Guardfile
+# More info at https://github.com/guard/guard#readme
+
+guard 'rspec', :version => 2 do
+ watch(%r{^spec/.+_spec\.rb$})
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
+ watch('spec/spec_helper.rb') { "spec" }
+
+ # Rails example
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
+ watch('config/routes.rb') { "spec/routing" }
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
+
+ # Capybara request specs
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
+
+ # Turnip features and steps
+ watch(%r{^spec/acceptance/(.+)\.feature$})
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
+end
+
View
6 examples/ascii_table.rb
@@ -19,7 +19,7 @@ def example_simple
2.times do |j|
header :title => "Table #{j}", :align => 'center', :width => 65
- table :border => j % 2 == 0, :encoding => 'ascii' do
+ table :border => j % 2 == 0, :encoding => :ascii do
3.times do
row do
i = 0
@@ -36,7 +36,7 @@ def example_simple
end
def example_header
- header :title => 'An example of a table with a header row. The color and border properties are not inherited'
+ header :title => 'An example of a table with a header row. The color and border properties are inherited from the first row'
table :border => true, :encoding => :ascii do
row :header => true, :color => 'red' do
@@ -58,7 +58,7 @@ def example_header
end
def example_inherit
- header :title => 'The same table with the properties inherited from the first row'
+ header :title => 'The same table without a header row so the properties inherited from it'
table :border => true, :encoding => :ascii do
row :color => 'red' do
View
4 examples/table.rb
@@ -37,7 +37,7 @@ def example_simple
end
def example_header
- header :title => 'An example of a table with a header row. The color and border properties are not inherited'
+ header :title => 'An example of a table with a header row. The color and border properties are inherited from the first row'
table :border => true do
row :header => true, :color => 'red' do
@@ -59,7 +59,7 @@ def example_header
end
def example_inherit
- header :title => 'The same table with the properties inherited from the first row'
+ header :title => 'The same table without a header row so the properties inherited from it'
table :border => true do
row :color => 'red' do
View
66 lib/command_line_reporter/table.rb
@@ -21,34 +21,34 @@ def add(row)
# Inheritance from the table
row.border = self.border
- # Inherit properties from the first row
+ # Inherit properties from the appropriate row
if self.rows[0]
row.columns.each_with_index do |c,i|
# The positional attributes are always required to inheret to make sure the table
# displays properly
c.align = self.rows[0].columns[i].align
c.padding = self.rows[0].columns[i].padding
c.width = self.rows[0].columns[i].width
+ c.color = use_color(row, c, i)
+ c.bold = use_bold(row, c, i)
- # Allow for the fact that the row or column may take precedence and that the first
- # row might be a header row which we don't want to inherit from
- unless row.color || c.color
- if self.rows[0].header
- c.color = self.rows[1].columns[i].color if self.rows[1]
- else
- c.color = self.rows[0].columns[i].color
- end
- end
+ #unless row.color || c.color
+ #if self.rows[0].header
+ #c.color = self.rows[1].columns[i].color if self.rows[1]
+ #else
+ #c.color = self.rows[0].columns[i].color
+ #end
+ #end
# Allow for the row to take precendence and that the first # row might be a header
# row which we don't want to inherit from
- unless row.bold
- if self.rows[0].header
- c.bold = self.rows[1].columns[i].bold if self.rows[1]
- else
- c.bold = self.rows[0].columns[i].bold
- end
- end
+ #unless row.bold
+ #if self.rows[0].header
+ #c.bold = self.rows[1].columns[i].bold if self.rows[1]
+ #else
+ #c.bold = self.rows[0].columns[i].bold
+ #end
+ #end
end
end
@@ -109,5 +109,37 @@ def separator(type = 'middle')
left + self.rows[0].columns.map {|c| bar * (c.width + 2)}.join(center) + right
end
+
+ def inherit_from
+ if self.rows.size == 0
+ raise RuntimeError
+ elsif self.rows[0] && !self.rows[0].header
+ 0
+ elsif self.rows[0].header && self.rows[1]
+ 1
+ else
+ 0
+ end
+ end
+
+ def use_color(row, column, index)
+ if column.color
+ column.color
+ elsif row.color
+ row.color
+ else
+ self.rows[inherit_from].columns[index].color
+ end
+ end
+
+ def use_bold(row, column, index)
+ if column.bold
+ column.bold
+ elsif row.bold
+ row.bold
+ else
+ self.rows[inherit_from].columns[index].bold
+ end
+ end
end
end

0 comments on commit 2edc275

Please sign in to comment.
Something went wrong with that request. Please try again.