Skip to content
This repository
Browse code

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

…nd a single arg
  • Loading branch information...
commit cfb2813833b0e1fc85ab5678e30cab76a23c7d67 1 parent 5cb0d30
Paul Springett authored

Showing 2 changed files with 28 additions and 5 deletions. Show diff stats Hide diff stats

  1. +15 5 lib/csv_shaper/row.rb
  2. +13 0 spec/row_spec.rb
20 lib/csv_shaper/row.rb
@@ -57,13 +57,23 @@ def cells(*args)
57 57 # `value` - data to assign to the cell (default: nil)
58 58 #
59 59 # Returns an Array of the Row's cells
60   - def cell(column, value = nil)
61   - column = column.to_sym
  60 + def cell(*args)
  61 + if args.empty?
  62 + raise ArgumentError, 'no args passed to #cell, you must pass at least a column name'
  63 + end
  64 +
  65 + column = args.first.to_sym
62 66
63   - if @model && @model.respond_to?(column) && value.nil?
64   - @cells[column] = @model.send(column)
  67 + if args.size == 2
  68 + @cells[column] = args.last
  69 + elsif args.size == 1
  70 + if @model && @model.respond_to?(column)
  71 + @cells[column] = @model.send(column)
  72 + else
  73 + raise ArgumentError, "##{column} is not a method on #{@model.class.to_s}, call `csv.cell #{column}, value` instead"
  74 + end
65 75 else
66   - @cells[column] = value
  76 + raise ArgumentError, 'you can pass a column or a column with a value to #cell'
67 77 end
68 78
69 79 @cells
13 spec/row_spec.rb
@@ -41,5 +41,18 @@
41 41 row.cell :foo, nil
42 42 row.cells.should eq({ foo: nil, gender: 'Male' })
43 43 end
  44 +
  45 + it "should not send column to model if two args are passed" do
  46 + row = CsvShaper::Row.new(user, :gender)
  47 + row.cell :name, 'Another name'
  48 + row.cells.should eq({ name: 'Another name', gender: 'Male' })
  49 + end
  50 +
  51 + it "should raise an exception of the model does not respond to column, and no value is passed" do
  52 + row = CsvShaper::Row.new(user, :gender)
  53 + expect {
  54 + row.cell :foo
  55 + }.to raise_error(ArgumentError)
  56 + end
44 57 end
45 58 end

0 comments on commit cfb2813

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