choron_support 0.1.1 → 0.1.3

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: 7bf7316c3b2d4d00a2ce3a44168fbafaacea812faeaf3f6224931c4a2db818e4
4
- data.tar.gz: 914705ff4dee8cd847a2f7cf74f0fc9f36f2385a0684fae5aae7df84832e50ef
3
+ metadata.gz: 17cf217951e1b100acb55aaa6b6413d2371a57a95b408e352eb726523cf8c0e6
4
+ data.tar.gz: '09051f84855546db9aba0fc0b8f7b1bd4190bd9a3ccf1f46d5a115f1321e7039'
5
5
  SHA512:
6
- metadata.gz: b500f4d6235d6ef95a902189323d4556dcf86145e53d6da4393a915d7ef97f657c634baad788d1131580ff205ae7a1f85d8ea5f92e132f1433118cdf1a68d6b6
7
- data.tar.gz: 909f8933d732bbc947bdd723d7addc9738d8aa552f4086ed3b35e541c789d2309cfc0084e17475e7f11b313cffe9905fdd18cd20eef1e365e04910d5a1283b9d
6
+ metadata.gz: c37870b25a163eb7d66fc0ff92c18de2c080538a80ac5e89601cd166e111af759c3064660b690934d9179f5c9ef6dc7ccb1527810d8961b99ecab5df5408ce1b
7
+ data.tar.gz: 2c3b9ae6440edabd7f2a4bbcef54928a69c46ed8799d8f8228db59bf99bc676e064104db5f94753951cf535a3b53eb8b9082788c5232831b19e53e34dcb87018
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- choron_support (0.1.1)
4
+ choron_support (0.1.3)
5
5
  activesupport
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -27,7 +27,50 @@ ChoronSupport.using :all
27
27
 
28
28
  ### AsProps
29
29
 
30
- * TODO
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
@@ -26,7 +26,7 @@ module ChoronSupport
26
26
  props_class_name = "#{namespace}::#{class_name}"
27
27
  when nil
28
28
  namespace = "Props"
29
- # 例: User
29
+ # 例: User / Master::Plan
30
30
  class_name = self.class.to_s
31
31
  # 例: Props::User
32
32
  props_class_name = "#{namespace}::#{class_name}"
@@ -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
@@ -13,10 +13,22 @@ module ChoronSupport
13
13
  # @param [Symbol] option domain_to_method 委譲先のDomainクラスの呼び出しメソッドを指定できます。デフォルトは :call です
14
14
  # @exampl
15
15
  # class User < ApplicationRecord
16
- # domain_for :purchase
16
+ # domain_delegate :purchase
17
17
  # #=>
18
18
  # def purchase(item)
19
- # Domains::Users::Purchase.new(self).run(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)
20
32
  # end
21
33
  # end
22
34
  def self.domain_delegate(method_symbol, specific: true, class_name: nil, to: :call)
@@ -7,7 +7,7 @@ module ChoronSupport
7
7
 
8
8
  # Modelにアクセスするためのメソッドを作成する
9
9
  # Userであれば user, UserFeedBack であれば user_feed_back というように単数系スネークケースでアクセス可能にする
10
- model_method_name = model.class.to_s.underscore
10
+ model_method_name = model.class.to_s.underscore.gsub("/", "_")
11
11
  self.define_singleton_method(model_method_name) do
12
12
  @model
13
13
  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 = "#{namespace}::#{class_symbol.to_s.camelize}".constantize
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ChoronSupport
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: choron_support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - mksava
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-12-04 00:00:00.000000000 Z
11
+ date: 2023-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport