choron_support 0.1.2 → 0.1.3
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +44 -1
- data/lib/choron_support/as_props.rb +10 -2
- data/lib/choron_support/helper.rb +8 -2
- data/lib/choron_support/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17cf217951e1b100acb55aaa6b6413d2371a57a95b408e352eb726523cf8c0e6
|
4
|
+
data.tar.gz: '09051f84855546db9aba0fc0b8f7b1bd4190bd9a3ccf1f46d5a115f1321e7039'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c37870b25a163eb7d66fc0ff92c18de2c080538a80ac5e89601cd166e111af759c3064660b690934d9179f5c9ef6dc7ccb1527810d8961b99ecab5df5408ce1b
|
7
|
+
data.tar.gz: 2c3b9ae6440edabd7f2a4bbcef54928a69c46ed8799d8f8228db59bf99bc676e064104db5f94753951cf535a3b53eb8b9082788c5232831b19e53e34dcb87018
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -27,7 +27,50 @@ ChoronSupport.using :all
|
|
27
27
|
|
28
28
|
### AsProps
|
29
29
|
|
30
|
-
|
30
|
+
`#as_props` はオブジェクトやモデルをhashに変換するものです。
|
31
|
+
|
32
|
+
名前の `props` の由来は `React` からきています。
|
33
|
+
由来の通り、RailsからJS側へ値を渡す際にオブジェクトをJSON化するために作られました。
|
34
|
+
|
35
|
+
#### 使い方
|
36
|
+
|
37
|
+
* ActiveRecord
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
class User < ApplicationRecord
|
41
|
+
include ChoronSupport::AsProps
|
42
|
+
# id: bigint
|
43
|
+
# name: string
|
44
|
+
end
|
45
|
+
|
46
|
+
# ActiveRecordから利用
|
47
|
+
User.new.as_props
|
48
|
+
#=> { id: nil, name: nil }
|
49
|
+
|
50
|
+
User.create(id: 1, name: "tarou")
|
51
|
+
|
52
|
+
User.find(1).as_props
|
53
|
+
#=> { id: 1, name: "tarou" }
|
54
|
+
|
55
|
+
# ActiveRecord::Relationからでも利用できます
|
56
|
+
users = User.all.as_props
|
57
|
+
#=> [
|
58
|
+
# { id: 1, name: "tarou" },
|
59
|
+
# ]
|
60
|
+
|
61
|
+
class Props::User < ChoronSupport::Props::Base
|
62
|
+
def as_props
|
63
|
+
model
|
64
|
+
.as_json
|
65
|
+
.merge(
|
66
|
+
name: "tanaka #{model.name}"
|
67
|
+
)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
user = User.find(1).as_props
|
72
|
+
#=> { id: 1, name: "tanaka tarou" }
|
73
|
+
```
|
31
74
|
|
32
75
|
### Domain
|
33
76
|
|
@@ -7,7 +7,7 @@ module ChoronSupport
|
|
7
7
|
serializer = self.__get_props_class(type_symbol, **params)
|
8
8
|
|
9
9
|
if serializer.nil?
|
10
|
-
|
10
|
+
self.as_json
|
11
11
|
else
|
12
12
|
serializer.as_props(**params)
|
13
13
|
end
|
@@ -38,9 +38,17 @@ module ChoronSupport
|
|
38
38
|
props_class = props_class_name.constantize
|
39
39
|
|
40
40
|
props_class.new(self)
|
41
|
-
rescue
|
41
|
+
rescue *rescue_errors
|
42
42
|
return nil
|
43
43
|
end
|
44
44
|
end
|
45
|
+
|
46
|
+
def rescue_errors
|
47
|
+
if defined?(Zeitwerk)
|
48
|
+
[NameError, Zeitwerk::NameError]
|
49
|
+
else
|
50
|
+
[NameError]
|
51
|
+
end
|
52
|
+
end
|
45
53
|
end
|
46
54
|
end
|
@@ -17,11 +17,17 @@ module ChoronSupport
|
|
17
17
|
namespace = "#{namespaces}::#{model_name.pluralize}"
|
18
18
|
end
|
19
19
|
|
20
|
+
target_class_name = "#{namespace}::#{class_symbol.to_s.camelize}"
|
21
|
+
# ? 終わりはクラスに変換できないため
|
22
|
+
if target_class_name.end_with?("?")
|
23
|
+
target_class_name.chop!
|
24
|
+
end
|
25
|
+
|
20
26
|
# 例: Queries::Users::NotLogined
|
21
27
|
target_class = nil
|
22
28
|
begin
|
23
|
-
target_class =
|
24
|
-
rescue => e
|
29
|
+
target_class = target_class_name.constantize
|
30
|
+
rescue NameError => e
|
25
31
|
if exception
|
26
32
|
raise e
|
27
33
|
end
|