Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improve #cell argument parsing to distinguish between multiple args a…

…nd a single arg
  • Loading branch information...
commit cfb2813833b0e1fc85ab5678e30cab76a23c7d67 1 parent 5cb0d30
@paulspringett authored
Showing with 28 additions and 5 deletions.
  1. +15 −5 lib/csv_shaper/row.rb
  2. +13 −0 spec/row_spec.rb
View
20 lib/csv_shaper/row.rb
@@ -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
View
13 spec/row_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.