activerecord-bitemporal-tablize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ae131bf581a0e9a7bfef125b093fac9e69238dfb9cae955a8618f8bb74a8fdba
4
+ data.tar.gz: 3c6919b96f711bd2729d885677783e9752810ff4d98231bd846d51ad1dec6420
5
+ SHA512:
6
+ metadata.gz: e50e46763fe730ae5cde090f605502d584c5790cf2a73611927ebc6c50a30a1b2acadf10f652aa59f4e093b2ab8a7fe175cb41c039c287c3d2966d2f4ac9904b
7
+ data.tar.gz: 39abdd88f1351365fd75315a54997ee12547a7ccf2ff746928166693162fed9554f1208b65d077782134da6777aed6eab7970dbf7f7f1a8aa6d6228d9812091d
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.1
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.15
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in activerecord-bitemporal-tablize.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'rspec', '~> 3.0'
11
+
12
+ gem 'activerecord-bitemporal'
13
+ gem 'activesupport'
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Activerecord::Bitemporal::Tablize
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/activerecord/bitemporal/tablize`. 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
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'activerecord-bitemporal-tablize'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install activerecord-bitemporal-tablize
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/activerecord-bitemporal-tablize.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/activerecord/bitemporal/tablize/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'activerecord-bitemporal-tablize'
7
+ spec.version = Activerecord::Bitemporal::Tablize::VERSION
8
+ spec.authors = ['lighty']
9
+ spec.email = ['hikalin8686@gmail.com']
10
+
11
+ spec.summary = 'Displaying the history of records managed by ActiveRecord::Bitemporal in a tabular format.'
12
+ spec.description = 'Displaying the history of records managed by ActiveRecord::Bitemporal in a tabular format.'
13
+ spec.homepage = 'https://github.com/lighty/activerecord-bitemporal-tablize'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = "https://github.com/lighty/activerecord-bitemporal-tablize"
18
+ spec.metadata['changelog_uri'] = "https://github.com/lighty/activerecord-bitemporal-tablize"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
24
+ end
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ # Uncomment to register a new dependency of your gem
30
+ # spec.add_dependency "example-gem", "~> 1.0"
31
+
32
+ # For more information and examples about making a new gem, checkout our
33
+ # guide at: https://bundler.io/guides/creating_gem.html
34
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'activerecord/bitemporal/tablize'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ module ActiveRecord
2
+ class Bitemporal
3
+ def history
4
+ self.class.ignore_valid_datetime.bitemporal_for(bitemporal_id).order(valid_from: :desc).tap do |records|
5
+ records.define_singleton_method(:print_table) do |*attributes|
6
+ print ActiveRecord::Bitemporal::Tablize.new(records: records, attributes: attributes).call
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,111 @@
1
+ require_relative 'tablize/version'
2
+ require 'lib/activerecord/bitemporal/tablize/string_half_width_size'
3
+
4
+ class ActiveRecord::Bitemporal::Tablize
5
+ using StringHalfWidthSize
6
+
7
+ attr :records, :result, :attributes
8
+
9
+ def initialize(records:, attributes: [])
10
+ @records = records.map { |record| RecordWrapper.new(record, self) }
11
+ @attributes = attributes
12
+ @result = []
13
+ end
14
+
15
+ def call
16
+ append_header
17
+ append_body
18
+ result.join("\n") + "\n"
19
+ end
20
+
21
+ def max_width_of(attribute)
22
+ [max_attribute_size_of(attribute), attribute.to_s.half_width_size].max
23
+ end
24
+
25
+ def max_attribute_size_of(attribute)
26
+ # @max_attribute_sizes = {} キャッシュしたほうが良い
27
+ records.map { |r| r.send(attribute).to_s.half_width_size }.max
28
+ end
29
+
30
+ def build_node(value, padding = ' ')
31
+ padding + value + padding
32
+ end
33
+
34
+ private
35
+
36
+ def append_header
37
+ result << "+#{build_roof.join('+')}+"
38
+ result << "|#{build_wall.join('|')}|"
39
+ end
40
+
41
+ def build_roof
42
+ ([:valid_from] + attributes).map do |attribute|
43
+ build_node('-' * max_width_of(attribute), '-')
44
+ end
45
+ end
46
+
47
+ def build_wall
48
+ duration_node = build_node(' ' * max_width_of(:valid_from), ' ')
49
+ [duration_node] + attributes.map do |attribute|
50
+ padding = [max_width_of(attribute) - attribute.to_s.half_width_size, 0].max
51
+ build_node(attribute.to_s + (' ' * padding), ' ')
52
+ end
53
+ end
54
+
55
+ def append_body
56
+ records.each do |record|
57
+ result << "+#{record.build_roof.join('+')}+"
58
+ result << "|#{record.build_wall.join('|')}|"
59
+ result << "+#{record.build_roof.join('+')}+"
60
+ end
61
+ end
62
+
63
+ class RecordWrapper
64
+ delegate_missing_to :record
65
+ delegate :build_node, :max_width_of, :max_attribute_size_of, to: :tablize
66
+
67
+ def initialize(record, tablize)
68
+ @record = record
69
+ @tablize = tablize
70
+ end
71
+
72
+ def valid_to
73
+ record.valid_to.strftime('%Y/%m/%d')
74
+ end
75
+
76
+ def valid_from
77
+ record.valid_from.strftime('%Y/%m/%d')
78
+ end
79
+
80
+ def max_attribute_size_of(attribute)
81
+ tablize.max_attribute_size_of(attribute)
82
+ end
83
+
84
+ def build_roof
85
+ build_cap(:valid_to)
86
+ end
87
+
88
+ def build_floor
89
+ build_cap(:valid_from)
90
+ end
91
+
92
+ def build_cap(duration_attribute)
93
+ duration_node = build_node(send(duration_attribute).to_s, '-')
94
+ [duration_node] + tablize.attributes.map do |attribute|
95
+ build_node('-' * max_width_of(attribute), '-')
96
+ end
97
+ end
98
+
99
+ def build_wall
100
+ duration_node = build_node(' ' * max_width_of(:valid_from), ' ')
101
+ [duration_node] + tablize.attributes.map do |attribute|
102
+ padding = [max_width_of(attribute) - send(attribute).to_s.half_width_size, 0].max
103
+ build_node(send(attribute).to_s + (' ' * padding), ' ')
104
+ end
105
+ end
106
+
107
+ private
108
+
109
+ attr :record, :tablize
110
+ end
111
+ end
@@ -0,0 +1,14 @@
1
+ module ActiveRecord
2
+ module Bitemporal
3
+ module Tablize
4
+ module StringHalfWidthSize
5
+ refine String do
6
+ # 半角換算のサイズ
7
+ def half_width_size
8
+ length + chars.reject(&:ascii_only?).length
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Activerecord
4
+ module Bitemporal
5
+ module Tablize
6
+ VERSION = '0.1.0'
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-bitemporal-tablize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - lighty
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-06-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Displaying the history of records managed by ActiveRecord::Bitemporal
14
+ in a tabular format.
15
+ email:
16
+ - hikalin8686@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".github/workflows/main.yml"
22
+ - ".gitignore"
23
+ - ".rspec"
24
+ - Gemfile
25
+ - README.md
26
+ - Rakefile
27
+ - activerecord-bitemporal-tablize.gemspec
28
+ - bin/console
29
+ - bin/setup
30
+ - lib/activerecord/bitemporal.rb
31
+ - lib/activerecord/bitemporal/tablize.rb
32
+ - lib/activerecord/bitemporal/tablize/string_half_width_size.rb
33
+ - lib/activerecord/bitemporal/tablize/version.rb
34
+ homepage: https://github.com/lighty/activerecord-bitemporal-tablize
35
+ licenses: []
36
+ metadata:
37
+ homepage_uri: https://github.com/lighty/activerecord-bitemporal-tablize
38
+ source_code_uri: https://github.com/lighty/activerecord-bitemporal-tablize
39
+ changelog_uri: https://github.com/lighty/activerecord-bitemporal-tablize
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 2.4.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.2.15
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Displaying the history of records managed by ActiveRecord::Bitemporal in
59
+ a tabular format.
60
+ test_files: []