type_fusion 0.0.1 → 0.0.2

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: ab5cbd551a4587ba748b987afef919d5f6c76e78ebf2ddeebdb42a37f57da3d8
4
- data.tar.gz: 637f311b870a08bf9089fa4d254e79a1e72c5a2a0fa8946e7d7e962cf6bb54d3
3
+ metadata.gz: ce0738a57fd5c0b08679b9ea5fd5fe16ddd786326a8269aa09905acb9b7bb7ac
4
+ data.tar.gz: c55387825018723f436f302d8ec6f842a9e9a635ca0c779a9a498d32a906ed91
5
5
  SHA512:
6
- metadata.gz: d0404f787af472e5667ff5b2df86ba6b5bf4dd788f4d7cc92c611fe1042e2ac73a83c6ebb4d2630743cea6d141bb796e4b25979deb988ab806174d2f4073f7c7
7
- data.tar.gz: ccbbd9629bc53af4f6a70b5bb2dff059b5b11b7b0732a628a93ea02ff5d3e8eb89e9aa09ce80b9bdc80284730045838f8f8deb29fffb592e63a6af56c4c2a992
6
+ metadata.gz: 9ba2541d870cd306acf5a5c8a06367117aff283a28502940424d2931641f9405c85a053684ff73e74a163cce14d15409706f00cf69de7d0c22f8caaf30586cd4
7
+ data.tar.gz: 4b929bacae1bc294fbdde9df386aa4833329252f46a438c8daae53049fb9687fca7296fa0ca3f4e4db9b576bc00241d249c62cf5b500e04561a9dc1ebb50590e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.0.2] - 2023-08-12
4
+
5
+ - Initial Proof-of-Concept release
6
+ - Implementation of the `TypeFusion::Sampler` class
7
+
3
8
  ## [0.0.1] - 2023-08-11
4
9
 
5
10
  - Initial release
data/README.md CHANGED
@@ -16,7 +16,54 @@ gem install type_fusion
16
16
 
17
17
  ## Usage
18
18
 
19
- TODO: Write usage instructions here
19
+ ```ruby
20
+ require "type_fusion"
21
+ ```
22
+
23
+ #### Type sample inside a block
24
+
25
+ ```ruby
26
+ TypeFusion::Sampler.instance.with_sampling do
27
+ # run code you want to type sample here
28
+ end
29
+ ```
30
+
31
+ #### Type sample globally
32
+
33
+ ```ruby
34
+ TypeFusion::Sampler.instance.trace.enable
35
+
36
+ # run code you want to type sample here
37
+
38
+ TypeFusion::Sampler.instance.trace.disable
39
+ ```
40
+
41
+ #### Retrieve the samples
42
+
43
+ ```ruby
44
+ TypeFusion::Sampler.instance.trace.samples
45
+ # => [...]
46
+ ```
47
+
48
+ ```ruby
49
+ TypeFusion::Sampler.instance.trace.samples.first
50
+
51
+ # => #<struct TypeFusion::SampleCall
52
+ # gem_and_version="nokogiri-1.15.4-x86_64-darwin",
53
+ # receiver="Nokogiri",
54
+ # method_name=:parse,
55
+ # location=[
56
+ # "/Users/marcoroth/.anyenv/envs/rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/nokogiri-1.15.4-x86_64-darwin/lib/nokogiri.rb",
57
+ # 43
58
+ # ],
59
+ # parameters=[
60
+ # [:string, :req, String],
61
+ # [:url, :opt, NilClass],
62
+ # [:encoding, :opt, NilClass],
63
+ # [:options, :opt, NilClass]
64
+ # ]
65
+ # >
66
+ ```
20
67
 
21
68
  ## Development
22
69
 
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TypeFusion
4
+ SampleCall = Struct.new(:gem_and_version, :receiver, :method_name, :location, :parameters) do
5
+ def to_s
6
+ JSON.pretty_generate(to_h)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+ require "json"
5
+
6
+ module TypeFusion
7
+ class Sampler
8
+ include Singleton
9
+
10
+ attr_accessor :samples
11
+
12
+ def initialize
13
+ @samples = []
14
+ end
15
+
16
+ def with_sampling
17
+ trace.enable
18
+
19
+ yield if block_given?
20
+
21
+ trace.disable
22
+ end
23
+
24
+ def trace
25
+ @trace ||= TracePoint.trace(:call) do |tracepoint|
26
+ if tracepoint.path.start_with?(gem_path)
27
+ receiver = begin
28
+ tracepoint.binding.receiver.name
29
+ rescue StandardError
30
+ tracepoint.binding.receiver.class.name
31
+ end
32
+
33
+ method_name = tracepoint.method_id
34
+ location = tracepoint.binding.source_location
35
+ gem_and_version = location.first.gsub(gem_path, "").split("/").first
36
+ args = tracepoint.parameters.map(&:reverse).to_h
37
+ parameters = extract_parameters(args, tracepoint.binding)
38
+
39
+ samples << SampleCall.new(
40
+ gem_and_version: gem_and_version,
41
+ receiver: receiver,
42
+ method_name: method_name,
43
+ location: location,
44
+ parameters: parameters,
45
+ )
46
+ end
47
+ end.tap(&:disable)
48
+ end
49
+
50
+ def to_s
51
+ inspect
52
+ end
53
+
54
+ def inspect
55
+ "#<TypeFusion::Sampler sample_count=#{@samples.count}>"
56
+ end
57
+
58
+ def reset!
59
+ @samples = []
60
+ @trace = nil
61
+ end
62
+
63
+ private
64
+
65
+ def type_for_object(object)
66
+ case object
67
+ when Hash
68
+ ["Hash", object.map { |key, value| [key, type_for_object(value)] }]
69
+ when Array
70
+ ["Array", object.map { |value| type_for_object(value) }]
71
+ else
72
+ object.class
73
+ end
74
+ end
75
+
76
+ def extract_parameters(args, binding)
77
+ args.map do |name, kind|
78
+ variable = name.to_s.gsub("*", "").gsub("&", "").to_sym
79
+
80
+ type = if binding.local_variables.include?(variable)
81
+ type_for_object(binding.local_variable_get(variable))
82
+ else
83
+ # *, ** or &
84
+ "unused"
85
+ end
86
+
87
+ [name, kind, type]
88
+ end
89
+ end
90
+
91
+ def gem_path
92
+ "#{Gem.default_path.last}/gems/"
93
+ end
94
+ end
95
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TypeFusion
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
data/lib/type_fusion.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "type_fusion/version"
4
+ require_relative "type_fusion/sample_call"
5
+ require_relative "type_fusion/sampler"
4
6
 
5
7
  module TypeFusion
6
8
  end
data/test.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nokogiri"
4
+ require_relative "lib/type_fusion"
5
+
6
+ TypeFusion::Sampler.instance.with_sampling do
7
+ Nokogiri.parse("<h1></h1>")
8
+ end
9
+
10
+ puts TypeFusion::Sampler.instance.samples
11
+ puts TypeFusion::Sampler.instance.to_s
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: type_fusion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Roth
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-11 00:00:00.000000000 Z
11
+ date: 2023-08-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Community-contributed sample data for Ruby types
14
14
  email:
@@ -26,8 +26,11 @@ files:
26
26
  - README.md
27
27
  - Rakefile
28
28
  - lib/type_fusion.rb
29
+ - lib/type_fusion/sample_call.rb
30
+ - lib/type_fusion/sampler.rb
29
31
  - lib/type_fusion/version.rb
30
32
  - sig/type_fusion.rbs
33
+ - test.rb
31
34
  homepage: https://github.com/marcoroth/type_fusion
32
35
  licenses: []
33
36
  metadata: