Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Improve #cell argument parsing to distinguish between multiple args and a single arg #9

Merged
merged 1 commit into from

3 participants

Paul Springett Don't Add Me To Your Organization a.k.a The Travis Bot Brendon Muir
Paul Springett

No description provided.

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged cfb2813 into 5cb0d30).

Paul Springett

@brendon I believe this should resolve your outstanding issue in #8

Now, whenever you call csv.cell with 2 arguments, that second argument is set as the cell value, even if it is (or evalutes to) nil

Paul Springett paulspringett merged commit 44d0519 into from
Paul Springett

@brendon I've released version 0.2.0 which should fix your outstanding issue in #8

Brendon Muir

Thanks Paul, that's really great :) Good to close off that edge case as I think it could have caught others out, and it's very difficult to figure out the cause without looking at the code :)

Have a great day! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 22, 2012
This page is out of date. Refresh to see the latest.
Showing with 28 additions and 5 deletions.
  1. +15 −5 lib/csv_shaper/row.rb
  2. +13 −0 spec/row_spec.rb
20 lib/csv_shaper/row.rb
View
@@ -57,13 +57,23 @@ def cells(*args)
# `value` - data to assign to the cell (default: nil)
#
# Returns an Array of the Row's cells
- def cell(column, value = nil)
- column = column.to_sym
+ def cell(*args)
+ if args.empty?
+ raise ArgumentError, 'no args passed to #cell, you must pass at least a column name'
+ end
+
+ column = args.first.to_sym
- if @model && @model.respond_to?(column) && value.nil?
- @cells[column] = @model.send(column)
+ if args.size == 2
+ @cells[column] = args.last
+ elsif args.size == 1
+ if @model && @model.respond_to?(column)
+ @cells[column] = @model.send(column)
+ else
+ raise ArgumentError, "##{column} is not a method on #{@model.class.to_s}, call `csv.cell #{column}, value` instead"
+ end
else
- @cells[column] = value
+ raise ArgumentError, 'you can pass a column or a column with a value to #cell'
end
@cells
13 spec/row_spec.rb
View
@@ -41,5 +41,18 @@
row.cell :foo, nil
row.cells.should eq({ foo: nil, gender: 'Male' })
end
+
+ it "should not send column to model if two args are passed" do
+ row = CsvShaper::Row.new(user, :gender)
+ row.cell :name, 'Another name'
+ row.cells.should eq({ name: 'Another name', gender: 'Male' })
+ end
+
+ it "should raise an exception of the model does not respond to column, and no value is passed" do
+ row = CsvShaper::Row.new(user, :gender)
+ expect {
+ row.cell :foo
+ }.to raise_error(ArgumentError)
+ end
end
end
Something went wrong with that request. Please try again.