choron_support 0.1.3 → 0.1.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 17cf217951e1b100acb55aaa6b6413d2371a57a95b408e352eb726523cf8c0e6
4
- data.tar.gz: '09051f84855546db9aba0fc0b8f7b1bd4190bd9a3ccf1f46d5a115f1321e7039'
3
+ metadata.gz: ee00806dd922746ab7a93c6835f09ddc18d78f39f34c5498abd00faad0f02948
4
+ data.tar.gz: 00047a1d9e08362f7cf476518b5c67d93f490f5058260ec5abee3d363c9ccffd
5
5
  SHA512:
6
- metadata.gz: c37870b25a163eb7d66fc0ff92c18de2c080538a80ac5e89601cd166e111af759c3064660b690934d9179f5c9ef6dc7ccb1527810d8961b99ecab5df5408ce1b
7
- data.tar.gz: 2c3b9ae6440edabd7f2a4bbcef54928a69c46ed8799d8f8228db59bf99bc676e064104db5f94753951cf535a3b53eb8b9082788c5232831b19e53e34dcb87018
6
+ metadata.gz: 5142e115d399be76589e39bdf21ea33944a8cd2bc1f3f2c5bcaba15aca77ec9244d27b48325d46a3eeea6f611cc5ecc08abc0e31a920b238a0ec3ec13fbba4c7
7
+ data.tar.gz: c3dadd661f9261167656b268acddf945671dea4cf9ecad9b85d12d42bfc9f11241045ec080fe58a0c39a49443ad8e69901ddf6ecb36885a6a95127b2070ac770
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- choron_support (0.1.3)
4
+ choron_support (0.1.5)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -3,13 +3,19 @@ require_relative "props/ext/relation"
3
3
  require_relative "props/ext/hash"
4
4
  module ChoronSupport
5
5
  module AsProps
6
+ class NameError < StandardError; end
7
+ # @param [Symbol, String, nil] type_symbol どのPropsクラスを利用してPropsを生成するかを指定するシンボル。nilのときはデフォルトのPropsクラスを利用する。
8
+ # @param [Hash] params その他のパラメータ。camel: false を指定すると自動でキャメライズしない。
9
+ # @return [Hash]
6
10
  def as_props(type_symbol = nil, **params)
7
11
  serializer = self.__get_props_class(type_symbol, **params)
8
12
 
13
+ skip_camel = (params[:camel] == false)
14
+ pass_params = params.except(:camel)
9
15
  if serializer.nil?
10
- self.as_json
16
+ skip_camel ? self.as_json : self.as_json.as_camel
11
17
  else
12
- serializer.as_props(**params)
18
+ skip_camel ? serializer.as_props(**pass_params) : serializer.as_props(**pass_params).as_camel
13
19
  end
14
20
  end
15
21
 
@@ -39,15 +45,19 @@ module ChoronSupport
39
45
 
40
46
  props_class.new(self)
41
47
  rescue *rescue_errors
42
- return nil
48
+ if type_symbol.blank?
49
+ return nil
50
+ else
51
+ raise ChoronSupport::AsProps::NameError, "Props class not found: #{props_class_name}. Got type symbol: #{type_symbol}."
52
+ end
43
53
  end
44
54
  end
45
55
 
46
56
  def rescue_errors
47
57
  if defined?(Zeitwerk)
48
- [NameError, Zeitwerk::NameError]
58
+ [::NameError, Zeitwerk::NameError]
49
59
  else
50
- [NameError]
60
+ [::NameError]
51
61
  end
52
62
  end
53
63
  end
@@ -9,7 +9,7 @@ module ChoronSupport
9
9
 
10
10
  # QueryオブジェクトパターンをEasyに使うためのクラスメソッドです
11
11
  # @param [Symbol] method_name Modelに定義されるメソッド名
12
- # @param [Choron::Domains::Base] option domain Domainクラスを直接指定することができます。デフォルトはnilです。
12
+ # @param [Choron::Domains::Base] option domain Domainクラスを文字列で直接指定することができます。シンボルを渡すとクラス化を自動で行います。デフォルトはnilです。
13
13
  # @param [Symbol] option domain_to_method 委譲先のDomainクラスの呼び出しメソッドを指定できます。デフォルトは :call です
14
14
  # @exampl
15
15
  # class User < ApplicationRecord
@@ -32,13 +32,7 @@ module ChoronSupport
32
32
  # end
33
33
  # end
34
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
35
+ domain_class = __generate_choron_domain_class(method_symbol, specific, class_name)
42
36
 
43
37
  # 被ることがないようにど__をつけてメソッド名を定義します
44
38
  # 例: :__domains_users_purchase_object__
@@ -57,6 +51,78 @@ module ChoronSupport
57
51
  # purchase メソッドを __domains_xxx__ の call メソッドにデリゲートする
58
52
  def_delegator domain_object_method_name, to, method_symbol
59
53
  end
54
+
55
+ # domain_delegate とほぼ同じ動きですが、こちらはクラスメソッドをデリゲートするものです。
56
+ # パラメータも同じなのでここでは説明を省略します
57
+ # @example
58
+ # class User < ApplicationRecord
59
+ # class_domain_delegate :import_csv
60
+ # #=>
61
+ # def self.import_csv
62
+ # Domains::Users::ImportCsv.new(self).call
63
+ # end
64
+ #
65
+ # class_domain_delegate :import_csv, specfic: false
66
+ # #=>
67
+ # def self.import_csv(csv_strings)
68
+ # # 細かいことは省略しますが引数もちゃんとデリゲートできるようにしています
69
+ # Domains::ImportCsv.new(self).call(csv_strings)
70
+ # end
71
+ #
72
+ # class_domain_delegate :import_csv, class_name: :manage_csv, to: :import
73
+ # #=>
74
+ # def self.import_csv
75
+ # Domains::Users::ManageCsv.new(self).import
76
+ # end
77
+ def self.class_domain_delegate(method_symbol, specific: true, class_name: nil, to: :call)
78
+ domain_class = __generate_choron_domain_class(method_symbol, specific, class_name)
79
+
80
+ # どのような引数でもデリゲートできるようにしています
81
+ define_singleton_method(method_symbol) do |*params, **keyparams|
82
+ case [!params.empty?, !keyparams.empty?]
83
+ when [true, true]
84
+ domain_class.new(self).send(to, *params, **keyparams)
85
+ when [true, false]
86
+ domain_class.new(self).send(to, *params)
87
+ when [false, true]
88
+ domain_class.new(self).send(to, **keyparams)
89
+ else
90
+ domain_class.new(self).send(to)
91
+ end
92
+ end
93
+ end
94
+
95
+ private
96
+
97
+ def self.__generate_choron_domain_class(method_symbol, specific, class_name)
98
+ # クラス名指定なしのときはメソッド名からクラスを推測する
99
+ if class_name.to_s.empty?
100
+ model_name = specific ? self.to_s : nil
101
+ # @example
102
+ # xxx_delegate :purchase
103
+ # => Domains::Users::Purchase
104
+ # xxx_delegate :purchase, specfic: false
105
+ # => Domains::Purchase
106
+ return ChoronSupport::Helper.generate_choron_class("Domains", model_name, method_symbol)
107
+ end
108
+
109
+ if class_name.is_a?(Symbol)
110
+ # クラス名がシンボルで渡されているときは、シンボル値からクラス名を推測する
111
+ model_name = specific ? self.to_s : nil
112
+ # @example
113
+ # xxx_delegate :purchase, class_name: :paymanet
114
+ # => Domains::Users::Payment
115
+ # xxx_delegate :purchase, class_name: :payment, specifix: false
116
+ # => Domains::Payment
117
+ ChoronSupport::Helper.generate_choron_class("Domains", model_name, class_name)
118
+ else
119
+ # それ以外のときは直接クラスにする
120
+ # @example
121
+ # xxx_delegate :purchase, class_name: "Domains::Payment"
122
+ # => Domains::Payment
123
+ class_name.constantize
124
+ end
125
+ end
60
126
  end
61
127
  end
62
128
  end
@@ -13,7 +13,7 @@ module ChoronSupport
13
13
  end
14
14
  end
15
15
 
16
- if model_name.present?
16
+ unless model_name.to_s.empty?
17
17
  namespace = "#{namespaces}::#{model_name.pluralize}"
18
18
  end
19
19
 
@@ -26,7 +26,7 @@ module ChoronSupport
26
26
  # @return [String]
27
27
  def like_sanitize(string)
28
28
  str = string.to_s
29
- return "" if str.blank?
29
+ return "" if str.empty?
30
30
 
31
31
  ApplicationRecord.sanitize_sql_like(str)
32
32
  end
@@ -16,7 +16,7 @@ module ChoronSupport
16
16
 
17
17
  def self.scope_query(query, specific: true, class_name: nil)
18
18
  # 直接Queryクラスを指定されていたらすぐにscopeにプロキシして返す
19
- if class_name.present?
19
+ if !class_name.to_s.empty?
20
20
  query_class = class_name.to_s.constantize
21
21
  else
22
22
  namespace = "Queries"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ChoronSupport
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.5"
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.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - mksava
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-08 00:00:00.000000000 Z
11
+ date: 2023-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport