choron_support 0.1.2 → 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: f7da95f65bf957254a5b66f62facc2ca0b10aabc0c3aba36e4441229330b565b
4
- data.tar.gz: 80b4a4ed2e0be8e67843a4056262adb65a7f66046524572e0b8cac16f8006e9e
3
+ metadata.gz: 17cf217951e1b100acb55aaa6b6413d2371a57a95b408e352eb726523cf8c0e6
4
+ data.tar.gz: '09051f84855546db9aba0fc0b8f7b1bd4190bd9a3ccf1f46d5a115f1321e7039'
5
5
  SHA512:
6
- metadata.gz: 633b6143e63daf2672fccdebbd24f73238a16ec04d7faa248ad6c6cc26626f433dae771fef2ef7c7129f72d5dfcfe5a506225e81f80a75240e681fa60ebaf5b2
7
- data.tar.gz: 4f968293d35924ed6fddbe222c59fbb69ae7e538b36bdf71ee118598650bc16d9484dd219c27d2a183762ca04a00f2018aa58aa04f1ae7423ff2b25b04d082e3
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.2)
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
@@ -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 = "#{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.2"
4
+ VERSION = "0.1.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: choron_support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - mksava