coactive 0.2.2 → 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: cc658ad8e03f5de6bb345b90606d45be83e91ec260e39abcdf4008f2890d6505
4
- data.tar.gz: bbe87c6271b5a42b4f4a08fc4c095d1f2078d9d4d9a4e6b8c8d0d7e05b771725
3
+ metadata.gz: 55895695a2ad27b38fd2ca9f1d8c38b1135975cd3e3ff41fe3f7e53b4564c5ba
4
+ data.tar.gz: e51d7e8e74748cd29284e9795d199c7fe9b2737ecdc79f57412632114c3bdee8
5
5
  SHA512:
6
- metadata.gz: 6f48fcf54084e267df1533eefe6569fca5ee5d36181c8e4ed36ab6327b1cd94b81d1a163f0dfdbe54058c061bc58410d10e925b7fa3d180a95bb0ebe740fe6c5
7
- data.tar.gz: f4f2339513f0292800d5d65862b631c989a0ccd0ae8d3aad3ad9e2f906692b71c1eca0df4684ffbb9e61475d3f1b80c6ed815758949de472da3ff4f6694d14e3
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']
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
@@ -33,6 +33,18 @@ jobs:
33
33
  gemfile: rails51
34
34
  - ruby: 3.0
35
35
  gemfile: rails52
36
+ - ruby: 3.1
37
+ gemfile: rails50
38
+ - ruby: 3.1
39
+ gemfile: rails51
40
+ - ruby: 3.1
41
+ gemfile: rails52
42
+ - ruby: 3.2
43
+ gemfile: rails50
44
+ - ruby: 3.2
45
+ gemfile: rails51
46
+ - ruby: 3.2
47
+ gemfile: rails52
36
48
 
37
49
  name: ruby ${{ matrix.ruby }}, ${{ matrix.gemfile }}
38
50
 
@@ -40,7 +52,7 @@ jobs:
40
52
  BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
41
53
 
42
54
  steps:
43
- - uses: actions/checkout@v2
55
+ - uses: actions/checkout@v3
44
56
  - uses: ruby/setup-ruby@v1
45
57
  with:
46
58
  ruby-version: ${{ matrix.ruby }}
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.4.0
4
+
5
+ * Improve lookup performance for named coactors.
6
+
7
+ ## 0.3.0
8
+
9
+ * Reduce inspection size for context.
10
+
3
11
  ## 0.2.2
4
12
 
5
13
  * Allow overwrite context variables.
data/bin/console CHANGED
@@ -10,5 +10,5 @@ require "coactive"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- # require "irb"
14
- # IRB.start
13
+ require "irb"
14
+ IRB.start
@@ -1,5 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem "rails", "~> 7.0.0"
3
+ gem "rails", "~> 7.0.1"
4
4
 
5
5
  gemspec path: "../"
@@ -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|
@@ -1,7 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'contexts/inspect'
4
+
3
5
  module Coactive
4
6
  class Context
7
+ include Contexts::Inspect
8
+
5
9
  attr_reader :_data
6
10
 
7
11
  def initialize(data = {}, &block)
@@ -25,11 +29,6 @@ module Coactive
25
29
  @_data
26
30
  end
27
31
 
28
- def to_s
29
- attrs = @_data.map { |k, v| "#{k}=#{v.to_s.truncate(300)}" }.join(', ')
30
- "#<#{self.class} #{attrs}>"
31
- end
32
-
33
32
  def define_accessors!(keys)
34
33
  Array(keys).each do |key|
35
34
  define_singleton_method key do
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coactive
4
+ module Contexts
5
+ module Inspect
6
+ extend ActiveSupport::Concern
7
+
8
+ def to_s
9
+ "#<#{self.class} #{self.class.inspect(@_data)}>"
10
+ end
11
+
12
+ class_methods do
13
+ def inspect(data)
14
+ data.map { |k, v| "#{k}=#{Coactive::Contexts::Inspect.call(v)}" }.join(', ')
15
+ end
16
+ end
17
+
18
+ class << self
19
+ class_attribute :max_num, :max_length
20
+ self.max_num = 5
21
+ self.max_length = 300
22
+
23
+ def call(data)
24
+ if data.is_a?(Array)
25
+ str = data.take(max_num).map { |v| call(v) }.join(', ')
26
+ str += '...' if data.size > max_num
27
+ "[#{str}]"
28
+ elsif data.is_a?(Hash)
29
+ str = data.take(max_num).map { |k, v| "#{k}: #{call(v)}" }.join(', ')
30
+ str += '...' if data.size > max_num
31
+ "{#{str}}"
32
+ else
33
+ data.to_s.truncate(max_length)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -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.2.2'
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.2.2
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-01-05 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,13 +120,14 @@ 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
130
+ - lib/coactive/contexts/inspect.rb
130
131
  - lib/coactive/contextualizer.rb
131
132
  - lib/coactive/errors.rb
132
133
  - lib/coactive/initializer.rb
@@ -158,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
159
  - !ruby/object:Gem::Version
159
160
  version: '0'
160
161
  requirements: []
161
- rubygems_version: 3.0.3
162
+ rubygems_version: 3.3.3
162
163
  signing_key:
163
164
  specification_version: 4
164
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