choron_support 0.1.10 → 0.1.11

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: 5f5e95a92c449d959b9e9522a05e244461e98fb078b5405c93ceb873fdc5c49b
4
- data.tar.gz: cec6ee644f7813d21e24039aadd30a6047e3a708a48cced1412f593c68be626a
3
+ metadata.gz: 4ed418bdccc564f3988b17a0fe5571d7e7bde690de9381b7e562e9866b4cbf3a
4
+ data.tar.gz: 476fe2f299614945830acea6a253a630dab27b45250cb04c5b60d9dbe14cc831
5
5
  SHA512:
6
- metadata.gz: a77b9097a63624fc09fdea17886eb85861c37272058938739692b812fd728d658602020c1b56f0ac3d2ed7f08e51f3b9b234dac28d23e9590620b81ac6794e08
7
- data.tar.gz: 5ceff8def9787ebf4bdadf656173669f5ef1271cf27e08b41ec22b122b134f1f3e19ef70f59674a3a714eb4061e617fd381ea0a84a8c542b2bb6f9803fc02cba
6
+ metadata.gz: 716153850aa6324f6ad3619b83d9f27694ee9c1a8cea669ecd73e37e0f29212bd5ef9cae7871e3046aac80d55054fef17200dfe48de9a6c8a18c7b0ac3a955a7
7
+ data.tar.gz: 3a68ee8bcfa75ff86b99abd97da1dc662ba7d832b36e1cc56e8d3cd6c9f70dd905886a95f71e94fb32fa2fb86776e8398612a9eef8db9172c31fb0cde1a8b1b6
data/.rubocop.yml CHANGED
@@ -1,13 +1,145 @@
1
+ # 参考
2
+ # * デフォルト値の設定ファイル
3
+ # https://github.com/rubocop/rubocop/blob/master/config/default.yml
4
+ inherit_from: .rubocop_todo.yml
5
+
1
6
  AllCops:
2
- TargetRubyVersion: 2.6
7
+ TargetRubyVersion: 3.1.3
8
+ # Rubocop更新時に新しいルールがあれば適用する
9
+ NewCops: enable
10
+ # 使用しているgemに対して拡張ツールを提案してくれるか。
11
+ # 特に不要(そこまでrubocopに頼っていないため)
12
+ SuggestExtensions: false
13
+ Exclude:
14
+ # デフォルトの設定
15
+ - "vendor/**/*"
16
+ - "node_modules/**/*"
17
+ # テストファイルは対象外にしています
18
+ - "spec/**/*"
19
+ # 設定ファイルは特に変更することがないため
20
+ - "config/**/*"
21
+ # スクリプトファイルは除外(プロダクトとは関係ないため)
22
+ - "scripts/**/*"
23
+ # bin系はあまり変更することがないため
24
+ - "bin/**/*"
3
25
 
26
+ # ドキュメントの無い public class は許可
27
+ Style/Documentation:
28
+ Enabled: false
29
+ # 空のメソッドを許可
30
+ Style/EmptyMethod:
31
+ Enabled: false
32
+ # if 内での個別変数代入を許可
33
+ Style/ConditionalAssignment:
34
+ Enabled: false
35
+ # else 内での if の許可
36
+ Style/IfInsideElse:
37
+ Enabled: false
38
+ # bad: [1,2,3].map { _1.to_s }, good: [1,2,3].map(&:to_s)
39
+ # _1.xxx のほうが読みやすいことが多いので false に変更しています
40
+ Style/SymbolProc:
41
+ Enabled: false
42
+ # 空の else を許可
43
+ Style/EmptyElse:
44
+ Enabled: false
45
+ # 1行の場合に後置ifの強制を許可
46
+ Style/IfUnlessModifier:
47
+ Enabled: false
48
+ # not禁止を許可
49
+ Style/Not:
50
+ Enabled: false
51
+ # ガード句の強制を許可
52
+ Style/GuardClause:
53
+ Enabled: false
54
+ # if文等の空ブロックを許可
55
+ Lint/EmptyConditionalBody:
56
+ Enabled: false
57
+ # 使い分けが面倒なのでダブルで統一
4
58
  Style/StringLiterals:
5
- Enabled: true
6
59
  EnforcedStyle: double_quotes
60
+ # Hashやメソッドの階層化はスペース分だけ
61
+ Layout/FirstHashElementIndentation:
62
+ EnforcedStyle: consistent
7
63
 
8
- Style/StringLiteralsInInterpolation:
9
- Enabled: true
10
- EnforcedStyle: double_quotes
64
+ Metrics/ModuleLength:
65
+ Max: 400
66
+ Metrics/BlockLength:
67
+ Max: 100
68
+ Metrics/AbcSize:
69
+ Max: 65
70
+ Metrics/ClassLength:
71
+ Max: 300
72
+ Metrics/CyclomaticComplexity:
73
+ Max: 30
74
+ Metrics/PerceivedComplexity:
75
+ Max: 30
11
76
 
12
77
  Layout/LineLength:
13
- Max: 120
78
+ Max: 200
79
+ Layout/TrailingEmptyLines:
80
+ Enabled: false
81
+ Layout/HashAlignment:
82
+ Enabled: false
83
+
84
+ # クラスとモジュール名を分けて記載するかどうか
85
+ # 検索容易性を加味してfalseにする
86
+ Style/ClassAndModuleChildren:
87
+ Enabled: false
88
+
89
+ # frozen literal をコメントで書くかどうか
90
+ # Ruby3系からデフォルトtrueなのでfalseにする
91
+ Style/FrozenStringLiteralComment:
92
+ Enabled: false
93
+
94
+ # 1行の長さ。基本長くてもOK
95
+ # Layout/LineLength:
96
+ # Max: 300
97
+
98
+ # メソッド名の長さ
99
+ # わかりやすさ重視とするので長さは考慮しない
100
+ Metrics/MethodLength:
101
+ Enabled: false
102
+
103
+ # Hashの最後の項目にカンマを許すかどうか
104
+ # カンマあったほうが便利なのでfalse
105
+ Style/TrailingCommaInHashLiteral:
106
+ Enabled: false
107
+ Style/TrailingCommaInArguments:
108
+ Enabled: false
109
+ Style/TrailingCommaInArrayLiteral:
110
+ Enabled: false
111
+
112
+ # ブロックコメントを許可するかどうか
113
+ # 使ってもいいではないかということで許可
114
+ Style/BlockComments:
115
+ Enabled: false
116
+
117
+ # self. をつけたほうがわかりやすいときもあるのでfalse(可読性重視)
118
+ Style/RedundantSelf:
119
+ Enabled: false
120
+
121
+ # 大文字の時点で定数と読み取れるので無理にmutableにしなくても良い
122
+ Style/MutableConstant:
123
+ Enabled: false
124
+
125
+ # _つきのメソッドを内部メソッドとしてはやすことがあるため
126
+ Naming/VariableNumber:
127
+ Enabled: false
128
+
129
+ # unless よりも if のほうが見やすいときがあるため
130
+ Style/NegatedIf:
131
+ Enabled: false
132
+
133
+ # _ 始まりの変数を許可するか
134
+ # スコープの狭い変数として利用することがあるため許可する
135
+ Lint/UnderscorePrefixedVariableName:
136
+ Enabled: false
137
+
138
+ # get とか set という名前のついたメソッドを許可するか
139
+ # DSL的なものを利用するときに利用することもあるため許可する
140
+ Naming/AccessorMethodName:
141
+ Enabled: false
142
+
143
+ # DSL的なものを使うためパラメータは7個まで許可
144
+ Metrics/ParameterLists:
145
+ Max: 7
data/.rubocop_todo.yml ADDED
@@ -0,0 +1 @@
1
+ # 必要に応じて記載していってください
data/CHANGELOG.md CHANGED
@@ -2,14 +2,22 @@
2
2
 
3
3
  ## [0.1.0] - 2022-12-04
4
4
 
5
- * Initial release
5
+ - Initial release
6
6
 
7
7
  ## [0.1.8] - 2023-06-04
8
8
 
9
9
  ### `as_props`
10
10
 
11
- * もしPropsクラスが見つからなかった場合にエラーをraiseするように修正
11
+ - もしPropsクラスが見つからなかった場合にエラーをraiseするように修正
12
12
 
13
13
  ### `set_mask_for`
14
14
 
15
- * マスク処理のAPIを提供開始
15
+ - マスク処理のAPIを提供開始
16
+
17
+ ## [0.1.11] - 2024-08-24
18
+
19
+ - `Props` の改善を実施
20
+ - `STI` のサポートを追加
21
+ - `only` や `except` で既存のPropsの一部の値だけを利用できるように修正
22
+ - `Domain` の改善を実施
23
+ - `!` で終わるメソッドもデリゲートできるように修正
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- choron_support (0.1.10)
4
+ choron_support (0.1.11)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.description = "By using this library, you can incorporate some useful functions into Ruby on Rails."
13
13
  spec.homepage = "https://github.com/mksava/choron_support"
14
14
  spec.license = "MIT"
15
- spec.required_ruby_version = ">= 2.6.0"
15
+ spec.required_ruby_version = ">= 3.1"
16
16
 
17
17
  spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
18
 
@@ -34,17 +34,18 @@ Gem::Specification.new do |spec|
34
34
  # Uncomment to register a new dependency of your gem
35
35
  spec.add_dependency "activesupport"
36
36
  spec.add_development_dependency "activerecord"
37
- spec.add_development_dependency "ridgepole"
37
+ spec.add_development_dependency "factory_bot_rails"
38
38
  spec.add_development_dependency "mysql2"
39
+ spec.add_development_dependency "pry-byebug"
40
+ spec.add_development_dependency "pry-rails"
41
+ spec.add_development_dependency "ridgepole"
42
+ spec.add_development_dependency "rspec-parameterized"
43
+ spec.add_development_dependency "simplecov"
39
44
  spec.add_development_dependency "spring"
40
45
  spec.add_development_dependency "spring-commands-rspec"
41
- spec.add_development_dependency "pry-rails"
42
- spec.add_development_dependency "pry-byebug"
43
46
  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"
47
47
 
48
48
  # For more information and examples about making a new gem, check out our
49
49
  # guide at: https://bundler.io/guides/creating_gem.html
50
+ spec.metadata["rubygems_mfa_required"] = "true"
50
51
  end
@@ -5,16 +5,21 @@ require_relative "props/ext/hash"
5
5
  module ChoronSupport
6
6
  module AsProps
7
7
  class NameError < StandardError; end
8
+
8
9
  # @param [Symbol, String, nil] type_symbol どのPropsクラスを利用してPropsを生成するかを指定するシンボル。nilのときはデフォルトのPropsクラスを利用する。
9
- # @param [Hash] params その他のパラメータ。camel: false を指定すると自動でキャメライズしない。
10
+ # @param [Hash] params その他のパラメータ
11
+ # @param [Hash] params params[:camel] false を指定すると自動でキャメライズしない。
12
+ # @param [Hash] params params[:only] 指定した属性のみを出力します
13
+ # @param [Hash] params params[:except] 指定した属性を出力しません
14
+ # @param [Hash] params params[:sti] true を指定すると継承クラスのPropsを利用する
10
15
  # @return [Hash]
11
16
  def as_props(type_symbol = nil, **params)
12
- pass_params = params.except(:camel)
17
+ pass_params = params.except(:camel, :sti)
13
18
 
14
- serializer = self.__get_props_class(type_symbol, pass_params)
19
+ serializer = __get_props_class(type_symbol, pass_params, sti: params[:sti])
15
20
  skip_camel = (params[:camel] == false)
16
21
  if serializer.nil?
17
- skip_camel ? self.as_json : self.as_json.as_camel
22
+ skip_camel ? as_json : as_json.as_camel
18
23
  else
19
24
  skip_camel ? serializer.as_props : serializer.as_props.as_camel
20
25
  end
@@ -22,24 +27,31 @@ module ChoronSupport
22
27
 
23
28
  private
24
29
 
25
- def __get_props_class(type_symbol, params)
30
+ def __get_props_class(type_symbol, params, sti:)
26
31
  case type_symbol
27
32
  when Symbol, String
28
- # 名前空間の例: Serialize::Users
29
- namespace = "Props::#{self.class.to_s.pluralize}"
30
- # クラス名の例: :foo_bar => FooBar
33
+ model_namespace = if sti
34
+ # STIのときは継承クラスのPropsを利用する
35
+ self.class.superclass.to_s.pluralize
36
+ else
37
+ self.class.to_s.pluralize
38
+ end
39
+ # 名前空間の例: Props::Users, Props::RealEstates::Buildings
40
+ namespace = "Props::#{model_namespace}"
41
+ # クラス名の例: :common => Common, :foo_bar => FooBar
31
42
  class_name = type_symbol.to_s.classify
32
- # 例: Serialize::Users::FooBar
43
+ # 例: Props::Users::Common, Props::RealEstates::Buildings::FooBar
33
44
  props_class_name = "#{namespace}::#{class_name}"
34
45
  when Class
46
+ # Classが渡されているときはそのまま利用する
35
47
  given_class = type_symbol
36
- if given_class.ancestors.include?(ChoronSupport::Props::Base)
37
- props_class_name = given_class.to_s
38
- else
39
- raise ArgumentError, "invalid class: #{given_class}"
48
+ unless given_class.method_defined?(:as_props)
49
+ raise ArgumentError, "invalid class: #{given_class}, must be respond to :as_props. self: #{self.class}"
40
50
  end
51
+
52
+ props_class_name = given_class.to_s
41
53
  else
42
- raise ArgumentError, "invalid type_symbol: #{type_symbol}"
54
+ raise ArgumentError, "invalid type_symbol: #{type_symbol.inspect}. self: #{self.class}"
43
55
  end
44
56
 
45
57
  begin
@@ -48,9 +60,11 @@ module ChoronSupport
48
60
  props_class.new(self, params)
49
61
  rescue *rescue_errors
50
62
  if type_symbol.blank?
51
- raise ChoronSupport::AsProps::NameError, "Props class not found: #{props_class_name}. Please create props class."
63
+ raise ChoronSupport::AsProps::NameError,
64
+ "Props class not found: #{props_class_name}. Please create props class. self: #{self.class}"
52
65
  else
53
- raise ChoronSupport::AsProps::NameError, "Props class not found: #{props_class_name}. Got type symbol: #{type_symbol}."
66
+ raise ChoronSupport::AsProps::NameError,
67
+ "Props class not found: #{props_class_name}. Got type symbol: #{type_symbol}. self: #{self.class}"
54
68
  end
55
69
  end
56
70
  end
@@ -35,7 +35,7 @@ module ChoronSupport
35
35
 
36
36
  # 被ることがないようにど__をつけてメソッド名を定義します
37
37
  # 例: :__domains_users_purchase_object__
38
- domain_object_method_name = ("__" + domain_class.to_s.underscore.gsub("/", "_") + "_object__").to_sym
38
+ domain_object_method_name = "__#{domain_class.to_s.underscore.gsub('/', '_')}_object__".to_sym
39
39
 
40
40
  define_method(domain_object_method_name) do
41
41
  # ドメインオブジェクトをインスタンス化したものを返します
@@ -83,8 +83,6 @@ module ChoronSupport
83
83
  end
84
84
  end
85
85
 
86
- private
87
-
88
86
  def self.__generate_choron_domain_class(method_symbol, specific, class_name)
89
87
  # クラス名指定なしのときはメソッド名からクラスを推測する
90
88
  if class_name.to_s.empty?
@@ -5,21 +5,19 @@ module ChoronSupport
5
5
  class << self
6
6
  def generate_choron_class(namespaces, model_name, class_symbol, exception: true)
7
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
8
+ namespace = if namespaces.is_a?(Array)
9
+ namespaces.join("::")
10
+ else
11
+ namespaces.to_s
12
+ end
15
13
 
16
14
  unless model_name.to_s.empty?
17
15
  namespace = "#{namespaces}::#{model_name.pluralize}"
18
16
  end
19
17
 
20
18
  target_class_name = "#{namespace}::#{class_symbol.to_s.camelize}"
21
- # ? 終わりはクラスに変換できないため
22
- if target_class_name.end_with?("?")
19
+ # ? や ! 終わりはクラスに変換できないため
20
+ if target_class_name.end_with?("?") || target_class_name.end_with?("!")
23
21
  target_class_name.chop!
24
22
  end
25
23
 
@@ -1,4 +1,3 @@
1
- require_relative "./private/type_builder"
2
1
  require_relative "./private/setting"
3
2
  module ChoronSupport::Props::Attributes
4
3
  unless defined?(ActiveSupport)
@@ -18,13 +17,7 @@ module ChoronSupport::Props::Attributes
18
17
  # self.skip_meta_mark = true
19
18
  # end
20
19
  class_attribute :skip_meta_mark, default: false
21
- # Propsから自動でtypescriptの型を生成しないときはfalseを設定してください
22
- # @example
23
- # class Props::Foos::Bar
24
- # self.skip_typescript = true
25
- # end
26
- class_attribute :skip_typescript, default: false
27
- # 他のPropsクラスの設定を継承するときに設定されます
20
+ # 他のPropsクラスの設定を継承するときに設定されます。設定できるのは1つだけです
28
21
  class_attribute :inherit_props_class, default: nil
29
22
  # ====================================================
30
23
 
@@ -32,12 +25,13 @@ module ChoronSupport::Props::Attributes
32
25
  # @param [Symbol] method モデル, もしくは to オプションで設定したオブジェクトに対して実行するメソッドを指定してください
33
26
  # @param [Keyword] options その他のオプションを指定してください
34
27
  # @option [Symbol] :to 指定したメソッドを実行するオブジェクトを指定できます
28
+ # @option [Symbol] :name Props化するときに出力する属性(Key)名を指定できます
35
29
  # @option [Symbol | lambda] :if 属性を出力するための条件を指定できます
36
30
  # @option [Symbol] :cast 属性を出力する前に指定したメソッドを実行できます。
37
31
  # @option [Boolean] :default 属性値がnilのときに代わりに出力する値を指定できます
38
32
  # @option [Proc] &block ブロックを渡すとそのブロックの戻り値を属性値として出力します
39
33
  def self.attribute(method, **options, &block)
40
- setting_params = options.merge(method: method, block: block)
34
+ setting_params = options.merge(method:, block:)
41
35
  setting = ChoronSupport::Props::Private::Setting.new(setting_params)
42
36
 
43
37
  self.settings ||= []
@@ -52,34 +46,39 @@ module ChoronSupport::Props::Attributes
52
46
  # end
53
47
  def self.inherit(props_class)
54
48
  # 継承するクラスはProps::Baseを継承している必要があります
55
- unless props_class.ancestors.include?(ChoronSupport::Props::Base)
56
- raise "inherit class must be ChoronSupport::Props::Base. got: #{props_class}"
49
+ unless props_class.method_defined?(:as_props)
50
+ raise "inherit class must be respond to :as_props. #{props_class} is not respond to :as_props"
57
51
  end
58
52
 
59
53
  # 既に継承先が設定されている場合はエラーにします
60
- if self.inherit_props_class.present?
61
- raise "inherit props inherit class already set: #{self.inherit_props_class}.(Only one class can be inherited)"
54
+ if inherit_props_class.present?
55
+ raise "inherit props inherit class already set: #{inherit_props_class}.(Only one class can be inherited)"
62
56
  end
63
57
 
64
58
  self.inherit_props_class = props_class
65
59
  self.settings ||= []
66
- self.inherit_props_class.settings.to_a.each do |setting|
60
+ inherit_props_class.settings.to_a.each do |setting|
67
61
  self.settings << setting
68
62
  end
69
63
  end
70
64
 
71
65
  # Modelに対して関連付けされた別ModelのPropsを結合するためのDSLです
72
66
  # @param [Symbol] method to オプションで指定されたオブジェクトに実行されるメソッドを指定してください
73
- # @param [ChoronSupport::Props::Base] props モデルをProps化するためのクラスを指定してください
67
+ # @param [ChoronSupport::Props::Base | Symbol] props_class モデルをProps化するためのクラス もしくはそれを示す Symbol を指定してください
68
+ # @param [Array<Symbol>] only 指定した属性のみを出力します
69
+ # @param [Array<Symbol>] except 指定した属性を除外して出力します
70
+ # @param [Symbol] sti 継承元のクラスのPropsを利用したいときにtrueを指定してください
74
71
  # @param [Keyword] options その他のオプションを指定してください。詳細は attribute と同じです
75
72
  # @example
76
73
  # class Props::Users::General < ChoronSupport::Props::Base
77
- # relation :posts, props: Props::Posts::General
78
- # #=> { posts: user.posts.as_props(:general) } と同じ結果になる
74
+ # relation :posts, :general, only: %i[id title]
75
+ # #=> { posts: user.posts.as_props(:general, only: %i[id title]) } と同じ結果になる
79
76
  # end
80
- def self.relation(method, props_class, **options)
81
- self.attribute(method, **options) do |model, params|
77
+ def self.relation(method, props_class, only: nil, except: nil, sti: false, **options)
78
+ attribute(method, **options) do |model, params|
82
79
  records = model.send(method)
80
+ # only等の設定はrelationで設定したものを適用する
81
+ params.merge!(only:, except:, sti:)
83
82
  records&.as_props(props_class, **params)
84
83
  end
85
84
  end
@@ -88,9 +87,16 @@ module ChoronSupport::Props::Attributes
88
87
  # @return [Hash] props
89
88
  def as_props
90
89
  _props = {}
90
+ only_params = params[:only].to_a.map(&:to_sym)
91
+ except_params = params[:except].to_a.map(&:to_sym)
91
92
 
92
93
  # DSLの設定を設定する
93
94
  self.class.settings.to_a.each do |setting|
95
+ # 除外設定がある場合はスキップ
96
+ next if except_params.include?(setting.method.to_sym)
97
+ # only設定がある場合はそれ以外をスキップ
98
+ next if only_params.present? && only_params.exclude?(setting.method.to_sym)
99
+
94
100
  _props.merge!(__build_props_attribute__(setting))
95
101
  end
96
102
 
@@ -105,22 +111,22 @@ module ChoronSupport::Props::Attributes
105
111
  private
106
112
 
107
113
  def model
108
- raise NotImplementedError, "model method is not implemented"
114
+ raise NotImplementedError, "model method is not implemented. Please implement model method in your class"
109
115
  end
110
116
 
111
117
  def params
112
- raise NotImplementedError, "params method is not implemented"
118
+ {}
113
119
  end
114
120
 
115
121
  FORMATS = {
116
122
  # HTMLのinput type="date"で使える形式
117
123
  date: "%Y-%m-%d",
118
- datetime: "%Y-%m-%dT%H:%M",
124
+ datetime: "%Y-%m-%dT%H:%M"
119
125
  }.freeze
120
126
  # 型のキャスト指定があってもキャストはしないメソッド
121
127
  CAST_IGNORE_METHODS = [
122
128
  # id は数値のほうが良いため
123
- :id,
129
+ :id
124
130
  ].freeze
125
131
  # @param [Array<Symbol>] Setting
126
132
  def __build_props_attribute__(setting)
@@ -139,27 +145,25 @@ module ChoronSupport::Props::Attributes
139
145
  key = "is_#{key}".to_sym unless key.start_with?("is_")
140
146
  end
141
147
 
142
- # valはこの後の工程で書き換えの可能性があるため注意
143
- val = nil
148
+ # javascriptは!をキーとして使えないので削除する
149
+ key = key.to_s.gsub("!", "").to_sym if key.to_s.end_with?("!")
150
+
144
151
  method = setting.method
145
152
  to = setting.to
146
- if setting.block.present?
147
- val = setting.block.call(model, params)
148
- else
149
- if to == :self
150
- if method.is_a?(Proc)
151
- val = method.call(self)
152
- else
153
- val = send(method)
154
- end
155
- else
156
- if method.is_a?(Proc)
157
- val = method.call(send(to))
158
- else
159
- val = send(to)&.send(method)
160
- end
161
- end
162
- end
153
+ # valはこの後の工程で書き換えの可能性があるため注意
154
+ val = if setting.block.present?
155
+ setting.block.call(model, params)
156
+ elsif to == :self
157
+ if method.is_a?(Proc)
158
+ method.call(self)
159
+ else
160
+ send(method)
161
+ end
162
+ elsif method.is_a?(Proc)
163
+ method.call(send(to))
164
+ else
165
+ send(to)&.send(method)
166
+ end
163
167
 
164
168
  case val
165
169
  when Date
@@ -175,9 +179,7 @@ module ChoronSupport::Props::Attributes
175
179
  end
176
180
  end
177
181
 
178
- if val.nil? && setting.set_default?
179
- val = setting.default
180
- end
182
+ val = setting.default if val.nil? && setting.set_default?
181
183
 
182
184
  attribute[key] = val
183
185
 
@@ -190,7 +192,8 @@ module ChoronSupport::Props::Attributes
190
192
  if ENV["RAILS_ENV"] == "test"
191
193
  mark[:props_class_name] = self.class.name
192
194
  if self.class.inherit_props_class.present?
193
- mark[:inherit_props_class_name] = self.class.inherit_props_class.try(:name) || self.class.inherit_props_class.to_s
195
+ mark[:inherit_props_class_name] =
196
+ self.class.inherit_props_class.try(:name) || self.class.inherit_props_class.to_s
194
197
  end
195
198
  end
196
199
 
@@ -209,7 +212,7 @@ module ChoronSupport::Props::Attributes
209
212
 
210
213
  {
211
214
  type: type_target.class.try(:name).to_s,
212
- model_name: type_target.class.try(:name).try(:demodulize).to_s,
215
+ model_name: type_target.class.try(:name).try(:demodulize).to_s
213
216
  }
214
217
  end
215
218
  end
@@ -7,25 +7,20 @@ module ChoronSupport
7
7
 
8
8
  # @param [ActiveRecord::Base] model Props対象のモデルのインスタンス
9
9
  # @param [Hash] params その他のパラメータ
10
+ # @param [Boolean] params params[:only] 指定した属性のみを出力します
11
+ # @param [Boolean] params params[:except] 指定した属性を出力しません
10
12
  def initialize(model, params = {})
11
13
  @model = model
12
- @params = params
14
+ @params = params.to_h
13
15
  end
14
16
 
15
17
  private
16
18
 
17
19
  # @override
18
- def model
19
- @model
20
- end
20
+ attr_reader :model
21
21
 
22
22
  # @override
23
- def params
24
- @params
25
- end
23
+ attr_reader :params
26
24
  end
27
25
  end
28
26
  end
29
-
30
- __END__
31
- abcd
@@ -6,4 +6,4 @@ class Hash
6
6
  def to_camel_json
7
7
  as_camel_json.to_json
8
8
  end
9
- end
9
+ end
@@ -5,6 +5,6 @@ class ActiveRecord::Relation
5
5
  return {}
6
6
  end
7
7
 
8
- records.map {|record| record.as_props(type_symbol, **params) }
8
+ records.map { |record| record.as_props(type_symbol, **params) }
9
9
  end
10
10
  end
@@ -4,14 +4,14 @@ class ChoronSupport::Props::Private::Setting
4
4
  NO_DEFAULT = Object.new.freeze
5
5
  private_constant :NO_DEFAULT
6
6
  SETTING_ATTRIBUTES = %i[method name to cast default if block].freeze
7
- private_constant :SETTING_ATTRIBUTES
8
7
 
9
- SETTING_ATTRIBUTES.each {|atr_name| attr_reader atr_name }
8
+ SETTING_ATTRIBUTES.each { |atr_name| attr_reader atr_name }
10
9
 
11
10
  def initialize(params)
12
11
  # 不正なオプションがあれば例外を発生させる
13
12
  if (params.keys - SETTING_ATTRIBUTES).present?
14
- raise Error, "invalid params: #{(params.keys - SETTING_ATTRIBUTES).join(", ")}, valid params are #{SETTING_ATTRIBUTES.join(", ")}"
13
+ raise Error,
14
+ "invalid params: #{(params.keys - SETTING_ATTRIBUTES).join(', ')}, valid params are #{SETTING_ATTRIBUTES.join(', ')}"
15
15
  end
16
16
 
17
17
  @method = params[:method]
@@ -26,17 +26,15 @@ class ChoronSupport::Props::Private::Setting
26
26
  end
27
27
 
28
28
  def set_default?
29
- self.default != NO_DEFAULT
29
+ default != NO_DEFAULT
30
30
  end
31
31
 
32
32
  private
33
33
 
34
34
  def check_params!
35
- if name.blank?
36
- raise Error, "name is required"
37
- end
38
- if method.blank?
39
- raise Error, "method is required"
40
- end
35
+ raise Error, "name is required" if name.blank?
36
+ return unless method.blank?
37
+
38
+ raise Error, "method is required"
41
39
  end
42
40
  end
@@ -7,7 +7,7 @@ module ChoronSupport
7
7
  @records = model_class.try!(:all)
8
8
  end
9
9
 
10
- # 各種このメソッドをオーバーライドしてください
10
+ # 各種このメソッドをオーバーライドしてください
11
11
  def call
12
12
  raise NotImplementedError
13
13
  # @example 実装例
@@ -16,12 +16,12 @@ module ChoronSupport
16
16
 
17
17
  def self.scope_query(query, specific: true, class_name: nil)
18
18
  # 直接Queryクラスを指定されていたらすぐにscopeにプロキシして返す
19
- if !class_name.to_s.empty?
20
- query_class = class_name.to_s.constantize
21
- else
19
+ if class_name.to_s.empty?
22
20
  namespace = "Queries"
23
21
  model_name = specific ? self.to_s : nil
24
22
  query_class = ChoronSupport::Helper.generate_choron_class(namespace, model_name, query)
23
+ else
24
+ query_class = class_name.to_s.constantize
25
25
  end
26
26
 
27
27
  # ActiveRecordのscopeメソッドを呼びます
@@ -9,7 +9,7 @@ module ChoronSupport
9
9
  MASKING_VALUES = { string: "****", else: nil }.freeze
10
10
 
11
11
  included do
12
- def self.set_mask_for(*method_symbols, condition: nil)
12
+ def self.set_mask_for(*method_symbols)
13
13
  method_symbols.each do |method_symbol|
14
14
  self.instance_eval { private attr_accessor WITH_OUT_MASK_FLAG_VARIABLE_NAME }
15
15
  define_method(method_symbol) do
@@ -34,11 +34,11 @@ module ChoronSupport
34
34
 
35
35
  # マスクの値に関係なく元の値を取得するためのメソッドです。
36
36
  define_method("danger_without_mask_#{method_symbol}") do
37
- val = nil
37
+ origin = nil
38
38
  self.without_mask do |model|
39
- val = model.send(method_symbol)
39
+ origin = model.send(method_symbol)
40
40
  end
41
- val
41
+ origin
42
42
  end
43
43
  end
44
44
 
@@ -74,4 +74,4 @@ module ChoronSupport
74
74
  end
75
75
  end
76
76
  end
77
- end
77
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ChoronSupport
4
- VERSION = "0.1.10"
4
+ VERSION = "0.1.11"
5
5
  end
@@ -27,7 +27,7 @@ module ChoronSupport
27
27
  module_names.to_a.each do |module_name|
28
28
  case module_name.to_sym
29
29
  when :all
30
- SUPPORT_FILES.each do |key, file_name|
30
+ SUPPORT_FILES.each do |_key, file_name|
31
31
  require file_name
32
32
  end
33
33
  else
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.10
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - mksava
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-04 00:00:00.000000000 Z
11
+ date: 2024-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: ridgepole
42
+ name: factory_bot_rails
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: spring
70
+ name: pry-byebug
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: spring-commands-rspec
84
+ name: pry-rails
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -95,7 +95,7 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: pry-rails
98
+ name: ridgepole
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
@@ -109,7 +109,7 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
- name: pry-byebug
112
+ name: rspec-parameterized
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
@@ -123,7 +123,7 @@ dependencies:
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  - !ruby/object:Gem::Dependency
126
- name: yard
126
+ name: simplecov
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="
@@ -137,7 +137,7 @@ dependencies:
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  - !ruby/object:Gem::Dependency
140
- name: factory_bot_rails
140
+ name: spring
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
143
  - - ">="
@@ -151,7 +151,7 @@ dependencies:
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
153
  - !ruby/object:Gem::Dependency
154
- name: simplecov
154
+ name: spring-commands-rspec
155
155
  requirement: !ruby/object:Gem::Requirement
156
156
  requirements:
157
157
  - - ">="
@@ -165,7 +165,7 @@ dependencies:
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0'
167
167
  - !ruby/object:Gem::Dependency
168
- name: rspec-parameterized
168
+ name: yard
169
169
  requirement: !ruby/object:Gem::Requirement
170
170
  requirements:
171
171
  - - ">="
@@ -188,6 +188,7 @@ extra_rdoc_files: []
188
188
  files:
189
189
  - ".rspec"
190
190
  - ".rubocop.yml"
191
+ - ".rubocop_todo.yml"
191
192
  - CHANGELOG.md
192
193
  - CODE_OF_CONDUCT.md
193
194
  - Dockerfile
@@ -213,8 +214,6 @@ files:
213
214
  - lib/choron_support/props/ext/hash.rb
214
215
  - lib/choron_support/props/ext/relation.rb
215
216
  - lib/choron_support/props/private/setting.rb
216
- - lib/choron_support/props/private/type_builder.rb
217
- - lib/choron_support/props/private/type_generator.rb
218
217
  - lib/choron_support/queries/base.rb
219
218
  - lib/choron_support/scope_query.rb
220
219
  - lib/choron_support/set_mask_for.rb
@@ -229,6 +228,7 @@ metadata:
229
228
  homepage_uri: https://github.com/mksava/choron_support
230
229
  source_code_uri: https://github.com/mksava/choron_support
231
230
  changelog_uri: https://github.com/mksava/choron_support/blob/main/CHANGELOG.md
231
+ rubygems_mfa_required: 'true'
232
232
  post_install_message:
233
233
  rdoc_options: []
234
234
  require_paths:
@@ -237,7 +237,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
237
237
  requirements:
238
238
  - - ">="
239
239
  - !ruby/object:Gem::Version
240
- version: 2.6.0
240
+ version: '3.1'
241
241
  required_rubygems_version: !ruby/object:Gem::Requirement
242
242
  requirements:
243
243
  - - ">="
@@ -1,129 +0,0 @@
1
- # @deprecated
2
- class ChoronSupport::Props::Private::TypeBuilder
3
- RESULT = Struct.new(:file_path, :body, :type_name, :attributes)
4
-
5
- # @return [String]
6
- # @memo
7
- # 必要に応じてoverrideしてください
8
- def self.output_dir
9
- "app/javascript/types/props"
10
- end
11
-
12
- # @return [String]
13
- # @memo
14
- # 必要に応じてoverrideしてください
15
- def self.file_path(props_class)
16
- self.default_build_file_path(props_class)
17
- end
18
-
19
- def initialize(props_class)
20
- @body_buffer = []
21
- @attributes_buffer = []
22
- @props_class = props_class
23
- end
24
-
25
- # 設定値やクラス名からTypeScriptの型を生成する
26
- # @return [RESULT]
27
- # @example
28
- # class Foo::Bars::Staff < Props::Base
29
- # attribute :id, type: "number | null"
30
- # attribute :name, type: "string"
31
- # attribute :is_super, type: "boolean",
32
- # attribute :license_names, type: "Array<string>"
33
- # end
34
- # builder = ChoronSupport::Props::Private::TypeBuilder.new(Foo::Bars::Staff)
35
- # builder.build
36
- # ####====#####
37
- # type Foo_Bars_StaffProps = {
38
- # id: number | null,
39
- # name: string,
40
- # is_super: boolean,
41
- # license_names: Array<string>,
42
- # type: "Foo::Bars::Staff"
43
- # modelName: "Foo::Bar"
44
- # }
45
- # ####====#####
46
- def build
47
- set_type_buffer
48
-
49
- file_path = self.class.file_path(props_class)
50
- body = body_buffer.join("\n")
51
- type_name = build_type_name(props_class)
52
- attributes = attributes_buffer.join("\n")
53
-
54
- RESULT.new(file_path, body, type_name, attributes)
55
- end
56
-
57
- # buildされたTypeScriptの型をファイルに出力する
58
- # @return [RESULT]
59
- def generate
60
- result = self.build
61
-
62
- # 出力用のディレクトリがなければ作成する
63
- if !Dir.exist?(self.class.output_dir)
64
- FileUtils.mkdir_p(self.class.output_dir)
65
- end
66
-
67
- # ファイルを作成する
68
- File.open(result.file_path, "w") do |f|
69
- f.puts(result.body)
70
- end
71
-
72
- result
73
- end
74
-
75
- def __body_buffer__
76
- body_buffer
77
- end
78
-
79
- private
80
-
81
- attr_reader :props_class, :body_buffer, :attributes_buffer
82
-
83
- def self.default_build_file_path(props_class)
84
- # 分かりやすいようにそのままtypenameをファイル名にする
85
- file_name = props_class.name.gsub("::", "_") + ".d.ts"
86
-
87
- if defined?(Rails) && Rails.root.present?
88
- Rails.root.join(self.output_dir, file_name).to_s
89
- else
90
- File.join(self.output_dir, file_name)
91
- end
92
- end
93
-
94
- def set_type_buffer
95
- body_buffer << "type #{build_type_name(props_class)} = {"
96
- attributes_buffer = "{"
97
-
98
- build_attributes(props_class).each do |attr_val|
99
- body_buffer << " #{attr_val}"
100
- attributes_buffer << " #{attr_val}"
101
- end
102
-
103
- body_buffer << "}"
104
- attributes_buffer << "}"
105
- end
106
-
107
- def build_attributes(props_class)
108
- attributes = []
109
- props_class.settings.each do |setting|
110
- attributes << build_attribute(setting)
111
- end
112
-
113
- attributes
114
- end
115
-
116
- def build_attribute(setting)
117
- name = setting.name
118
- _if = setting.if
119
- name_val = "#{name}#{_if ? "?" : ""}"
120
-
121
- type = setting.type
122
-
123
- "#{name_val}: #{type}"
124
- end
125
-
126
- def build_type_name(props_class)
127
- props_class.name.gsub("::", "_")
128
- end
129
- end
@@ -1,62 +0,0 @@
1
- # @deprecated
2
- class ChoronSupport::Props::Private::TypeGenerator
3
- def run
4
- start_output
5
-
6
- results = []
7
- targer_props.each do |props_class|
8
- result = builder_class.new(props_class).generate
9
- results << result
10
- end
11
-
12
- output_results(results)
13
-
14
- results
15
- end
16
-
17
- private
18
-
19
- def target_props_class?(props_class)
20
- props_class.respond_to?(:skip_typescript) && props_class.respond_to?(:settings)
21
- end
22
-
23
- def self.props_base
24
- Props::Base
25
- end
26
-
27
- def targer_props
28
- props_list = []
29
- self.class.props_base.descendants.each do |props_class|
30
- next unless target_props_class?(props_class)
31
- next if props_class.skip_typescript
32
-
33
- props_list << props_class
34
- end
35
-
36
- props_list
37
- end
38
-
39
- def builder_class
40
- ChoronSupport::Props::Private::TypeBuilder
41
- end
42
-
43
- def start_output
44
- log("Start generating TypeScript Props...: #{targer_props.size}")
45
- end
46
-
47
- def output_results(results)
48
- log("Generated TypeScript Props: #{results.size}")
49
- log("for...")
50
- results.each do |result|
51
- log(" #{result.file_path}")
52
- end
53
-
54
- log("Done.")
55
- end
56
-
57
- def log(str)
58
- @logger_method ||= defined?(Rails) ? Rails.logger.method(:info) : method(:puts)
59
-
60
- @logger_method.call(str)
61
- end
62
- end