choron_support 0.1.3 → 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: 17cf217951e1b100acb55aaa6b6413d2371a57a95b408e352eb726523cf8c0e6
4
- data.tar.gz: '09051f84855546db9aba0fc0b8f7b1bd4190bd9a3ccf1f46d5a115f1321e7039'
3
+ metadata.gz: 5aec7e221e4fc870c2cd989203b23d977c361a55830d4f3ec54732254ef9d68c
4
+ data.tar.gz: 7ce392003f8f967ac35019e436dd29230a36d1b2232c5676b98f86820535fcb9
5
5
  SHA512:
6
- metadata.gz: c37870b25a163eb7d66fc0ff92c18de2c080538a80ac5e89601cd166e111af759c3064660b690934d9179f5c9ef6dc7ccb1527810d8961b99ecab5df5408ce1b
7
- data.tar.gz: 2c3b9ae6440edabd7f2a4bbcef54928a69c46ed8799d8f8228db59bf99bc676e064104db5f94753951cf535a3b53eb8b9082788c5232831b19e53e34dcb87018
6
+ metadata.gz: 3a1eb4b85d33c2e40060b1b4d9941f432a06ba034dc6f8dee696817969bb4da9b7456ceac7be5bce90ce090ffd02e995c593587f0894bfd0f7645017d777b496
7
+ data.tar.gz: 7f88d5b647058c03b70f0e95fb02df0b757ea11c2c5c2493a9da4346c876d81713e4c3a7576514c03a9471f5e27f31e1a9eb56febe3aeb5353cefa5e44132148
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.4)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -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.4"
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.4
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-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport