activerecord-bitemporal-tablize 0.1.0 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae131bf581a0e9a7bfef125b093fac9e69238dfb9cae955a8618f8bb74a8fdba
4
- data.tar.gz: 3c6919b96f711bd2729d885677783e9752810ff4d98231bd846d51ad1dec6420
3
+ metadata.gz: '09a8d3ec50bd99cbd07b14fd9efab561697739d8aee8e7aa82ef9677e81885c3'
4
+ data.tar.gz: e13e3a01b35aac80b6feb6d92fc4dbc028f6da7e278f5e9039f9b09ed211857a
5
5
  SHA512:
6
- metadata.gz: e50e46763fe730ae5cde090f605502d584c5790cf2a73611927ebc6c50a30a1b2acadf10f652aa59f4e093b2ab8a7fe175cb41c039c287c3d2966d2f4ac9904b
7
- data.tar.gz: 39abdd88f1351365fd75315a54997ee12547a7ccf2ff746928166693162fed9554f1208b65d077782134da6777aed6eab7970dbf7f7f1a8aa6d6228d9812091d
6
+ metadata.gz: 70b7cb9932c970f2ef0c9bcfa431c89265f27747dc49b6a9481b921a37016e19d00ded4c51210ec1d3e62bb9d8414050fde1e13b38c323e8c9f7920d49ef7fdf
7
+ data.tar.gz: 8116c05478d403487dfd09641f0a4ae4579add18bd3bb829d5cd5d0c8f49854ccfc842d66c91a401f903e7af5e0f96b2552ed614dbe890a899acbb32badbcda5
data/Gemfile.lock ADDED
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activerecord-bitemporal-tablize (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activerecord-bitemporal (0.0.1)
10
+ activesupport (6.1.3.2)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 1.6, < 2)
13
+ minitest (>= 5.1)
14
+ tzinfo (~> 2.0)
15
+ zeitwerk (~> 2.3)
16
+ concurrent-ruby (1.1.8)
17
+ diff-lcs (1.3)
18
+ i18n (1.8.10)
19
+ concurrent-ruby (~> 1.0)
20
+ minitest (5.14.4)
21
+ rake (13.0.3)
22
+ rspec (3.8.0)
23
+ rspec-core (~> 3.8.0)
24
+ rspec-expectations (~> 3.8.0)
25
+ rspec-mocks (~> 3.8.0)
26
+ rspec-core (3.8.0)
27
+ rspec-support (~> 3.8.0)
28
+ rspec-expectations (3.8.1)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.8.0)
31
+ rspec-mocks (3.8.0)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.8.0)
34
+ rspec-support (3.8.0)
35
+ tzinfo (2.0.4)
36
+ concurrent-ruby (~> 1.0)
37
+ zeitwerk (2.4.2)
38
+
39
+ PLATFORMS
40
+ x86_64-linux
41
+
42
+ DEPENDENCIES
43
+ activerecord-bitemporal
44
+ activerecord-bitemporal-tablize!
45
+ activesupport
46
+ rake (~> 13.0)
47
+ rspec (~> 3.0)
48
+
49
+ BUNDLED WITH
50
+ 2.2.15
data/README.md CHANGED
@@ -22,7 +22,58 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ 1. ローカル環境でgemをインストールする
26
+ 2. 以下を.pryrcなどに追加
27
+ - インストールされたパスをロードパスに含める
28
+ - requireする
29
+ ```
30
+ # cat ~/.pryrc
31
+ $: << '/Users/lighty/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-bitemporal-tablize-0.1.3/lib/'
32
+ require 'activerecord-bitemporal-tablize'
33
+ ```
34
+
35
+
36
+ ref: https://qiita.com/kyanny/items/d370efe14ef15d9afacb
37
+
38
+ ```
39
+ [1] pry(main)> Employee.first.histories.print_histories
40
+ (0.8ms) SELECT sqlite_version(*)
41
+ Employee Load (0.3ms) SELECT "employees".* FROM "employees" WHERE "employees"."valid_from" <= ? AND "employees"."valid_to" > ? AND "employees"."deleted_at" IS NULL ORDER BY "employees"."bitemporal_id" ASC LIMIT ? [["valid_from", "2021-06-20 13:21:42.229006"], ["valid_to", "2021-06-20 13:21:42.229006"], ["LIMIT", 1]]
42
+ Employee Load (0.2ms) SELECT "employees".* FROM "employees" WHERE "employees"."deleted_at" IS NULL AND "employees"."bitemporal_id" = ? ORDER BY "employees"."valid_from" DESC [["bitemporal_id", 1]]
43
+ +------------+----+---------+--------+-----------+-------------------------+-------------------------+---------------+------------+------------+------------+-------------------------+-------------------------+
44
+ | | id | unit_id | name | joined_at | created_at | updated_at | bitemporal_id | valid_from | valid_to | deleted_at | transaction_from | transaction_to |
45
+ +-9999/12/31-+----+---------+--------+-----------+-------------------------+-------------------------+---------------+------------+------------+------------+-------------------------+-------------------------+
46
+ | | 1 | 2 | 山田 | | 2021-06-20 01:44:43 UTC | 2021-06-20 01:44:43 UTC | 1 | 2021/04/01 | 9999/12/31 | | 2021-06-20 01:44:43 UTC | 9999-12-31 00:00:00 UTC |
47
+ +-2021/04/01-+----+---------+--------+-----------+-------------------------+-------------------------+---------------+------------+------------+------------+-------------------------+-------------------------+
48
+ +-2021/04/01-+----+---------+--------+-----------+-------------------------+-------------------------+---------------+------------+------------+------------+-------------------------+-------------------------+
49
+ | | 1 | 1 | たけし | | 2021-06-20 01:44:43 UTC | 2021-06-20 01:44:43 UTC | 1 | 2020/01/01 | 2021/04/01 | | 2021-06-20 01:44:43 UTC | 9999-12-31 00:00:00 UTC |
50
+ +-2020/01/01-+----+---------+--------+-----------+-------------------------+-------------------------+---------------+------------+------------+------------+-------------------------+-------------------------+
51
+ => nil
52
+ [2] pry(main)> Employee.first.histories.print_histories_diff
53
+ Employee Load (0.2ms) SELECT "employees".* FROM "employees" WHERE "employees"."valid_from" <= ? AND "employees"."valid_to" > ? AND "employees"."deleted_at" IS NULL ORDER BY "employees"."bitemporal_id" ASC LIMIT ? [["valid_from", "2021-06-20 13:21:44.302316"], ["valid_to", "2021-06-20 13:21:44.302316"], ["LIMIT", 1]]
54
+ Employee Load (0.1ms) SELECT "employees".* FROM "employees" WHERE "employees"."deleted_at" IS NULL AND "employees"."bitemporal_id" = ? ORDER BY "employees"."valid_from" DESC [["bitemporal_id", 1]]
55
+ +------------+---------+--------+-------------------------+------------+------------+
56
+ | | unit_id | name | updated_at | valid_from | valid_to |
57
+ +-9999/12/31-+---------+--------+-------------------------+------------+------------+
58
+ | | 2 | 山田 | 2021-06-20 01:44:43 UTC | 2021/04/01 | 9999/12/31 |
59
+ +-2021/04/01-+---------+--------+-------------------------+------------+------------+
60
+ +-2021/04/01-+---------+--------+-------------------------+------------+------------+
61
+ | | 1 | たけし | 2021-06-20 01:44:43 UTC | 2020/01/01 | 2021/04/01 |
62
+ +-2020/01/01-+---------+--------+-------------------------+------------+------------+
63
+ => nil
64
+ [3] pry(main)> Employee.first.histories.print_histories(:name)
65
+ Employee Load (0.2ms) SELECT "employees".* FROM "employees" WHERE "employees"."valid_from" <= ? AND "employees"."valid_to" > ? AND "employees"."deleted_at" IS NULL ORDER BY "employees"."bitemporal_id" ASC LIMIT ? [["valid_from", "2021-06-20 13:22:54.269095"], ["valid_to", "2021-06-20 13:22:54.269095"], ["LIMIT", 1]]
66
+ Employee Load (0.1ms) SELECT "employees".* FROM "employees" WHERE "employees"."deleted_at" IS NULL AND "employees"."bitemporal_id" = ? ORDER BY "employees"."valid_from" DESC [["bitemporal_id", 1]]
67
+ +------------+--------+
68
+ | | name |
69
+ +-9999/12/31-+--------+
70
+ | | 山田 |
71
+ +-2021/04/01-+--------+
72
+ +-2021/04/01-+--------+
73
+ | | たけし |
74
+ +-2020/01/01-+--------+
75
+ => nil
76
+ ```
26
77
 
27
78
  ## Development
28
79
 
@@ -0,0 +1,2 @@
1
+ require 'activerecord/bitemporal/tablize'
2
+ require 'activerecord/bitemporal/bitemporalize/instance_methods'
@@ -0,0 +1,26 @@
1
+ module ActiveRecord
2
+ module Bitemporal
3
+ module Bitemporalize
4
+ module InstanceMethods
5
+
6
+ def print_histories(*attributes)
7
+ print ActiveRecord::Bitemporal::Tablize.new(records: ordered_histories, attributes: attributes).call
8
+ end
9
+
10
+ def print_histories_diff
11
+ print ActiveRecord::Bitemporal::Tablize.new(records: ordered_histories, diff_only: true).call
12
+ end
13
+
14
+ private
15
+
16
+ def ordered_histories
17
+ if respond_to?(:histories)
18
+ histories
19
+ else
20
+ self.class.ignore_valid_datetime.bitemporal_for(bitemporal_id)
21
+ end.order(valid_from: :desc)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ module ActiveRecord
2
+ module Bitemporal
3
+ module StringHalfWidthSize
4
+ refine String do
5
+ # 半角換算のサイズ
6
+ def half_width_size
7
+ length + chars.reject(&:ascii_only?).length
8
+ end
9
+ end
10
+ end
11
+
12
+ end
13
+ end
@@ -1,14 +1,16 @@
1
- require_relative 'tablize/version'
2
- require 'lib/activerecord/bitemporal/tablize/string_half_width_size'
1
+ require 'activerecord/bitemporal/tablize/version'
2
+ require 'activerecord/bitemporal/string_half_width_size'
3
+ require 'active_support/all'
3
4
 
4
5
  class ActiveRecord::Bitemporal::Tablize
5
- using StringHalfWidthSize
6
+ using ActiveRecord::Bitemporal::StringHalfWidthSize
6
7
 
7
8
  attr :records, :result, :attributes
8
9
 
9
- def initialize(records:, attributes: [])
10
+ def initialize(records:, attributes: [], diff_only: false)
10
11
  @records = records.map { |record| RecordWrapper.new(record, self) }
11
- @attributes = attributes
12
+ attributes = attributes.empty? ? all_attributes : attributes
13
+ @attributes = diff_only ? select_having_diff : attributes
12
14
  @result = []
13
15
  end
14
16
 
@@ -33,6 +35,14 @@ class ActiveRecord::Bitemporal::Tablize
33
35
 
34
36
  private
35
37
 
38
+ def all_attributes
39
+ records.first&.then { |r| r.attributes.keys.map(&:to_sym) }
40
+ end
41
+
42
+ def select_having_diff
43
+ all_attributes&.select { |attribute| records.map(&:"#{attribute}").uniq.size != 1 }
44
+ end
45
+
36
46
  def append_header
37
47
  result << "+#{build_roof.join('+')}+"
38
48
  result << "|#{build_wall.join('|')}|"
@@ -56,7 +66,7 @@ class ActiveRecord::Bitemporal::Tablize
56
66
  records.each do |record|
57
67
  result << "+#{record.build_roof.join('+')}+"
58
68
  result << "|#{record.build_wall.join('|')}|"
59
- result << "+#{record.build_roof.join('+')}+"
69
+ result << "+#{record.build_floor.join('+')}+"
60
70
  end
61
71
  end
62
72
 
@@ -3,7 +3,7 @@
3
3
  module Activerecord
4
4
  module Bitemporal
5
5
  module Tablize
6
- VERSION = '0.1.0'
6
+ VERSION = '0.1.4'
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-bitemporal-tablize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - lighty
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-20 00:00:00.000000000 Z
11
+ date: 2021-07-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Displaying the history of records managed by ActiveRecord::Bitemporal
14
14
  in a tabular format.
@@ -22,14 +22,16 @@ files:
22
22
  - ".gitignore"
23
23
  - ".rspec"
24
24
  - Gemfile
25
+ - Gemfile.lock
25
26
  - README.md
26
27
  - Rakefile
27
28
  - activerecord-bitemporal-tablize.gemspec
28
29
  - bin/console
29
30
  - bin/setup
30
- - lib/activerecord/bitemporal.rb
31
+ - lib/activerecord-bitemporal-tablize.rb
32
+ - lib/activerecord/bitemporal/bitemporalize/instance_methods.rb
33
+ - lib/activerecord/bitemporal/string_half_width_size.rb
31
34
  - lib/activerecord/bitemporal/tablize.rb
32
- - lib/activerecord/bitemporal/tablize/string_half_width_size.rb
33
35
  - lib/activerecord/bitemporal/tablize/version.rb
34
36
  homepage: https://github.com/lighty/activerecord-bitemporal-tablize
35
37
  licenses: []
@@ -52,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
54
  - !ruby/object:Gem::Version
53
55
  version: '0'
54
56
  requirements: []
55
- rubygems_version: 3.2.15
57
+ rubygems_version: 3.0.3
56
58
  signing_key:
57
59
  specification_version: 4
58
60
  summary: Displaying the history of records managed by ActiveRecord::Bitemporal in
@@ -1,11 +0,0 @@
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
@@ -1,14 +0,0 @@
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