lp_csv_exportable 0.1.6 → 0.2.8

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: 27cb7b83786f789431b64a9bbf8873e3123bedcf
4
- data.tar.gz: 13706fb398738f69d914c5161a526c9973feb1d7
3
+ metadata.gz: ffe1ee3b0b2d1e86a1424ffd542e637ee88a91a6
4
+ data.tar.gz: 25ef7ec02dc545f68235305ebf9f845c635c3cb3
5
5
  SHA512:
6
- metadata.gz: 4d9d48093288b6d91c293bce15cfc4aad9dc13085be75e2acdb4e6d86fa1ea607a7671bdd7007462fc9bcacdc73421dace781813edbdd6d63f9af031f2df3739
7
- data.tar.gz: fc3919e22f6ef0d83a7b57f339880e544f0a6ab0e59d5be21b17868f1f6c9445fd08d8394d46f2605181dd2700e4d6ad25cfac698c34e8181d0fae8248e2a594
6
+ metadata.gz: 9ae801398f7c3b26cca38295c51d19fb5ee793b020cfc8b65fc9ca54eeaf128ee954ab233f8106bdaf4f4eb668db19eb6597e2c4f94ce68e481209b9b296ee40
7
+ data.tar.gz: 2910dd27654e9fdc6aa3572ad931373236bba4ae99a1470ce2676c99b3c2eb6647a3b6dfaf739d2af4b5a9308fd78df51608a89caf05a06ecbceb7c7468a8ca4
data/README.md CHANGED
@@ -22,20 +22,22 @@ Basic Usage
22
22
 
23
23
  Create a class that includes the module `LpCSVExportable::CanExportAsCSV`
24
24
 
25
- ```
25
+ ```ruby
26
26
  class ExportUsers
27
27
  include LpCSVExportable::CanExportAsCSV
28
28
 
29
- column 'First Name', model_method: :first_name
30
- column 'Last Name', model_method: :last_name
31
- column 'Email', model_method: :email
32
- column 'Role', model_methods: %i[membership name]
29
+ column :first_name
30
+ column :last_name
31
+ column :email
32
+ column 'User Role', model_methods: %i[membership name]
33
33
  end
34
34
  ```
35
35
 
36
+ Note: the header argument can be either a symbol or a string.
37
+
36
38
  And then to export, simply instantiate your class and pass in your `collection`, then call `to_csv`. For example, in a Rails controller action, you would do:
37
39
 
38
- ```
40
+ ```ruby
39
41
  def index
40
42
  users = User.all
41
43
 
@@ -49,6 +51,55 @@ And then to export, simply instantiate your class and pass in your `collection`,
49
51
  end
50
52
  ```
51
53
 
54
+ <<<<<<< HEAD
55
+ The Column Method
56
+ ---
57
+
58
+ Every column in a CSV contains (1) a header, and (2) data for each row. The first argument to our `column` method is the header name. The second argument is an `options` hash which includes the following:
59
+
60
+ ```
61
+ model_method
62
+ model_methods
63
+ ```
64
+
65
+ `model_method` is used to determine how to access the data on the object. Let's say we are exporting a CSV of users and our `User` object has a `first_name` database column. In order to access the first name of a user, we need to call `first_name` on an instance of `User`. Therefore, the following would suffice:
66
+
67
+ ```ruby
68
+ column 'First Name', model_method: :first_name
69
+ ```
70
+
71
+ When the first argument to `column` is a symbol and no value is passed for model_method or model_methods, we will assume it is both the header and the model method. That's why the following will work:
72
+
73
+ ```ruby
74
+ column :first_name
75
+ ```
76
+
77
+ You can also chain methods by using `model_methods` like so:
78
+
79
+ ```ruby
80
+ column 'First Name', model_methods: [:names, :first]
81
+ ```
82
+
83
+ Finally, you can use custom methods on your Export class like so:
84
+
85
+ ```ruby
86
+ class ExportUsers
87
+ include LpCSVExportable::CanExportAsCSV
88
+
89
+ column 'Full Name', model_method: :full_name
90
+
91
+ def full_name(obj)
92
+ [obj.first_name, obj.last_name].compact.join(' ')
93
+ end
94
+ end
95
+ ```
96
+
97
+ If you would like to default a column to a specific value if the model method returns `nil`, use the `default_value` key when defining the column.
98
+
99
+ ```
100
+ column 'Email', model_method: :email, default_value: 'N/A'
101
+ ```
102
+
52
103
  TODO
53
104
  ---
54
105
 
data/bin/console CHANGED
@@ -7,8 +7,8 @@ require "lp_csv_exportable"
7
7
  # with your gem easier. You can also use a different console, if you like.
8
8
 
9
9
  # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
10
+ require "pry"
11
+ Pry.start
12
12
 
13
13
  require "irb"
14
14
  IRB.start(__FILE__)
@@ -1,5 +1,6 @@
1
1
  require 'lp_csv_exportable/version'
2
2
  require 'csv'
3
+ require 'active_support/all'
3
4
 
4
5
  module LpCSVExportable
5
6
  end
@@ -1,6 +1,6 @@
1
1
  module LpCSVExportable
2
2
  module CanExportAsCSV
3
- attr_accessor :collection
3
+ attr_accessor :collection, :column_class
4
4
 
5
5
  def self.included(base)
6
6
  base.extend ClassMethods
@@ -17,14 +17,18 @@ module LpCSVExportable
17
17
 
18
18
  private
19
19
 
20
+ def column_class
21
+ @column_class ||= CSVColumn
22
+ end
23
+
20
24
  def columns
21
25
  self.class.columns_hashes.map do |hash|
22
- CSVColumn.new(hash)
26
+ column_class.new(hash)
23
27
  end
24
28
  end
25
29
 
26
30
  def headers
27
- columns.map(&:header)
31
+ columns.map { |c| c.header.try(:to_s) }
28
32
  end
29
33
 
30
34
  def data_matrix
@@ -51,7 +55,7 @@ module LpCSVExportable
51
55
  if respond_to?(model_method)
52
56
  send(model_method, memo)
53
57
  else
54
- memo.try(:send, model_method)
58
+ memo.try(model_method)
55
59
  end
56
60
  end
57
61
 
@@ -64,7 +68,9 @@ module LpCSVExportable
64
68
  # Configuration...
65
69
  module ClassMethods
66
70
  def column(header, options = {})
67
- columns_hashes << { header: header }.merge(options)
71
+ column_settings = { header: header }
72
+ column_settings[:model_method] = header if header.is_a?(Symbol) && options[:model_methods].nil?
73
+ columns_hashes << column_settings.merge(options)
68
74
  end
69
75
 
70
76
  def columns_hashes
@@ -1,17 +1,19 @@
1
1
  module LpCSVExportable
2
2
  class CSVColumn
3
- attr_reader :header, :model_method, :model_methods, :type
3
+ attr_reader :header, :default_value, :model_method, :model_methods, :type
4
4
 
5
5
  def initialize(args = {})
6
6
  @header = args[:header]
7
7
  @model_method = args[:model_method]
8
8
  @model_methods = args[:model_methods]
9
9
  @type = args.fetch(:type, :string)
10
+ @default_value = args.fetch(:default_value, '')
10
11
  after_init(args)
11
12
  end
12
13
 
13
14
  def format(result)
14
- return send(type, result) if respond_to?(type)
15
+ return formatted_result(result) if respond_to?(type)
16
+ return default_value if use_default?(result)
15
17
  result
16
18
  end
17
19
 
@@ -20,5 +22,15 @@ module LpCSVExportable
20
22
  def after_init(args = {})
21
23
  # hook for subclasses
22
24
  end
25
+
26
+ def use_default?(result)
27
+ default_value.present? && result.nil?
28
+ end
29
+
30
+ def formatted_result(result)
31
+ formatted = send(type, result)
32
+ return default_value if use_default?(formatted)
33
+ formatted
34
+ end
23
35
  end
24
36
  end
@@ -1,3 +1,3 @@
1
1
  module LpCSVExportable
2
- VERSION = '0.1.6'.freeze
2
+ VERSION = '0.2.8'.freeze
3
3
  end
@@ -33,4 +33,7 @@ Gem::Specification.new do |spec|
33
33
  spec.add_development_dependency 'bundler', '~> 1.14'
34
34
  spec.add_development_dependency 'rake', '~> 10.0'
35
35
  spec.add_development_dependency 'rspec', '~> 3.0'
36
+ spec.add_development_dependency 'pry'
37
+
38
+ spec.add_dependency 'activesupport'
36
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lp_csv_exportable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Francis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-22 00:00:00.000000000 Z
11
+ date: 2017-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,34 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  description: Export from your Rails app to CSV in no time.
56
84
  email:
57
85
  - ryan@launchpadlab.com
@@ -97,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
125
  version: '0'
98
126
  requirements: []
99
127
  rubyforge_project:
100
- rubygems_version: 2.5.1
128
+ rubygems_version: 2.6.13
101
129
  signing_key:
102
130
  specification_version: 4
103
131
  summary: Easily export data to CSV