coactive 0.3.0 → 0.4.0

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: d34754b34f7bf3ed8e44b8f7b1ef42eff9a8c2915c335e5098e119b891c27053
4
- data.tar.gz: e9194f39d2395961bceb2f88613de90e2f8acb7028af24ffd868c651f78f60c8
3
+ metadata.gz: 55895695a2ad27b38fd2ca9f1d8c38b1135975cd3e3ff41fe3f7e53b4564c5ba
4
+ data.tar.gz: e51d7e8e74748cd29284e9795d199c7fe9b2737ecdc79f57412632114c3bdee8
5
5
  SHA512:
6
- metadata.gz: c78fca95b4d4ffc86a5be1327d2049e9bd8023844aa48ff864185aa5ce6852cd9170fb8080bb412792db11db23b591ca67d79699c8b9ebeecbcc8ce8063247b3
7
- data.tar.gz: 7c95f6e44e30304170a983db92c41ca446605d1b5abc15ffe92a680b76a0fba376ab5b629abf78c7ea6bf39aef81a174358db16628617ee1fe104f817bd6b5f3
6
+ metadata.gz: 92e2fbe3d500452f8b9584174e2f34c9383b03e12b2eb70d58324caf5ba48b793bb86d6f8153f5a54c454141e1dd223825d1d3fd1ad1c6b8f0c375a5a2c40a8d
7
+ data.tar.gz: c9c6da5d6f236221af0f20927eeff9c4846a3ef5cb3940c481a0409842d98186b778b772f777589743d525b2f0914206ef41206530a3043e91098663e937fafe
@@ -8,7 +8,7 @@ jobs:
8
8
  strategy:
9
9
  fail-fast: false
10
10
  matrix:
11
- ruby: [2.3, 2.4, 2.5, 2.6, 2.7, '3.0', 3.1]
11
+ ruby: [2.3, 2.4, 2.5, 2.6, 2.7, '3.0', 3.1, 3.2]
12
12
  gemfile: ['rails50', 'rails51', 'rails52', 'rails60', 'rails61', 'rails70']
13
13
  exclude:
14
14
  - ruby: 2.3
@@ -39,6 +39,12 @@ jobs:
39
39
  gemfile: rails51
40
40
  - ruby: 3.1
41
41
  gemfile: rails52
42
+ - ruby: 3.2
43
+ gemfile: rails50
44
+ - ruby: 3.2
45
+ gemfile: rails51
46
+ - ruby: 3.2
47
+ gemfile: rails52
42
48
 
43
49
  name: ruby ${{ matrix.ruby }}, ${{ matrix.gemfile }}
44
50
 
@@ -46,7 +52,7 @@ jobs:
46
52
  BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
47
53
 
48
54
  steps:
49
- - uses: actions/checkout@v2
55
+ - uses: actions/checkout@v3
50
56
  - uses: ruby/setup-ruby@v1
51
57
  with:
52
58
  ruby-version: ${{ matrix.ruby }}
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.4.0
4
+
5
+ * Improve lookup performance for named coactors.
6
+
3
7
  ## 0.3.0
4
8
 
5
9
  * Reduce inspection size for context.
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coactive
4
+ module Coactants
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ class_attribute :_coactants
9
+ self._coactants = []
10
+ end
11
+
12
+ class_methods do
13
+ def coact(*coactants, **options, &block)
14
+ if block
15
+ self._coactants = _coactants + [block]
16
+ elsif options[:before]
17
+ index = self._coactants.index { |coactant| coactant == options[:before] }
18
+ self._coactants = self._coactants.insert(index, *coactants)
19
+ else
20
+ self._coactants = _coactants + coactants
21
+ end
22
+ end
23
+
24
+ def coactants
25
+ self._coactants
26
+ end
27
+
28
+ def clear_coactants
29
+ self._coactants = []
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coactive
4
+ class Coaction < Struct.new(:coactor, :name, :options)
5
+ def priority
6
+ options[:priority] || 1 << 63
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'coaction'
4
+
5
+ module Coactive
6
+ module Coactions
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ class_attribute :coactions_map
11
+ self.coactions_map = {}
12
+ end
13
+
14
+ class_methods do
15
+ def coaction(*names, **options)
16
+ names.each do |name|
17
+ coactions = coactions_map[name].to_a + [Coaction.new(self, name, options)]
18
+ coactions_map[name] = coactions.sort_by.with_index { |coaction, i| [coaction.priority, i] }
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -2,14 +2,14 @@
2
2
 
3
3
  require_relative 'loader'
4
4
  require_relative 'lookup'
5
- require_relative 'coactors/coactants'
6
- require_relative 'coactors/coactions'
5
+ require_relative 'coactants'
6
+ require_relative 'coactions'
7
7
 
8
8
  module Coactive
9
9
  module Coactor
10
10
  extend ActiveSupport::Concern
11
- include Coactors::Coactants
12
- include Coactors::Coactions
11
+ include Coactants
12
+ include Coactions
13
13
 
14
14
  def coactors
15
15
  self.class._coactants.map do |coactant|
@@ -3,18 +3,34 @@
3
3
  module Coactive
4
4
  module Loader
5
5
  class << self
6
+ class_attribute :loaded
7
+ self.loaded = {}
8
+
6
9
  def call(paths)
7
10
  return unless defined?(Rails)
8
- return if Rails.application.config.eager_load
9
- return if paths.blank?
11
+ return if Rails.application.config.eager_load || paths.blank?
12
+
13
+ Array.wrap(paths).each do |path|
14
+ unless loaded[path]
15
+ load(path)
16
+ loaded[path] = true
17
+ end
18
+ end
19
+ end
20
+
21
+ private
10
22
 
11
- engines = [Rails] + Rails::Engine.subclasses.map(&:instance)
12
- engines.each do |engine|
13
- Dir["#{engine.root}/{#{Array(paths).join(',')}}/**/*.rb"].each do |file|
23
+ def load(path)
24
+ rails_engines.each do |engine|
25
+ Dir["#{engine.root}/#{path}/**/*.rb"].each do |file|
14
26
  require_dependency file
15
27
  end
16
28
  end
17
29
  end
30
+
31
+ def rails_engines
32
+ [Rails] + Rails::Engine.subclasses.map(&:instance)
33
+ end
18
34
  end
19
35
  end
20
36
  end
@@ -6,6 +6,7 @@ module Coactive
6
6
  def initialize(klass, coactant)
7
7
  @klass = klass
8
8
  @coactant = coactant
9
+ @config = @klass.coactive_config
9
10
  end
10
11
 
11
12
  def call
@@ -11,14 +11,11 @@ module Coactive
11
11
  private
12
12
 
13
13
  def load_files
14
- Coactive::Loader.call(@klass.coactive_config.load_paths)
14
+ Coactive::Loader.call(@config.load_paths)
15
15
  end
16
16
 
17
17
  def lookup
18
- @klass.coactive_config.base_class.descendants.each_with_object({}).with_index do |(coactor, hash), i|
19
- coaction = coactor.coactions.detect { |coaction| coaction.name == @coactant }
20
- hash[[coaction.priority || 1 << 63, i]] = coactor if coaction
21
- end.sort.map { |x| x[1] }
18
+ @config.base_class.coactions_map[@coactant].to_a.map(&:coactor)
22
19
  end
23
20
 
24
21
  class << self
@@ -14,13 +14,13 @@ module Coactive
14
14
 
15
15
  if coactant.name.present? && (coactor = resolve(coactant))
16
16
  coactor
17
- elsif @klass.coactive_config.lookup_superclass_for_object && coactant.superclass
17
+ elsif @config.lookup_superclass_for_object && coactant.superclass
18
18
  lookup(coactant.superclass)
19
19
  end
20
20
  end
21
21
 
22
22
  def terminate?(coactant)
23
- coactant.name.to_s.in?(@klass.coactive_config.lookup_superclass_until)
23
+ coactant.name.to_s.in?(@config.lookup_superclass_until)
24
24
  end
25
25
 
26
26
  def resolve(coactant)
@@ -30,7 +30,7 @@ module Coactive
30
30
  end
31
31
 
32
32
  def resolve_name(coactant)
33
- suffix = @klass.coactive_config.class_suffix
33
+ suffix = @config.class_suffix
34
34
  namespace = @klass.name.to_s.sub(/#{suffix}$/, '').to_s
35
35
  [namespace, "#{coactant.name}#{suffix}"].join('::')
36
36
  end
@@ -3,6 +3,7 @@
3
3
  module Coactive
4
4
  module Railtie
5
5
  ActiveSupport::Reloader.to_prepare do
6
+ Coactive::Loader.loaded.clear
6
7
  Coactive::Lookup.cache.clear
7
8
  end
8
9
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Coactive
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coactive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshikazu Kaneta
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-08 00:00:00.000000000 Z
11
+ date: 2023-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -120,10 +120,10 @@ files:
120
120
  - gemfiles/rails70.gemfile
121
121
  - lib/coactive.rb
122
122
  - lib/coactive/base.rb
123
+ - lib/coactive/coactants.rb
124
+ - lib/coactive/coaction.rb
125
+ - lib/coactive/coactions.rb
123
126
  - lib/coactive/coactor.rb
124
- - lib/coactive/coactors/coactants.rb
125
- - lib/coactive/coactors/coaction.rb
126
- - lib/coactive/coactors/coactions.rb
127
127
  - lib/coactive/config.rb
128
128
  - lib/coactive/configure.rb
129
129
  - lib/coactive/context.rb
@@ -159,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
159
  - !ruby/object:Gem::Version
160
160
  version: '0'
161
161
  requirements: []
162
- rubygems_version: 3.1.2
162
+ rubygems_version: 3.3.3
163
163
  signing_key:
164
164
  specification_version: 4
165
165
  summary: Make classes coactive
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Coactive
4
- module Coactors
5
- module Coactants
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- class_attribute :_coactants
10
- self._coactants = []
11
- end
12
-
13
- class_methods do
14
- def coact(*coactants, **options, &block)
15
- if block
16
- self._coactants = _coactants + [block]
17
- elsif options[:before]
18
- index = self._coactants.index { |coactant| coactant == options[:before] }
19
- self._coactants = self._coactants.insert(index, *coactants)
20
- else
21
- self._coactants = _coactants + coactants
22
- end
23
- end
24
-
25
- def coactants
26
- self._coactants
27
- end
28
-
29
- def clear_coactants
30
- self._coactants = []
31
- end
32
- end
33
- end
34
- end
35
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Coactive
4
- module Coactors
5
- class Coaction < Struct.new(:name, :options)
6
- def priority
7
- options[:priority]
8
- end
9
- end
10
- end
11
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'coaction'
4
-
5
- module Coactive
6
- module Coactors
7
- module Coactions
8
- extend ActiveSupport::Concern
9
-
10
- included do
11
- class_attribute :_coactions
12
- self._coactions = []
13
- end
14
-
15
- class_methods do
16
- def coaction(*names, **options)
17
- self._coactions = _coactions + names.map { |name| Coaction.new(name, options) }
18
- end
19
-
20
- def coactions
21
- self._coactions
22
- end
23
-
24
- def clear_coactions
25
- self._coactions = []
26
- end
27
- end
28
- end
29
- end
30
- end