duckery 0.0.0

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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/bin/duckery +54 -0
  3. data/lib/configuration.rb +11 -0
  4. data/lib/duckery.rb +47 -0
  5. metadata +74 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1558aa65c35fea2aa1f8e82b876a1540edf2a7cfabe4939e8dd65f2bec891149
4
+ data.tar.gz: 39b88535d08e5b190221a1497270b5d2bc9cefc9b338dbff655ca250407485f1
5
+ SHA512:
6
+ metadata.gz: d0e895d4cc074c671eb9395afff2e6490506ee0ba5bf2552cd0af82951a27f426c4db538e9f74d03e37a3b89da999cf4ce010666d56d7a6716776d554c34fe24
7
+ data.tar.gz: caee8936d44e064e1299b24313af786369eb849b6c0bbf5758c93647d78b0840bf31f5de0efb676326f9a4a5b77f2a113f7c94daf90a034c90f23776d0c8d8f7
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'duckery'
5
+ require 'require_all'
6
+
7
+ Duckery.config do |config|
8
+ config.variants = ARGV[1].split(',').map(&:to_sym)
9
+ end
10
+
11
+ # --with-subclasses by default
12
+ # how about module? and its nested?
13
+ # rails
14
+ if defined?(Rails)
15
+ # require '/Users/stewart/Workspace/collabs/api/config/environment'
16
+ # ApplicationRecord.prepend(Duckery)
17
+ Dir[File.join('/Users/stewart/Workspace/collabs/api/app/models', '**', '*.rb')].each do |file_name|
18
+ klass = file_name.split('/').last.sub('.rb', '').classify.safe_constantize
19
+
20
+ next if klass.nil? || klass.superclass != ApplicationRecord
21
+
22
+ Object.send(:remove_const, klass.name)
23
+ load(file_name)
24
+
25
+ missing_variants = Duckery.configuration.variants - Duckery.statistic[klass.name].fetch(:variants, [])
26
+
27
+ next if missing_variants.empty?
28
+
29
+ p "Missing `#{missing_variants.join(', ')}` for #{klass}, please add it to #{File.expand_path(file_name)}"
30
+ end
31
+ else
32
+ existing_classes = ObjectSpace.each_object(Class).to_a
33
+ require_all File.join(ARGV[0], '**', '*.rb')
34
+
35
+ Dir[File.join(ARGV[0], '**', '*.rb')].each do |file_name|
36
+ new_classes = ObjectSpace.each_object(Class).to_a - existing_classes
37
+
38
+ new_classes.reject(&:singleton_class?).each do |klass|
39
+ klass.prepend(Duckery)
40
+
41
+ Duckery.configuration.variants.each do |variant|
42
+ klass.send(variant) rescue nil
43
+ end
44
+
45
+ missing_variants = Duckery.configuration.variants - Duckery.statistic[klass.name].fetch(:variants, [])
46
+
47
+ next if missing_variants.empty?
48
+
49
+ p "Missing `#{missing_variants.join(', ')}` for #{klass}, please add it to #{File.expand_path(file_name)}"
50
+ end
51
+
52
+ existing_classes.concat(new_classes)
53
+ end
54
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Duckery
4
+ class Configuration
5
+ attr_writer :variants
6
+
7
+ def variants
8
+ @variants ||= []
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'configuration'
4
+ require 'duckery/railtie' if defined?(Rails)
5
+
6
+ module Duckery
7
+ def self.prepended(base)
8
+ overiden_classes_module =
9
+ Module.new do
10
+ Duckery.configuration.variants.each do |variant|
11
+ Duckery.statistic[base.name] ||= { variants: [] }
12
+
13
+ # Lookup on self and ancestor for method `variant`
14
+ next unless base.methods.include?(variant)
15
+
16
+ define_method variant do |*args|
17
+ Duckery.statistic[base.name][:variants].push(variant) # Roll call
18
+
19
+ super(*args)
20
+ end
21
+ end
22
+ end
23
+
24
+ base.singleton_class.prepend(overiden_classes_module)
25
+ end
26
+
27
+ def self.configuration
28
+ @configuration ||= Duckery::Configuration.new
29
+ end
30
+
31
+ # TODO: convert to symbols
32
+ def self.variants
33
+ configuration.variants
34
+ end
35
+
36
+ def self.config
37
+ yield(configuration) if block_given?
38
+ end
39
+
40
+ def self.reset_configuration
41
+ @configuration = Duckery::Configuration.new
42
+ end
43
+
44
+ def self.statistic
45
+ @statistic ||= {}
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: duckery
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Stewart Nguyen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-11-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: require_all
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Much longer explanation of the example!
42
+ email: cuongkb3g@gmail.com
43
+ executables:
44
+ - duckery
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - bin/duckery
49
+ - lib/configuration.rb
50
+ - lib/duckery.rb
51
+ homepage: https://rubygems.org/gems/duckery
52
+ licenses:
53
+ - MIT
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.1.2
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Dectect add-on
74
+ test_files: []