Skip to content

Commit

Permalink
Added column_defaults instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
iwan committed Oct 29, 2013
1 parent ab600e3 commit 003488e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Given a spreadsheet:
```ruby
ss = Goodsheet::Spreadsheet.new("example.xls")
res = ss.read do
column_names :filename => 0, :size => 1, :created_at => 3, :updated_at => 4 # ignore 'description' column
column_names :filename => 0, :size => 1, :created_at => 3, :updated_at => 4 # i want to ignore 'description' column
column_defaults :filename => "UNKNOWN"
validates :filename, :presence => true
validates :size, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0 }
validate :order_of_dates
Expand Down Expand Up @@ -126,14 +127,21 @@ ss.rows_wo_skipped # => except the skipped ones, aliased by `rows` method

#### Reading and validate

Use the `validate` and `read` methods to perform validation and reading. Note that the reading function include a validation call.
Use the `validate` and `read` methods to perform validation and reading. Note that the reading function include the validation step.
Pass the previously seen `options` hash and a block to `validate`/`read` method.
Inside the block you define columns names and indexes you want to validate/read using the `column_names` method. You can use one of these 4 forms (and their effect is the same):
Inside the block you define columns names and indexes you want to validate/read using the `column_names` method. You can use one of these 4 forms (and their effect is identical):
- `column_names :a => 0, :b => 1, :c => 3`
- `column_names 0 => :a, 1 => :b, 3 => :c`
- `column_names [:a, :b, nil, :c]`
- `column_names :a, :b, nil, :c`

Use the `column_defaults` method to specify the value to set when the corresponding cell hold a nil value (is empty). Like the previous method, the following forms are identical:
- `column_defaults :a => 0.0, :b => 0.0, :c => "UNKNOWN"`
- `column_defaults 0 => 0.0, 1 => 0.0, 3 => "UNKNOWN"`
- `column_defaults [0.0, 0.0, "UNKNOWN"]`
- `column_defaults 0.0, 0.0, "UNKNOWN"`
The `column_defaults` macro overwrite the read/validate `:force_nil` option.

Aside from define the columns settings, into block you define the validation rules.
Refer to the [official guide](http://guides.rubyonrails.org/active_record_validations.html) and [ROR Api](http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html)

Expand Down
5 changes: 3 additions & 2 deletions goodsheet.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'goodsheet/version'
require 'date'

Gem::Specification.new do |spec|
spec.name = "goodsheet"
Expand All @@ -12,7 +13,7 @@ Gem::Specification.new do |spec|
spec.summary = "Extract and validate data from a spreadsheet"
spec.homepage = "https://github.com/iwan/goodsheet"
spec.license = "MIT"
spec.date = '2013-07-19'
spec.date = Date.today.to_s
spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
Expand All @@ -23,6 +24,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake"

spec.add_dependency('roo', '>= 1.12.1') # https://github.com/Empact/roo
spec.add_dependency('activemodel', '>= 3.2.14')
spec.add_dependency('activemodel', '~> 3.2')
spec.add_dependency('google_drive')
end

0 comments on commit 003488e

Please sign in to comment.