csv_creator 0.9.1 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 50fa518f5b034a07d9faed2f2553312ca89e1474
4
- data.tar.gz: 789b0250a03153444bbd47ca2c0e15b3f9e21f68
3
+ metadata.gz: 77591e2a208645116ab085c32e1edf107e87d215
4
+ data.tar.gz: d32c84d8ed38cf004495eb7cd714ae33266532a1
5
5
  SHA512:
6
- metadata.gz: bef1657c111cedc0c8f1a43ea81129a50f9c2d7a8f5ba6c18d9ce4c70196b7189b34f2562dfa0a5f30d602c3fbbe1a27df0991bb339998589937f5aae8d19e10
7
- data.tar.gz: 6062236d4b0c8b626d2803d527072b247504ab7b1a3f9b2972a65e939baf8f5f341ce28dfcb6f10370ebbb550ca003c7e5efaac71109df74e84af2cdbc18829d
6
+ metadata.gz: b499241e9ddd19a1e1defc0c40820a6ad2279637c1c77a777d0b1d72823142624d06f7efe7cdac3d73307fe18a89e011c471f5c11f052aa6d3ee885555cffd62
7
+ data.tar.gz: 9d4c8ab2c0b8b9df2c4efa83dcb44169de4e1ba8e0e98be9db01d1042b61b9238ec67fc4ff7a792b5a19103b7c3c09b81c12f03384f77d4e1a65e2c0174748b8
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # CsvCreator
2
2
 
3
- Description
3
+ CsvCreator provides nice, configurable interface which will generate CSV file for you, from collection you want (database or array with non-db related objects).
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,17 +20,70 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- TODO: Write usage instructions here
23
+ Basic usage with default options (see below) is just by calling `create` method on `CsvCreator` module.
24
+ There is also an alias method `generate`. It does the same thing and takes the same arguments.
24
25
 
25
- ## Development
26
+ ```ruby
27
+ CsvCreator.create(my_collection)
28
+ ```
29
+
30
+ ### Options
31
+
32
+ Options can be passed as second argument to the `create` method. The default options hash looks like this.
33
+ ```ruby
34
+ {
35
+ csv: { force_quotes: true, headers: true },
36
+ only: nil,
37
+ without: nil,
38
+ order: [],
39
+ order_direction: :asc,
40
+ translate: {},
41
+ callbacks: {}
42
+ }
43
+ ```
44
+
45
+ ### Options explanation
46
+
47
+ * **csv** - In `csv` key you can pass all options that you would usually pass to the `CSV.generate`.
48
+ Yoo can find more about them under [this link](http://ruby-doc.org/stdlib-2.2.2/libdoc/csv/rdoc/CSV.html). Look for "DEFAULT_OPTIONS".
49
+ ```ruby
50
+ CsvCreator.create(my_collection, { csv: { force_quotes: false } })
51
+ ```
52
+
53
+ * **only** - It's self explanatory. By passing an array of field names you will get in your CSV data only for that fields.
54
+ ```ruby
55
+ CsvCreator.create(my_collection, { only: [:id, :name] })
56
+ ```
26
57
 
27
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+ * **without** - Like above, but this time, fields that are in the array passed under this key will be skipped during generation process.
59
+ ```ruby
60
+ CsvCreator.create(my_collection, { without: [:id, :name] })
61
+ ```
28
62
 
29
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
63
+ * **order** - Makes sense only for collections that are not from database. Obviously it's way better to sort collection with use of database queries. But if you want, `CsvCreator` can sort given collection for you. Under the hood there is simple [sort_by](http://ruby-doc.org/core-2.2.2/Enumerable.html#method-i-sort_by) method.
64
+ ```ruby
65
+ CsvCreator.create(my_collection, { order: [:name] })
66
+ ```
67
+
68
+ * **order_direction** - If you defined some fields that you want to order by your collection, you can manipulate direction.
69
+ ```ruby
70
+ CsvCreator.create(my_collection, { order: [:name], order_direction: :desc })
71
+ ```
72
+
73
+ * **translate** - Useful if you want to somehow change how the column header will be displayed in the CSV document. Very useful for language translations.
74
+ ```ruby
75
+ CsvCreator.create(my_collection, { translate: { name: 'Translated Name Field' })
76
+ ```
77
+
78
+ * **callbacks** - Callbacks can help you with modifying result for each row exactly for the CSV document. For example by formatting date time field or some other calculations.
79
+ ```ruby
80
+ options = { callbacks: { birth_date: ->(person) { person.birth_date.strftime('%m/%d/%Y') } } }
81
+ CsvCreator.create(my_collection, options)
82
+ ```
30
83
 
31
84
  ## Contributing
32
85
 
33
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/csv_creator.
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dsawa/csv_creator.
34
87
 
35
88
  ## License
36
89
 
@@ -1,3 +1,5 @@
1
+ require 'csv'
2
+
1
3
  module CsvCreator
2
4
  class Creator
3
5
  attr_reader :collection
@@ -1,3 +1,3 @@
1
1
  module CsvCreator
2
- VERSION = '0.9.1'
2
+ VERSION = '0.9.2'
3
3
  end
data/lib/csv_creator.rb CHANGED
@@ -1,8 +1,6 @@
1
1
  require 'csv_creator/version'
2
2
  require 'csv_creator/creator'
3
- require 'csv'
4
3
  require 'active_support/core_ext/hash'
5
- require 'pry'
6
4
 
7
5
  module CsvCreator
8
6
  def self.create(collection, options = {})
@@ -18,4 +16,9 @@ module CsvCreator
18
16
 
19
17
  Creator.new(collection, options_with_defaults).generate_csv(options_with_defaults)
20
18
  end
19
+
20
+ # Alias
21
+ def self.generate(collection, options = {})
22
+ create(collection, options)
23
+ end
21
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv_creator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Sawa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-14 00:00:00.000000000 Z
11
+ date: 2015-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport