acb 0.1.0 → 0.1.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
  SHA256:
3
- metadata.gz: d03c6b8de9a823ee7da1d09ba0a8c919729de19c2386ebe0f8d3d7ac0f8b290d
4
- data.tar.gz: 7e4fad7a51f42efe2720a4a6b8388fc495d08bf0c58694600b0e343f967b89df
3
+ metadata.gz: 3f68e2938e80af9df35a5ae1994a7aa479ffb5d2b11208e35944991d82957b8d
4
+ data.tar.gz: 24f7f96a0635e48002f4c7a743cf112d5bd4067633264688d8a9668b0abe2e90
5
5
  SHA512:
6
- metadata.gz: 13c6dfe5f3c3ef2343e5f25e61f8e0f14f268fc568124cfa00d661fde77a2f586826319fe1f2d7a24ebca9926b130d7a164d87d5b28f8a2be0f921d78c03c9bc
7
- data.tar.gz: 80e9ad4bfd5e64252950251c0415e78130ebc74a22678d2d2cd164a94bff360e5dabde9c69cfbac14b1499a39787e794143c9bdb7edc148b9a3f2f025d4de90b
6
+ metadata.gz: 51a8387a18a49aa52e7e55608c5d5bd35267f57f8319a83970d296eb7b64ffdc01918545201650722917d7fc248e7d067f3d5eeef50a2b3814aab5060971f119
7
+ data.tar.gz: d3b4736088edbebc5d7ecfa372b037af409d3eb3e6a46e0ca01286ed138a5d805893e45526f4161dc8dde2bea7871bf0905fe464d0e5da74bfc220ff3bd3809b
data/.rubocop.yml CHANGED
@@ -1,5 +1,12 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+ SuggestExtensions: false
5
+ Exclude:
6
+ - 'gemfiles/*'
7
+
8
+ Gemspec/RequireMFA:
9
+ Enabled: false
3
10
 
4
11
  Style/Documentation:
5
12
  Enabled: false
@@ -12,5 +19,14 @@ Style/StringLiteralsInInterpolation:
12
19
  Enabled: true
13
20
  EnforcedStyle: single_quotes
14
21
 
22
+ Style/WordArray:
23
+ Enabled: false
24
+
15
25
  Layout/LineLength:
16
26
  Max: 120
27
+ Exclude:
28
+ - 'spec/support/seed.rb'
29
+
30
+ Metrics/BlockLength:
31
+ Exclude:
32
+ - 'spec/**/*'
data/Appraisals ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ appraise 'rails-6.0-stable' do
4
+ gem 'activerecord', '~> 6.0.0'
5
+ gem 'activesupport', '~> 6.0.0'
6
+ end
7
+
8
+ appraise 'rails-6.1-stable' do
9
+ gem 'activerecord', '~> 6.1.0'
10
+ gem 'activesupport', '~> 6.1.0'
11
+ end
12
+
13
+ appraise 'rails-7.0-stable' do
14
+ gem 'activerecord', '~> 7.0.0'
15
+ gem 'activesupport', '~> 7.0.0'
16
+ end
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Acb
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/acb`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Acb is a gem for formatting and outputting csv data from ActiveRecord data
6
4
 
7
5
  ## Installation
8
6
 
@@ -10,24 +8,61 @@ Install the gem and add to the application's Gemfile by executing:
10
8
 
11
9
  $ bundle add acb
12
10
 
13
- If bundler is not being used to manage dependencies, install the gem by executing:
14
-
15
- $ gem install acb
16
11
 
17
12
  ## Usage
18
13
 
19
- TODO: Write usage instructions here
20
14
 
21
- ## Development
15
+ ```ruby
16
+ ActiveRecord::Schema.define do
17
+ create_table :users, force: true do |t|
18
+ t.string :name
19
+ end
20
+
21
+ create_table :posts, force: true do |t|
22
+ t.integer :user_id
23
+ t.timestamps
24
+ end
25
+
26
+ create_table :comments, force: true do |t|
27
+ t.integer :post_id
28
+ t.string :content
29
+ end
30
+ end
31
+
32
+ class User < ActiveRecord::Base
33
+ has_many :posts
34
+ end
35
+
36
+ class Post < ActiveRecord::Base
37
+ belongs_to :user
38
+ has_many :comments
39
+ end
40
+
41
+ class Comment < ActiveRecord::Base
42
+ belongs_to :post
43
+ end
44
+
45
+ class PostCsvBuilder
46
+ include Acb
22
47
 
23
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48
+ add_column name: 'id'
49
+ add_column name: 'User Name', index: 'user.name'
50
+ add_column name: 'created_at', format: '%Y-%m-%d'
51
+ add_column name: 'Comment Amount', index: 'comments.size'
52
+ add_column name: 'First Comment', index: ->(post) { post.comments.first&.content }
24
53
 
25
- 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
54
+ def initialize(user_id)
55
+ @data = Post.where(user_id: user_id)
56
+ end
26
57
 
27
- ## Contributing
58
+ def relations
59
+ [:user, :comments]
60
+ end
61
+ end
28
62
 
29
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/acb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/acb/blob/main/CODE_OF_CONDUCT.md).
63
+ PostCsvBuilder.new(user_id).content_string
64
+ ```
30
65
 
31
66
  ## Code of Conduct
32
67
 
33
- Everyone interacting in the Acb project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/acb/blob/main/CODE_OF_CONDUCT.md).
68
+ Everyone interacting in the Acb project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/kakubin/acb/blob/main/CODE_OF_CONDUCT.md).
@@ -0,0 +1,11 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake", "~> 13.0"
6
+ gem "rspec", "~> 3.0"
7
+ gem "rubocop", "~> 1.21"
8
+ gem "activerecord", "~> 6.0.0"
9
+ gem "activesupport", "~> 6.0.0"
10
+
11
+ gemspec path: "../"
@@ -0,0 +1,11 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake", "~> 13.0"
6
+ gem "rspec", "~> 3.0"
7
+ gem "rubocop", "~> 1.21"
8
+ gem "activerecord", "~> 6.1.0"
9
+ gem "activesupport", "~> 6.1.0"
10
+
11
+ gemspec path: "../"
@@ -0,0 +1,11 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake", "~> 13.0"
6
+ gem "rspec", "~> 3.0"
7
+ gem "rubocop", "~> 1.21"
8
+ gem "activerecord", "~> 7.0.0"
9
+ gem "activesupport", "~> 7.0.0"
10
+
11
+ gemspec path: "../"
data/lib/acb/column.rb ADDED
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Acb
4
+ class Column
5
+ attr_reader :name
6
+
7
+ def initialize(name, **options)
8
+ @name = name
9
+ @index = case options[:index]
10
+ when Proc
11
+ options[:index]
12
+ when String
13
+ options[:index].split('.')
14
+ else
15
+ [name]
16
+ end
17
+ @format = options[:format]
18
+ end
19
+
20
+ def digest(row)
21
+ data = _digest(row)
22
+ @format && data ? format(data) : data
23
+ end
24
+
25
+ private
26
+
27
+ def _digest(row)
28
+ if @index.is_a?(Array)
29
+ @index.reduce(row) do |acc, mthd|
30
+ acc.send(mthd)
31
+ end
32
+ else
33
+ @index.call(row)
34
+ end
35
+ end
36
+
37
+ def format(data)
38
+ case data
39
+ when Date, Time
40
+ data.strftime(@format)
41
+ else
42
+ @format % data
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Acb
4
+ class Columns
5
+ def initialize
6
+ @columns = []
7
+ end
8
+
9
+ def push(name, **options)
10
+ @columns.push(Column.new(name, **options))
11
+ end
12
+
13
+ def header
14
+ @columns.map(&:name)
15
+ end
16
+
17
+ def get_data(row)
18
+ @columns.map { |column| column.digest(row) }
19
+ end
20
+ end
21
+ end
data/lib/acb/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Acb
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.2'
5
5
  end
data/lib/acb.rb CHANGED
@@ -2,108 +2,37 @@
2
2
 
3
3
  require_relative 'acb/version'
4
4
  require 'active_support'
5
- require 'forwardable'
5
+ require 'csv'
6
6
 
7
7
  module Acb
8
8
  extend ActiveSupport::Concern
9
9
 
10
- class Header
11
- extend Forwardable
12
- include Enumerable
13
-
14
- attr_accessor :columns
15
-
16
- def_delegator :@columns, :each
17
-
18
- def initialize
19
- @columns = []
20
- end
21
-
22
- def push(name, **options)
23
- @columns.push(Column.new(name, **options))
24
- end
25
-
26
- def keys
27
- @columns.map(&:name)
28
- end
29
-
30
- def get_data(row)
31
- @columns.map { _1.digest(row) }
32
- end
33
- end
34
-
35
- class Column
36
- attr_reader :name
37
-
38
- def initialize(name, **options)
39
- @name = name
40
- @index = case options[:index]
41
- when Proc
42
- options[:index]
43
- when String
44
- options[:index].split('.')
45
- else
46
- [name]
47
- end
48
- @format = options[:format]
49
- end
50
-
51
- def digest(row)
52
- data = _digest(row)
53
- @format && data ? format(data) : data
54
- end
55
-
56
- def _digest(row)
57
- if @index.is_a?(Array)
58
- @index.reduce(row) do |acc, set|
59
- acc.send(set)
60
- end
61
- else
62
- @index.call(row)
63
- end
64
- end
65
-
66
- def format(data)
67
- case data
68
- when Date, Time
69
- data.strftime(@format)
70
- else
71
- @format % data
72
- end
73
- end
74
- end
10
+ autoload :Column, 'acb/column'
11
+ autoload :Columns, 'acb/columns'
75
12
 
76
13
  class_methods do
77
- def header
78
- @header ||= Header.new
14
+ def columns
15
+ @columns ||= Columns.new
79
16
  end
80
17
 
81
18
  def add_column(name:, **options)
82
- header.push(name, **options)
19
+ columns.push(name, **options)
83
20
  end
84
21
  end
85
22
 
86
- def initialize(options = {})
87
- @options = options
88
- end
89
-
90
23
  def data
91
24
  @data = @data.preload(relations) if respond_to?(:relations)
92
25
  @data.find_each
93
26
  end
94
27
 
95
- def header
96
- self.class.header.keys
97
- end
98
-
99
28
  def get_data_from(row)
100
- self.class.header.get_data(row)
29
+ self.class.columns.get_data(row)
101
30
  end
102
31
 
103
- def content_string
104
- header_content = header.join(',')
32
+ def content_string(**options)
33
+ header_content = self.class.columns.header.join(',')
105
34
 
106
- CSV.generate(header_content, **@options) do |csv|
35
+ CSV.generate(header_content, **options) do |csv|
107
36
  data.each do |row|
108
37
  csv << get_data_from(row)
109
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akito Hikasa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-12-23 00:00:00.000000000 Z
11
+ date: 2022-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,9 +16,51 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 6.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 6.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 6.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: appraisal
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
22
64
  version_requirements: !ruby/object:Gem::Requirement
23
65
  requirements:
24
66
  - - ">="
@@ -33,13 +75,19 @@ extra_rdoc_files: []
33
75
  files:
34
76
  - ".rspec"
35
77
  - ".rubocop.yml"
78
+ - Appraisals
36
79
  - CHANGELOG.md
37
80
  - CODE_OF_CONDUCT.md
38
81
  - Gemfile
39
82
  - LICENSE
40
83
  - README.md
41
84
  - Rakefile
85
+ - gemfiles/rails_6.0_stable.gemfile
86
+ - gemfiles/rails_6.1_stable.gemfile
87
+ - gemfiles/rails_7.0_stable.gemfile
42
88
  - lib/acb.rb
89
+ - lib/acb/column.rb
90
+ - lib/acb/columns.rb
43
91
  - lib/acb/version.rb
44
92
  - sig/acb.rbs
45
93
  homepage: https://github.com/kakubin/acb