coactive 0.4.0 → 0.5.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 55895695a2ad27b38fd2ca9f1d8c38b1135975cd3e3ff41fe3f7e53b4564c5ba
4
- data.tar.gz: e51d7e8e74748cd29284e9795d199c7fe9b2737ecdc79f57412632114c3bdee8
3
+ metadata.gz: e7c25e57f4b9e1c2519945529c1026470f77faa37a59753789e6c821270a3bfd
4
+ data.tar.gz: 447682aaf62af161dbdc3ac8d2a7bebdf5a39ae78a106e6163b86b916e0f69b9
5
5
  SHA512:
6
- metadata.gz: 92e2fbe3d500452f8b9584174e2f34c9383b03e12b2eb70d58324caf5ba48b793bb86d6f8153f5a54c454141e1dd223825d1d3fd1ad1c6b8f0c375a5a2c40a8d
7
- data.tar.gz: c9c6da5d6f236221af0f20927eeff9c4846a3ef5cb3940c481a0409842d98186b778b772f777589743d525b2f0914206ef41206530a3043e91098663e937fafe
6
+ metadata.gz: 9e4df828d91f088d1b99784bc7456cc160633b66961431e36dcb5fba5994659718cde602b4433b4510517daf29a63b4af733e7218aa5cd27066415c8efe92456
7
+ data.tar.gz: 99aa8892d8a1f6324e5e587d13e44259088badc9ab923cd6969c0b746d7c3a8b1bb834827117569f75782c243de4fe43d5324025eb247cff27f04ff04a8fce05
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.5.0
4
+
5
+ * Simplify inspect of context.
6
+ * Add default priority config.
7
+
8
+ ## 0.4.1
9
+
10
+ * Clear registry of coactions when reloaded.
11
+
3
12
  ## 0.4.0
4
13
 
5
14
  * Improve lookup performance for named coactors.
@@ -2,5 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  gem "rails", "~> 5.0.0"
4
4
  gem "sqlite3", "~> 1.3.6"
5
+ gem "loofah", "~> 2.19.1"
5
6
 
6
7
  gemspec path: "../"
@@ -2,5 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  gem "rails", "~> 5.1.0"
4
4
  gem "sqlite3", "~> 1.3.6"
5
+ gem "loofah", "~> 2.19.1"
5
6
 
6
7
  gemspec path: "../"
@@ -2,5 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  gem "rails", "~> 5.2.0"
4
4
  gem "sqlite3", "~> 1.3.6"
5
+ gem "loofah", "~> 2.19.1"
5
6
 
6
7
  gemspec path: "../"
@@ -3,7 +3,11 @@
3
3
  module Coactive
4
4
  class Coaction < Struct.new(:coactor, :name, :options)
5
5
  def priority
6
- options[:priority] || 1 << 63
6
+ options[:priority]
7
+ end
8
+
9
+ def priority=(val)
10
+ options[:priority] = val
7
11
  end
8
12
  end
9
13
  end
@@ -6,16 +6,28 @@ module Coactive
6
6
  module Coactions
7
7
  extend ActiveSupport::Concern
8
8
 
9
- included do
10
- class_attribute :coactions_map
11
- self.coactions_map = {}
9
+ class << self
10
+ class_attribute :registry
11
+ self.registry = {}
12
+
13
+ def [](base, name)
14
+ registry.dig(base, name)
15
+ end
16
+
17
+ def []=(base, name, array)
18
+ registry[base] ||= {}
19
+ registry[base][name] = array
20
+ end
12
21
  end
13
22
 
14
23
  class_methods do
15
24
  def coaction(*names, **options)
25
+ base = coactive_config.base_class
16
26
  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] }
27
+ coaction = Coaction.new(self, name, options)
28
+ coaction.priority ||= coactive_config.default_priority
29
+ coactions = (Coactions[base, name].to_a + [coaction])
30
+ Coactions[base, name] = coactions.sort_by.with_index { |coaction, i| [coaction.priority, i] }
19
31
  end
20
32
  end
21
33
  end
@@ -9,6 +9,7 @@ module Coactive
9
9
  use_cache: true,
10
10
  lookup_superclass_for_object: true,
11
11
  lookup_superclass_until: ['ActiveRecord::Base', 'ActiveModel::Base'],
12
+ default_priority: 1 << 63
12
13
  }
13
14
 
14
15
  attr_accessor :data
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'inspector'
4
+
3
5
  module Coactive
4
6
  module Contexts
5
7
  module Inspect
@@ -11,27 +13,7 @@ module Coactive
11
13
 
12
14
  class_methods do
13
15
  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
16
+ data.map { |k, v| "#{k}=#{Coactive::Contexts::Inspector.call(v)}" }.join(', ')
35
17
  end
36
18
  end
37
19
  end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coactive
4
+ module Contexts
5
+ module Inspector
6
+ class << self
7
+ class_attribute :max_num, :max_length, :basic_classes
8
+ self.max_num = 3
9
+ self.max_length = 100
10
+ self.basic_classes = [Module, Symbol, String, Numeric, TrueClass, FalseClass, Regexp]
11
+
12
+ def call(data)
13
+ if data.is_a?(Array)
14
+ inspect_array(data)
15
+ elsif data.is_a?(Hash)
16
+ inspect_hash(data)
17
+ elsif basic_classes.any? { |klass| data.is_a?(klass) }
18
+ inspect_basic(data)
19
+ else
20
+ inspect_object(data)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def inspect_array(data)
27
+ str = data.take(max_num).map { |v| call(v) }.join(', ')
28
+ str += "..." if data.size > max_num
29
+ "[#{str}]"
30
+ end
31
+
32
+ def inspect_hash(data)
33
+ str = data.take(max_num).map { |k, v| "#{k}: #{call(v)}" }.join(', ')
34
+ str += "..." if data.size > max_num
35
+ "{#{str}}"
36
+ end
37
+
38
+ def inspect_basic(data)
39
+ data.inspect
40
+ end
41
+
42
+ def inspect_object(data)
43
+ "#<#{data.class}:#{data.object_id}>"
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -11,11 +11,11 @@ module Coactive
11
11
  private
12
12
 
13
13
  def load_files
14
- Coactive::Loader.call(@config.load_paths)
14
+ Loader.call(@config.load_paths)
15
15
  end
16
16
 
17
17
  def lookup
18
- @config.base_class.coactions_map[@coactant].to_a.map(&:coactor)
18
+ Coactions[@config.base_class, @coactant].to_a.map(&:coactor)
19
19
  end
20
20
 
21
21
  class << self
@@ -5,6 +5,7 @@ module Coactive
5
5
  ActiveSupport::Reloader.to_prepare do
6
6
  Coactive::Loader.loaded.clear
7
7
  Coactive::Lookup.cache.clear
8
+ Coactive::Coactions.registry.clear
8
9
  end
9
10
  end
10
11
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Coactive
4
- VERSION = '0.4.0'
4
+ VERSION = '0.5.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.4.0
4
+ version: 0.5.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: 2023-01-10 00:00:00.000000000 Z
11
+ date: 2023-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -128,6 +128,7 @@ files:
128
128
  - lib/coactive/configure.rb
129
129
  - lib/coactive/context.rb
130
130
  - lib/coactive/contexts/inspect.rb
131
+ - lib/coactive/contexts/inspector.rb
131
132
  - lib/coactive/contextualizer.rb
132
133
  - lib/coactive/errors.rb
133
134
  - lib/coactive/initializer.rb