choron_support 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Dockerfile +23 -0
- data/Gemfile.lock +119 -1
- data/Makefile +30 -1
- data/README.md +78 -18
- data/choron_support.gemspec +12 -1
- data/docker-compose.yml +22 -0
- data/lib/choron_support/as_props.rb +46 -0
- data/lib/choron_support/build_form.rb +17 -0
- data/lib/choron_support/domain_delegate.rb +62 -0
- data/lib/choron_support/domains/base.rb +26 -0
- data/lib/choron_support/forms/base.rb +14 -0
- data/lib/choron_support/helper.rb +34 -0
- data/lib/choron_support/props/base.rb +17 -0
- data/lib/choron_support/props/ext/hash.rb +9 -0
- data/lib/choron_support/props/ext/relation.rb +10 -0
- data/lib/choron_support/queries/base.rb +35 -0
- data/lib/choron_support/scope_query.rb +32 -0
- data/lib/choron_support/version.rb +1 -1
- data/lib/choron_support.rb +31 -2
- data/tmp/.keep +0 -0
- metadata +185 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7da95f65bf957254a5b66f62facc2ca0b10aabc0c3aba36e4441229330b565b
|
4
|
+
data.tar.gz: 80b4a4ed2e0be8e67843a4056262adb65a7f66046524572e0b8cac16f8006e9e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 633b6143e63daf2672fccdebbd24f73238a16ec04d7faa248ad6c6cc26626f433dae771fef2ef7c7129f72d5dfcfe5a506225e81f80a75240e681fa60ebaf5b2
|
7
|
+
data.tar.gz: 4f968293d35924ed6fddbe222c59fbb69ae7e538b36bdf71ee118598650bc16d9484dd219c27d2a183762ca04a00f2018aa58aa04f1ae7423ff2b25b04d082e3
|
data/Dockerfile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
FROM ruby:3.1.3
|
2
|
+
|
3
|
+
ENV LANG C.UTF-8
|
4
|
+
|
5
|
+
RUN wget -O /usr/share/keyrings/yarn-archive-keyring-armor.gpg https://dl.yarnpkg.com/debian/pubkey.gpg
|
6
|
+
RUN gpg -o /usr/share/keyrings/yarn-archive-keyring.gpg --dearmor /usr/share/keyrings/yarn-archive-keyring-armor.gpg
|
7
|
+
RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/yarn-archive-keyring.gpg] https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list > /dev/null
|
8
|
+
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
|
9
|
+
|
10
|
+
RUN apt-get update -qq
|
11
|
+
|
12
|
+
RUN apt-get install default-mysql-client -y \
|
13
|
+
&& apt-get install apt-file -y && apt-file update && apt-get install vim graphviz -y
|
14
|
+
|
15
|
+
RUN apt-get install -y nodejs yarn
|
16
|
+
|
17
|
+
RUN apt-get install -y yarn
|
18
|
+
|
19
|
+
RUN rm -rf /var/lib/apt/lists/*
|
20
|
+
|
21
|
+
RUN mkdir /app
|
22
|
+
WORKDIR /app
|
23
|
+
COPY . /app
|
data/Gemfile.lock
CHANGED
@@ -1,21 +1,102 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
choron_support (0.1.
|
4
|
+
choron_support (0.1.2)
|
5
|
+
activesupport
|
5
6
|
|
6
7
|
GEM
|
7
8
|
remote: https://rubygems.org/
|
8
9
|
specs:
|
10
|
+
actionpack (7.0.4)
|
11
|
+
actionview (= 7.0.4)
|
12
|
+
activesupport (= 7.0.4)
|
13
|
+
rack (~> 2.0, >= 2.2.0)
|
14
|
+
rack-test (>= 0.6.3)
|
15
|
+
rails-dom-testing (~> 2.0)
|
16
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
17
|
+
actionview (7.0.4)
|
18
|
+
activesupport (= 7.0.4)
|
19
|
+
builder (~> 3.1)
|
20
|
+
erubi (~> 1.4)
|
21
|
+
rails-dom-testing (~> 2.0)
|
22
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
23
|
+
activemodel (7.0.4)
|
24
|
+
activesupport (= 7.0.4)
|
25
|
+
activerecord (7.0.4)
|
26
|
+
activemodel (= 7.0.4)
|
27
|
+
activesupport (= 7.0.4)
|
28
|
+
activesupport (7.0.4)
|
29
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
30
|
+
i18n (>= 1.6, < 2)
|
31
|
+
minitest (>= 5.1)
|
32
|
+
tzinfo (~> 2.0)
|
9
33
|
ast (2.4.2)
|
34
|
+
binding_ninja (0.2.3)
|
35
|
+
builder (3.2.4)
|
36
|
+
byebug (11.1.3)
|
37
|
+
coderay (1.1.3)
|
38
|
+
concurrent-ruby (1.1.10)
|
39
|
+
crass (1.0.6)
|
10
40
|
diff-lcs (1.5.0)
|
41
|
+
diffy (3.4.2)
|
42
|
+
docile (1.4.0)
|
43
|
+
erubi (1.11.0)
|
44
|
+
factory_bot (6.2.1)
|
45
|
+
activesupport (>= 5.0.0)
|
46
|
+
factory_bot_rails (6.2.0)
|
47
|
+
factory_bot (~> 6.2.0)
|
48
|
+
railties (>= 5.0.0)
|
49
|
+
i18n (1.12.0)
|
50
|
+
concurrent-ruby (~> 1.0)
|
11
51
|
json (2.6.2)
|
52
|
+
loofah (2.19.0)
|
53
|
+
crass (~> 1.0.2)
|
54
|
+
nokogiri (>= 1.5.9)
|
55
|
+
method_source (1.0.0)
|
56
|
+
minitest (5.16.3)
|
57
|
+
mysql2 (0.5.4)
|
58
|
+
nokogiri (1.13.9-x86_64-darwin)
|
59
|
+
racc (~> 1.4)
|
60
|
+
nokogiri (1.13.9-x86_64-linux)
|
61
|
+
racc (~> 1.4)
|
12
62
|
parallel (1.22.1)
|
13
63
|
parser (3.1.3.0)
|
14
64
|
ast (~> 2.4.1)
|
65
|
+
proc_to_ast (0.1.0)
|
66
|
+
coderay
|
67
|
+
parser
|
68
|
+
unparser
|
69
|
+
pry (0.14.1)
|
70
|
+
coderay (~> 1.1)
|
71
|
+
method_source (~> 1.0)
|
72
|
+
pry-byebug (3.10.1)
|
73
|
+
byebug (~> 11.0)
|
74
|
+
pry (>= 0.13, < 0.15)
|
75
|
+
pry-rails (0.3.9)
|
76
|
+
pry (>= 0.10.4)
|
77
|
+
racc (1.6.0)
|
78
|
+
rack (2.2.4)
|
79
|
+
rack-test (2.0.2)
|
80
|
+
rack (>= 1.3)
|
81
|
+
rails-dom-testing (2.0.3)
|
82
|
+
activesupport (>= 4.2.0)
|
83
|
+
nokogiri (>= 1.6)
|
84
|
+
rails-html-sanitizer (1.4.3)
|
85
|
+
loofah (~> 2.3)
|
86
|
+
railties (7.0.4)
|
87
|
+
actionpack (= 7.0.4)
|
88
|
+
activesupport (= 7.0.4)
|
89
|
+
method_source
|
90
|
+
rake (>= 12.2)
|
91
|
+
thor (~> 1.0)
|
92
|
+
zeitwerk (~> 2.5)
|
15
93
|
rainbow (3.1.1)
|
16
94
|
rake (13.0.6)
|
17
95
|
regexp_parser (2.6.1)
|
18
96
|
rexml (3.2.5)
|
97
|
+
ridgepole (1.2.0)
|
98
|
+
activerecord (>= 5.1, < 7.1)
|
99
|
+
diffy
|
19
100
|
rspec (3.12.0)
|
20
101
|
rspec-core (~> 3.12.0)
|
21
102
|
rspec-expectations (~> 3.12.0)
|
@@ -28,6 +109,12 @@ GEM
|
|
28
109
|
rspec-mocks (3.12.0)
|
29
110
|
diff-lcs (>= 1.2.0, < 2.0)
|
30
111
|
rspec-support (~> 3.12.0)
|
112
|
+
rspec-parameterized (0.5.2)
|
113
|
+
binding_ninja (>= 0.2.3)
|
114
|
+
parser
|
115
|
+
proc_to_ast
|
116
|
+
rspec (>= 2.13, < 4)
|
117
|
+
unparser
|
31
118
|
rspec-support (3.12.0)
|
32
119
|
rubocop (1.39.0)
|
33
120
|
json (~> 2.3)
|
@@ -42,16 +129,47 @@ GEM
|
|
42
129
|
rubocop-ast (1.24.0)
|
43
130
|
parser (>= 3.1.1.0)
|
44
131
|
ruby-progressbar (1.11.0)
|
132
|
+
simplecov (0.21.2)
|
133
|
+
docile (~> 1.1)
|
134
|
+
simplecov-html (~> 0.11)
|
135
|
+
simplecov_json_formatter (~> 0.1)
|
136
|
+
simplecov-html (0.12.3)
|
137
|
+
simplecov_json_formatter (0.1.4)
|
138
|
+
spring (4.1.0)
|
139
|
+
spring-commands-rspec (1.0.4)
|
140
|
+
spring (>= 0.9.1)
|
141
|
+
thor (1.2.1)
|
142
|
+
tzinfo (2.0.5)
|
143
|
+
concurrent-ruby (~> 1.0)
|
45
144
|
unicode-display_width (2.3.0)
|
145
|
+
unparser (0.6.5)
|
146
|
+
diff-lcs (~> 1.3)
|
147
|
+
parser (>= 3.1.0)
|
148
|
+
webrick (1.7.0)
|
149
|
+
yard (0.9.28)
|
150
|
+
webrick (~> 1.7.0)
|
151
|
+
zeitwerk (2.6.6)
|
46
152
|
|
47
153
|
PLATFORMS
|
48
154
|
x86_64-darwin-20
|
155
|
+
x86_64-linux
|
49
156
|
|
50
157
|
DEPENDENCIES
|
158
|
+
activerecord
|
51
159
|
choron_support!
|
160
|
+
factory_bot_rails
|
161
|
+
mysql2
|
162
|
+
pry-byebug
|
163
|
+
pry-rails
|
52
164
|
rake (~> 13.0)
|
165
|
+
ridgepole
|
53
166
|
rspec (~> 3.0)
|
167
|
+
rspec-parameterized
|
54
168
|
rubocop (~> 1.21)
|
169
|
+
simplecov
|
170
|
+
spring
|
171
|
+
spring-commands-rspec
|
172
|
+
yard
|
55
173
|
|
56
174
|
BUNDLED WITH
|
57
175
|
2.3.26
|
data/Makefile
CHANGED
@@ -1,5 +1,34 @@
|
|
1
1
|
help:
|
2
2
|
cat Makefile
|
3
3
|
echo ""
|
4
|
+
|
4
5
|
build:
|
5
|
-
bundle exec rake build
|
6
|
+
bundle exec rake build
|
7
|
+
|
8
|
+
release:
|
9
|
+
bundle exec rake release
|
10
|
+
|
11
|
+
run:
|
12
|
+
docker-compose up
|
13
|
+
|
14
|
+
web:
|
15
|
+
eval "docker exec -it `docker ps | grep _web_ | cut -d' ' -f1` /bin/bash"
|
16
|
+
|
17
|
+
clean:
|
18
|
+
docker system prune
|
19
|
+
docker system prune --volumes
|
20
|
+
|
21
|
+
down:
|
22
|
+
docker-compose down
|
23
|
+
|
24
|
+
d-build:
|
25
|
+
docker-compose build
|
26
|
+
|
27
|
+
d-build-no-cache:
|
28
|
+
docker-compose build --no-cache
|
29
|
+
|
30
|
+
install:
|
31
|
+
docker-compose run web bash -c "gem uninstall bundler && gem install bundler -v 2.3.13 && bundle install && yarn install"
|
32
|
+
|
33
|
+
spec-table-create:
|
34
|
+
bundle exec ridgepole --config ./spec/rails/config/database.yml --file ./spec/rails/config/schemafile --apply
|
data/README.md
CHANGED
@@ -1,37 +1,97 @@
|
|
1
1
|
# ChoronSupport
|
2
2
|
|
3
|
-
|
3
|
+
Choron Support は Rails に便利な機能をいくつか提供する、生産性を向上するためのGemです
|
4
4
|
|
5
|
-
|
5
|
+
## インストール
|
6
6
|
|
7
|
-
|
7
|
+
```bash
|
8
|
+
$ gem install choron_support
|
9
|
+
```
|
8
10
|
|
9
|
-
|
11
|
+
```Gemfile:ruby
|
12
|
+
gem "choron_support"
|
13
|
+
```
|
10
14
|
|
11
|
-
|
15
|
+
### Rails
|
12
16
|
|
13
|
-
|
17
|
+
* config/initializers/choron.rb を作成し以下のコードを記載してください
|
14
18
|
|
15
|
-
$ gem install choron_support
|
16
19
|
|
17
|
-
|
20
|
+
```config/initializers/choron.rb
|
21
|
+
ChoronSupport.using :all
|
22
|
+
```
|
18
23
|
|
19
|
-
|
24
|
+
## 使い方
|
20
25
|
|
21
|
-
|
26
|
+
* 必要に応じて各種モジュールをincludeすることで利用できます
|
22
27
|
|
23
|
-
|
28
|
+
### AsProps
|
24
29
|
|
25
|
-
|
30
|
+
* TODO
|
26
31
|
|
27
|
-
|
32
|
+
### Domain
|
28
33
|
|
29
|
-
|
34
|
+
* TODO
|
30
35
|
|
31
|
-
|
36
|
+
### Forms
|
32
37
|
|
33
|
-
|
38
|
+
* TODO
|
39
|
+
|
40
|
+
### Query
|
41
|
+
|
42
|
+
* TODO
|
43
|
+
|
44
|
+
## Develop
|
45
|
+
|
46
|
+
Dockerを起動することで開発環境が整います
|
47
|
+
|
48
|
+
* Docker Image の作成
|
49
|
+
|
50
|
+
```bash
|
51
|
+
make d-build
|
52
|
+
```
|
53
|
+
|
54
|
+
* Docker コンテナの起動
|
55
|
+
|
56
|
+
```bash
|
57
|
+
make run
|
58
|
+
```
|
59
|
+
|
60
|
+
* テスト用のDBおよびテーブルの作成 & RSpecの実行
|
61
|
+
* spec/spec_helper.rb を開いて下記にあるDBの作成/Tableの作成のフラグを true に書き換えてから、テストを実行してください
|
62
|
+
* `bin/rspec spec`
|
63
|
+
|
64
|
+
|
65
|
+
## 本Gemの思想
|
66
|
+
|
67
|
+
Railsにはこれまで多くのリファクタリング手法が、多くの人々から提案されてきました。
|
68
|
+
その中で本Gemは以下の思想をサポートするようにしています
|
34
69
|
|
35
|
-
|
70
|
+
* レイヤーを多く作らずにModelへ処理を凝集する
|
71
|
+
* Railsがデフォルトで用意してくれている `controllers`, `models`, `views` といったレイヤーのみをできるだけ使い、独自のレイヤーを**あまり**追加しない
|
72
|
+
* Modelの見通しをよくするためにファイル内の処理を委譲させる
|
73
|
+
* 委譲先のクラスはModel以外からは直接呼び出さない(必ずModelにpublicなメソッドを経由させる)
|
36
74
|
|
37
|
-
|
75
|
+
これによりドメインの知識をModelレイヤーに集めつつ、
|
76
|
+
中規模以上のシステムになってきた際のファットモデルによる問題を解消する取り組みを行います
|
77
|
+
|
78
|
+
### 具体的な取り組み
|
79
|
+
|
80
|
+
Modelの中で行われる処理の中でも、本Gemは以下の処理を簡易に別クラスへ委譲させます
|
81
|
+
|
82
|
+
* ビジネスロジック・ドメインロジック
|
83
|
+
* DBへのアクセス・取得
|
84
|
+
* データを表示するための加工(json化)
|
85
|
+
|
86
|
+
|
87
|
+
---
|
88
|
+
|
89
|
+
以下、TODO
|
90
|
+
|
91
|
+
---
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
## License
|
96
|
+
|
97
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/choron_support.gemspec
CHANGED
@@ -32,7 +32,18 @@ Gem::Specification.new do |spec|
|
|
32
32
|
spec.require_paths = ["lib"]
|
33
33
|
|
34
34
|
# Uncomment to register a new dependency of your gem
|
35
|
-
|
35
|
+
spec.add_dependency "activesupport"
|
36
|
+
spec.add_development_dependency "activerecord"
|
37
|
+
spec.add_development_dependency "ridgepole"
|
38
|
+
spec.add_development_dependency "mysql2"
|
39
|
+
spec.add_development_dependency "spring"
|
40
|
+
spec.add_development_dependency "spring-commands-rspec"
|
41
|
+
spec.add_development_dependency "pry-rails"
|
42
|
+
spec.add_development_dependency "pry-byebug"
|
43
|
+
spec.add_development_dependency "yard"
|
44
|
+
spec.add_development_dependency "factory_bot_rails"
|
45
|
+
spec.add_development_dependency "simplecov"
|
46
|
+
spec.add_development_dependency "rspec-parameterized"
|
36
47
|
|
37
48
|
# For more information and examples about making a new gem, check out our
|
38
49
|
# guide at: https://bundler.io/guides/creating_gem.html
|
data/docker-compose.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
version: '3'
|
2
|
+
services:
|
3
|
+
db:
|
4
|
+
image: mysql:8.0
|
5
|
+
environment:
|
6
|
+
MYSQL_ROOT_PASSWORD: root
|
7
|
+
MYSQL_DATABASE: root
|
8
|
+
ports:
|
9
|
+
- 13306:3306
|
10
|
+
volumes:
|
11
|
+
- ./containers/db/data:/var/lib/mysql
|
12
|
+
web:
|
13
|
+
build: .
|
14
|
+
stdin_open: true
|
15
|
+
tty: true
|
16
|
+
volumes:
|
17
|
+
- .:/app
|
18
|
+
- ./vendor/bundle:/usr/local/bundle
|
19
|
+
links:
|
20
|
+
- db
|
21
|
+
depends_on:
|
22
|
+
- db
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative "props/base"
|
2
|
+
require_relative "props/ext/relation"
|
3
|
+
require_relative "props/ext/hash"
|
4
|
+
module ChoronSupport
|
5
|
+
module AsProps
|
6
|
+
def as_props(type_symbol = nil, **params)
|
7
|
+
serializer = self.__get_props_class(type_symbol, **params)
|
8
|
+
|
9
|
+
if serializer.nil?
|
10
|
+
{}
|
11
|
+
else
|
12
|
+
serializer.as_props(**params)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def __get_props_class(type_symbol, **params)
|
19
|
+
case type_symbol
|
20
|
+
when Symbol, String
|
21
|
+
# 名前空間の例: Serialize::Users
|
22
|
+
namespace = "Props::#{self.class.to_s.pluralize}"
|
23
|
+
# クラス名の例: :foo_bar => FooBar
|
24
|
+
class_name = type_symbol.to_s.classify
|
25
|
+
# 例: Serialize::Users::FooBar
|
26
|
+
props_class_name = "#{namespace}::#{class_name}"
|
27
|
+
when nil
|
28
|
+
namespace = "Props"
|
29
|
+
# 例: User / Master::Plan
|
30
|
+
class_name = self.class.to_s
|
31
|
+
# 例: Props::User
|
32
|
+
props_class_name = "#{namespace}::#{class_name}"
|
33
|
+
else
|
34
|
+
raise ArgumentError
|
35
|
+
end
|
36
|
+
|
37
|
+
begin
|
38
|
+
props_class = props_class_name.constantize
|
39
|
+
|
40
|
+
props_class.new(self)
|
41
|
+
rescue
|
42
|
+
return nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative "forms/base"
|
2
|
+
module ChoronSupport
|
3
|
+
module BuildForm
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
def build_form(type = "", form_params = nil)
|
8
|
+
form_name = "#{type}_form"
|
9
|
+
form_class_name = File.join(controller_path, form_name).classify
|
10
|
+
form_class = form_class_name.constantize
|
11
|
+
init_params = form_params || params
|
12
|
+
|
13
|
+
form_class.new(init_params, current_user)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require_relative "domains/base"
|
2
|
+
|
3
|
+
module ChoronSupport
|
4
|
+
module DomainDelegate
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
# QueryオブジェクトパターンをEasyに使うためのクラスメソッドです
|
11
|
+
# @param [Symbol] method_name Modelに定義されるメソッド名
|
12
|
+
# @param [Choron::Domains::Base] option domain Domainクラスを直接指定することができます。デフォルトはnilです。
|
13
|
+
# @param [Symbol] option domain_to_method 委譲先のDomainクラスの呼び出しメソッドを指定できます。デフォルトは :call です
|
14
|
+
# @exampl
|
15
|
+
# class User < ApplicationRecord
|
16
|
+
# domain_delegate :purchase
|
17
|
+
# #=>
|
18
|
+
# def purchase(item)
|
19
|
+
# Domains::Users::Purchase.new(self).call(item)
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# domain_delegate :purchase, specific: false
|
23
|
+
# #=>
|
24
|
+
# def run_get(item)
|
25
|
+
# Domains::Purchase.new(self).call(item)
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# domain_delegate :purchase, specific: false, class_name: "Domains::Buy", to: :buy_user
|
29
|
+
# #=>
|
30
|
+
# def purchase(item)
|
31
|
+
# Domains::Buy.new(self).buy_user(item)
|
32
|
+
# end
|
33
|
+
# end
|
34
|
+
def self.domain_delegate(method_symbol, specific: true, class_name: nil, to: :call)
|
35
|
+
if class_name.present?
|
36
|
+
domain_class = class_name.constantize
|
37
|
+
else
|
38
|
+
model_name = specific ? self.to_s : nil
|
39
|
+
# 例: Domains::Users::Purchase
|
40
|
+
domain_class = ChoronSupport::Helper.generate_choron_class("Domains", model_name, method_symbol)
|
41
|
+
end
|
42
|
+
|
43
|
+
# 被ることがないようにど__をつけてメソッド名を定義します
|
44
|
+
# 例: :__domains_users_purchase_object__
|
45
|
+
domain_object_method_name = ("__" + domain_class.to_s.underscore.gsub("/", "_") + "_object__").to_sym
|
46
|
+
|
47
|
+
define_method(domain_object_method_name) do
|
48
|
+
# ドメインオブジェクトをインスタンス化したものを返します
|
49
|
+
# このインスタンスに対して後述でデリゲートさせています
|
50
|
+
# 例: Domains::Users::Purchase.new(self)
|
51
|
+
domain_class.new(self)
|
52
|
+
end
|
53
|
+
self.instance_eval { private domain_object_method_name }
|
54
|
+
|
55
|
+
# ドメインオブジェクトにデリゲートさせます
|
56
|
+
# 例: def_delegator :__domains_users_purchase_object__, :call, :purchase
|
57
|
+
# purchase メソッドを __domains_xxx__ の call メソッドにデリゲートする
|
58
|
+
def_delegator domain_object_method_name, to, method_symbol
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ChoronSupport
|
2
|
+
module Domains
|
3
|
+
class Base
|
4
|
+
# @param [ActiveRecord::Base] model
|
5
|
+
def initialize(model)
|
6
|
+
@model = model
|
7
|
+
|
8
|
+
# Modelにアクセスするためのメソッドを作成する
|
9
|
+
# Userであれば user, UserFeedBack であれば user_feed_back というように単数系スネークケースでアクセス可能にする
|
10
|
+
model_method_name = model.class.to_s.underscore.gsub("/", "_")
|
11
|
+
self.define_singleton_method(model_method_name) do
|
12
|
+
@model
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# 各種このメソッドをオーバーライドしてください
|
17
|
+
def call
|
18
|
+
raise NotImplementedError
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
attr_reader :model
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ChoronSupport
|
4
|
+
module Helper
|
5
|
+
class << self
|
6
|
+
def generate_choron_class(namespaces, model_name, class_symbol, exception: true)
|
7
|
+
# 命名規則に従いQueryクラスを自動で探して scope を設定する
|
8
|
+
namespace = begin
|
9
|
+
if namespaces.is_a?(Array)
|
10
|
+
namespaces.join("::")
|
11
|
+
else
|
12
|
+
namespaces.to_s
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
if model_name.present?
|
17
|
+
namespace = "#{namespaces}::#{model_name.pluralize}"
|
18
|
+
end
|
19
|
+
|
20
|
+
# 例: Queries::Users::NotLogined
|
21
|
+
target_class = nil
|
22
|
+
begin
|
23
|
+
target_class = "#{namespace}::#{class_symbol.to_s.camelize}".constantize
|
24
|
+
rescue => e
|
25
|
+
if exception
|
26
|
+
raise e
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
target_class
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module ChoronSupport
|
2
|
+
module Queries
|
3
|
+
class Base
|
4
|
+
# @param [ActiveRecord::Base] model_class
|
5
|
+
def initialize(model_class)
|
6
|
+
@model_class = model_class
|
7
|
+
@records = model_class.try!(:all)
|
8
|
+
end
|
9
|
+
|
10
|
+
# 各種このメソッドをオーバーライドしてください
|
11
|
+
def call
|
12
|
+
raise NotImplementedError
|
13
|
+
# @example 実装例
|
14
|
+
# recordsにはscopeが呼び出された瞬間にチェインされてきたSQL情報が入ったActiveRecord::Relationが入っているため、そのままwhereを繋げていけば良い
|
15
|
+
# records.where(xxx: foo)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
attr_reader :model_class, :records
|
21
|
+
|
22
|
+
# 以下、共通的に使えそうな便利メソッドたちです
|
23
|
+
|
24
|
+
# 渡された文字列をLike文用にサニタイズして返します
|
25
|
+
# @param [String]
|
26
|
+
# @return [String]
|
27
|
+
def like_sanitize(string)
|
28
|
+
str = string.to_s
|
29
|
+
return "" if str.blank?
|
30
|
+
|
31
|
+
ApplicationRecord.sanitize_sql_like(str)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative "queries/base"
|
2
|
+
module ChoronSupport
|
3
|
+
module ScopeQuery
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
# QueryオブジェクトパターンをEasyに使うためのクラスメソッドです
|
8
|
+
# @param [Symbol, String] ActiveRecordの .scope に渡す第一引数
|
9
|
+
# @param [Boolean] option specific Queryクラスの配置箇所の指定に関わるパラメータ。デフォルトtrue。falseのときは名前空間を Queries:: だけにし、trueのときは Queries::{クラス名の複数形}:: にします
|
10
|
+
# @param [Choron::Queries::Base] q 直接Queryクラスを指定したいときはこちらにパラメータで渡してください
|
11
|
+
# @example
|
12
|
+
# class User < ApplicationRecord
|
13
|
+
# query_scope :not_logined
|
14
|
+
# #=> scope :not_logined, Queries::Users::NotLogined.new(self)
|
15
|
+
# end
|
16
|
+
|
17
|
+
def self.scope_query(query, specific: true, class_name: nil)
|
18
|
+
# 直接Queryクラスを指定されていたらすぐにscopeにプロキシして返す
|
19
|
+
if class_name.present?
|
20
|
+
query_class = class_name.to_s.constantize
|
21
|
+
else
|
22
|
+
namespace = "Queries"
|
23
|
+
model_name = specific ? self.to_s : nil
|
24
|
+
query_class = ChoronSupport::Helper.generate_choron_class(namespace, model_name, query)
|
25
|
+
end
|
26
|
+
|
27
|
+
# ActiveRecordのscopeメソッドを呼びます
|
28
|
+
scope(query.to_sym, query_class.new(self))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/choron_support.rb
CHANGED
@@ -1,7 +1,36 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative "choron_support/version"
|
4
|
-
|
5
3
|
module ChoronSupport
|
4
|
+
SUPPORT_FILES = {
|
5
|
+
domains: "choron_support/domain_delegate",
|
6
|
+
queries: "choron_support/scope_query",
|
7
|
+
forms: "choron_support/build_form",
|
8
|
+
props: "choron_support/as_props",
|
9
|
+
}
|
10
|
+
private_constant :SUPPORT_FILES
|
11
|
+
|
6
12
|
class Error < StandardError; end
|
13
|
+
|
14
|
+
def self.using(*module_names)
|
15
|
+
# 何かを使うのであれば共通で利用するもの
|
16
|
+
require "active_support/all"
|
17
|
+
require_relative "choron_support/version"
|
18
|
+
require_relative "choron_support/helper"
|
19
|
+
|
20
|
+
module_names.to_a.each do |module_name|
|
21
|
+
case module_name.to_sym
|
22
|
+
when :all
|
23
|
+
SUPPORT_FILES.each do |key, file_name|
|
24
|
+
require file_name
|
25
|
+
end
|
26
|
+
else
|
27
|
+
file_name = SUPPORT_FILES[module_name.to_sym].to_s
|
28
|
+
if file_name.empty?
|
29
|
+
raise ArgumentError, "Not support #{module_name}, expected names: #{SUPPORT_FILES.keys} and :all"
|
30
|
+
else
|
31
|
+
require file_name
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
7
36
|
end
|
data/tmp/.keep
ADDED
File without changes
|
metadata
CHANGED
@@ -1,15 +1,183 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: choron_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mksava
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
11
|
+
date: 2023-01-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '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: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: ridgepole
|
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: mysql2
|
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: spring
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: spring-commands-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry-rails
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry-byebug
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: yard
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: factory_bot_rails
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: simplecov
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: rspec-parameterized
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
13
181
|
description: By using this library, you can incorporate some useful functions into
|
14
182
|
Ruby on Rails.
|
15
183
|
email:
|
@@ -22,6 +190,7 @@ files:
|
|
22
190
|
- ".rubocop.yml"
|
23
191
|
- CHANGELOG.md
|
24
192
|
- CODE_OF_CONDUCT.md
|
193
|
+
- Dockerfile
|
25
194
|
- Gemfile
|
26
195
|
- Gemfile.lock
|
27
196
|
- LICENSE.txt
|
@@ -29,9 +198,22 @@ files:
|
|
29
198
|
- README.md
|
30
199
|
- Rakefile
|
31
200
|
- choron_support.gemspec
|
201
|
+
- docker-compose.yml
|
32
202
|
- lib/choron_support.rb
|
203
|
+
- lib/choron_support/as_props.rb
|
204
|
+
- lib/choron_support/build_form.rb
|
205
|
+
- lib/choron_support/domain_delegate.rb
|
206
|
+
- lib/choron_support/domains/base.rb
|
207
|
+
- lib/choron_support/forms/base.rb
|
208
|
+
- lib/choron_support/helper.rb
|
209
|
+
- lib/choron_support/props/base.rb
|
210
|
+
- lib/choron_support/props/ext/hash.rb
|
211
|
+
- lib/choron_support/props/ext/relation.rb
|
212
|
+
- lib/choron_support/queries/base.rb
|
213
|
+
- lib/choron_support/scope_query.rb
|
33
214
|
- lib/choron_support/version.rb
|
34
215
|
- sig/choron_support.rbs
|
216
|
+
- tmp/.keep
|
35
217
|
homepage: https://github.com/mksava/choron_support
|
36
218
|
licenses:
|
37
219
|
- MIT
|