coactive 0.4.1 → 0.5.1

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: b15dae4695420734d0486669dd7f88d7eeceb8815932dc5f47d37cedf0dd4c61
4
- data.tar.gz: cd7f7aae30957f69c76230b8cb97ca846ad4d4c99f6c02045f54b9c866d2ec68
3
+ metadata.gz: 81adac1b8a25c283b42061c7cea9d08bc4ee06987fe2de6aba8ccee17a40e12d
4
+ data.tar.gz: ae0509c320a8e32f10dff9820d8c92adc60dee58797c6ba1d6befd206d923781
5
5
  SHA512:
6
- metadata.gz: eebff4ac3fdb067eb44a59ffc634e4fdda567663e6be2c30923828a311f29a52679ea2aaf3295e17412ee492ef3705c1ff200a0065f5bfee03bbf36a045dd007
7
- data.tar.gz: e94206eda575c397edc5686fe012cf7ebd66074fa74b1a4215c733d7462b01099a9e2d537041749a039bcfe079bbf4ade6c71d4c2a249e65fa2849175ed94532
6
+ metadata.gz: c501671b40e84f6bf4951318db3e77e67eaa5ac7fc2120f03b6038402a9f61d4dd9ae95c4859307902f9dc5bfb0d83f193d04b1bbc8c3a9c7544b56f46c567d4
7
+ data.tar.gz: 91a4346db771c3b0ddb21c07fcf9892e8580243e80916988159b2b5a11813ea39f50b225dc21bdd74faa2430e38fbf4a1fe6ffb91a2a50d264f6744b261fb50b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.5.1
4
+
5
+ * Fix max length of inspected string.
6
+ * Fix inspect of nil.
7
+ * Fix inspect of objects with id or attributes.
8
+
9
+ ## 0.5.0
10
+
11
+ * Simplify inspect of context.
12
+ * Add default priority config.
13
+
3
14
  ## 0.4.1
4
15
 
5
16
  * Clear registry of coactions when reloaded.
@@ -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
@@ -24,7 +24,9 @@ module Coactive
24
24
  def coaction(*names, **options)
25
25
  base = coactive_config.base_class
26
26
  names.each do |name|
27
- coactions = Coactions[base, name].to_a + [Coaction.new(self, name, options)]
27
+ coaction = Coaction.new(self, name, options)
28
+ coaction.priority ||= coactive_config.default_priority
29
+ coactions = (Coactions[base, name].to_a + [coaction])
28
30
  Coactions[base, name] = coactions.sort_by.with_index { |coaction, i| [coaction.priority, i] }
29
31
  end
30
32
  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,59 @@
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, NilClass, 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
+ inspected = data.inspect
40
+ if inspected.length > max_length
41
+ inspected[0..max_length] + '...'
42
+ else
43
+ inspected
44
+ end
45
+ end
46
+
47
+ def inspect_object(data)
48
+ if data.respond_to?(:id)
49
+ "#<#{data.class}/#{data.id}>"
50
+ elsif data.respond_to?(:attributes) && (attributes = data.attributes).is_a?(Hash)
51
+ "#<#{data.class} #{inspect_hash(attributes)}>"
52
+ else
53
+ "#<#{data.class}:#{data.object_id}>"
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Coactive
4
- VERSION = '0.4.1'
4
+ VERSION = '0.5.1'
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.1
4
+ version: 0.5.1
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-12 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