view_component_devtools 0.1.0 → 0.1.1

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: 21babb25c8660808d7564914dd2105566e39e9954a197643e7862815ea867583
4
- data.tar.gz: c64e1042bef475216ff30b64d7fc8f5ac16d308524bb916d2460ef3a9d3e75a9
3
+ metadata.gz: 133887ab12a90a7f356d8ab4c77475c53ac65e9280769060c1a02f6e9df6043e
4
+ data.tar.gz: fcf6a866579b6a27b71d40ac3c57d8453b76907e9520fb5b74cc03260985820b
5
5
  SHA512:
6
- metadata.gz: f88cbecd121cc209eb6dd9e2a82e792cf1f7636becbc183ab6ea490cf563af7b3318d86246bce203780f79465ad5f457047ad348704732185d3ce952109fded6
7
- data.tar.gz: e9420245c4fdad743a6cec795c2e2c8846d247181436ec3bc0d41f4a1868dc0c793a745f0744be4f565e31976ff5a7a3b70d0ac69a49ebd6c97b271b2727d1d2
6
+ metadata.gz: 9e79c04dd30cdcadb4c80a1dbf4f53410d17a95f2fdcffe4c27e03e98cd718705e89ee56de1f8c82a8a65e784cae21b86b7a0dacdb07dc4e8f95d99a2cdbae09
7
+ data.tar.gz: cbb037a59de31c4d41fb7017d2231f1e58660031f1a8f6c422eda105cf7e00afb5174d5dd7d7df0810510875d704b64db28d925f001c9ce56ee8c3f238e44c97
@@ -13,39 +13,54 @@ module ViewComponentDevtools
13
13
  end
14
14
 
15
15
  def view_component_devtools_prop_names
16
- inherited = superclass.respond_to?(:view_component_devtools_prop_names) ?
17
- superclass.view_component_devtools_prop_names : []
18
- initializer_names = instance_method(:initialize).parameters.filter_map do |kind, name|
19
- name if %i[req opt keyreq key].include?(kind)
16
+ classes = component_class_chain
17
+ declared = classes.flat_map do |component_class|
18
+ component_class.instance_variable_get(:@view_component_devtools_props) || []
19
+ end
20
+ inferred = classes.flat_map do |component_class|
21
+ component_initializer(component_class)&.parameters&.filter_map do |kind, name|
22
+ name if %i[req opt keyreq key].include?(kind)
23
+ end || []
20
24
  end
21
- declared = @view_component_devtools_props || []
22
- trusted = inherited + declared
23
25
 
24
- (trusted + initializer_names).uniq.select do |name|
25
- prop_reader?(name, trusted: trusted.include?(name))
26
+ (declared + inferred).uniq.select do |name|
27
+ prop_reader?(name, classes:, explicitly_declared: declared.include?(name))
26
28
  end
27
29
  end
28
30
 
29
31
  private
30
32
 
31
- def prop_reader?(name, trusted:)
32
- return false unless public_method_defined?(name)
33
+ def component_class_chain
34
+ boundary = defined?(ViewComponent::Base) && self < ViewComponent::Base ?
35
+ ViewComponent::Base : Object
36
+ classes = []
37
+ component_class = self
33
38
 
34
- method = instance_method(name)
35
- callable_without_arguments = method.parameters.none? do |kind, _|
36
- %i[req keyreq].include?(kind)
39
+ while component_class.is_a?(Class) && component_class != boundary
40
+ classes << component_class
41
+ component_class = component_class.superclass
37
42
  end
38
- callable_without_arguments && (trusted || component_class_owner?(method.owner))
43
+
44
+ classes
39
45
  end
40
46
 
41
- def component_class_owner?(owner)
42
- return false unless owner.is_a?(Class)
47
+ def component_initializer(component_class)
48
+ return unless component_class.private_instance_methods(false).include?(:initialize) ||
49
+ component_class.protected_instance_methods(false).include?(:initialize) ||
50
+ component_class.public_instance_methods(false).include?(:initialize)
43
51
 
44
- if defined?(ViewComponent::Base) && self < ViewComponent::Base
45
- owner < ViewComponent::Base
46
- else
47
- self <= owner
52
+ initializer = component_class.instance_method(:initialize)
53
+ initializer if initializer.owner == component_class
54
+ end
55
+
56
+ def prop_reader?(name, classes:, explicitly_declared:)
57
+ return false unless public_method_defined?(name)
58
+
59
+ method = instance_method(name)
60
+ callable_without_arguments = method.parameters.none? do |kind, _|
61
+ %i[req keyreq].include?(kind)
48
62
  end
63
+ callable_without_arguments && (explicitly_declared || classes.include?(method.owner))
49
64
  end
50
65
  end
51
66
 
@@ -71,7 +86,10 @@ module ViewComponentDevtools
71
86
  return identifier if identifier
72
87
  end
73
88
 
74
- self.class.instance_method(:initialize).source_location&.join(":")
89
+ initializer = self.class.send(:component_class_chain).filter_map do |component_class|
90
+ self.class.send(:component_initializer, component_class)
91
+ end.first
92
+ initializer&.source_location&.join(":")
75
93
  end
76
94
  end
77
95
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ViewComponentDevtools
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_component_devtools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ViewComponent DevTools contributors