async-service 0.2.1 → 0.3.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: 4ab5e7d4ee86d20db8effcf473f60b83a0f2d969b5ef3c461fe7479a2dcc3803
4
- data.tar.gz: 2e72c469ad515af58898dd4a96c7622b613022b7aef631531ba4fb9b65556606
3
+ metadata.gz: 383a3a329442a291c3ec373e55626e1a10ab01e237aa545787f7282c8bace859
4
+ data.tar.gz: 0b3436a1ccf535c15fcd90b59b77dbc3e2638170496a2c7d8298d76134edb3a3
5
5
  SHA512:
6
- metadata.gz: a1c95132372c7a4abf787b76364a92a32cd459909040472d1dbaafad47751254a88a637a1ef7c2d19cae001cb27f4f38f42e5edfec4af280dc09592909f3bb36
7
- data.tar.gz: 9170e30dd17d228c110a7b411df61ee55206f9f67ca0ec50ee2005848d17e47afdcf72c5547934c2f8d3dde6682b29ca7e6d0e43b6f1018abf5c04d6ae3e5c93
6
+ metadata.gz: f419c921dec91bd81a3b0a51e08471017fd3d8a31fb3fcfdf0abb0c53eb044b131f190be5a93f33b37d0a2eee3c4935ce55e3ce67bc3217db01b818abaa5fe24
7
+ data.tar.gz: 84decffdf10d7e0dc269f812e83dc5a780615a6d90ff51237743f1ffa48fd96e7b436516ecc4b1c14ea7ec8b0b379a2e8c613ef3affa0da3bf1b3f4f3103754c
checksums.yaml.gz.sig CHANGED
Binary file
@@ -7,120 +7,123 @@ module Async
7
7
  module Service
8
8
  class Environment
9
9
  class Builder < BasicObject
10
- def self.for(initial, block)
11
- builder = self.new(initial.dup)
10
+ def self.wrap(hash, facet = ::Module.new)
11
+ hash.each do |key, value|
12
+ if value.is_a?(::Proc)
13
+ facet.define_method(key, &value)
14
+ else
15
+ facet.define_method(key){value}
16
+ end
17
+ end
18
+
19
+ return facet
20
+ end
21
+
22
+ def self.for(facet = ::Module.new, &block)
23
+ builder = self.new(facet)
12
24
 
13
- builder.instance_exec(&block) if block
25
+ builder.instance_exec(&block) if block_given?
14
26
 
15
- return builder
27
+ return facet
16
28
  end
17
29
 
18
- def initialize(cache = ::Hash.new)
19
- @cache = cache
30
+ def initialize(facet = ::Module.new)
31
+ @facet = facet
20
32
  end
21
33
 
22
34
  def include(target)
23
- target.each do |key, value|
24
- @cache[key] = value
35
+ if target.class == ::Module
36
+ @facet.include(target)
37
+ elsif target.respond_to?(:included)
38
+ target.included(@facet)
39
+ else
40
+ ::Kernel.raise ::ArgumentError, "Cannot include #{target.inspect} into #{@facet.inspect}!"
25
41
  end
26
42
  end
27
43
 
28
44
  def method_missing(name, argument = nil, &block)
29
- previous = @cache[name]
30
-
31
45
  if block
32
- if block.arity == 0
33
- @cache[name] = block
34
- else
35
- # Bind the |previous| argument to the block:
36
- @cache[name] = ::Kernel.lambda{self.instance_exec(previous, &block)}
37
- end
38
- elsif previous.is_a?(::Array)
39
- @cache[name] = previous + argument
40
- elsif previous.is_a?(::Hash)
41
- @cache[name] = previous.merge(argument)
46
+ @facet.define_method(name, &block)
42
47
  else
43
- @cache[name] = argument
48
+ @facet.define_method(name){argument}
44
49
  end
45
50
  end
51
+ end
52
+
53
+ def self.build(**values, &block)
54
+ if values.any?
55
+ environment = Environment.new(Builder.wrap(values))
56
+ end
46
57
 
47
- def to_h
48
- @cache
58
+ if block_given?
59
+ environment = Environment.new(Builder.for(&block), environment)
49
60
  end
61
+
62
+ return environment
50
63
  end
51
64
 
52
- def initialize(**initial, &block)
53
- @block = block
54
- @initial = initial
65
+ def initialize(facet = ::Module.new, parent = nil)
66
+ @facet = facet
67
+ @parent = parent
55
68
  end
56
69
 
57
- def builder
58
- Builder.for(@initial, @block)
70
+ def included(target)
71
+ @parent&.included(target)
72
+ target.include(@facet)
59
73
  end
60
74
 
61
- # An evaluator is lazy read-only view of an environment. It allows for the evaluation of procs and other dynamic values.
62
- # Those values are cached, and thus the evaluator is not thread safe.
63
- class Evaluator < BasicObject
64
- def initialize(source)
65
- @source = source
66
- @cache = {}
67
- end
68
-
69
- def inspect
70
- "#<#{Evaluator} #{@source}>"
71
- end
72
-
73
- private def __evaluate__(value)
74
- case value
75
- when ::Array
76
- value.collect{|item| __evaluate__(item)}
77
- when ::Hash
78
- value.transform_values{|item| __evaluate__(item)}
79
- # when ::Symbol
80
- # self[value]
81
- when ::Proc
82
- __evaluate__(instance_exec(&value))
83
- else
84
- value
75
+ # An evaluator is lazy read-only view of an environment. It memoizes all method calls.
76
+ class Evaluator
77
+ def self.wrap(environment)
78
+ evaluator = ::Class.new(self)
79
+
80
+ facet = ::Module.new
81
+ environment.included(facet)
82
+
83
+ evaluator.include(facet)
84
+
85
+ # Memoize all instance methods:
86
+ facet.instance_methods.each do |name|
87
+ evaluator.define_method(name) do
88
+ @cache[name] ||= super()
89
+ end
85
90
  end
86
- end
87
-
88
- def [](key)
89
- @cache.fetch(key) do
90
- @cache[key] = __evaluate__(@source[key])
91
+
92
+ evaluator.define_method(:keys) do
93
+ facet.instance_methods
91
94
  end
95
+
96
+ return evaluator.new
92
97
  end
93
98
 
94
- def respond_to?(name, include_all = false)
95
- @source.key?(name) || super
99
+ def initialize
100
+ @cache = {}
96
101
  end
97
102
 
98
- def method_missing(name, ...)
99
- if @source.key?(name)
100
- self[name]
101
- else
102
- super
103
- end
103
+ def inspect
104
+ "#<#{Evaluator} #{self.keys}>"
104
105
  end
105
106
 
106
107
  def to_h
107
108
  # Ensure all keys are evaluated:
108
- @source.each_key{|key| self[key]}
109
+ self.keys.each do |name|
110
+ self.__send__(name)
111
+ end
109
112
 
110
113
  return @cache
111
114
  end
112
115
 
116
+ def [](key)
117
+ self.__send__(key)
118
+ end
119
+
113
120
  def key?(key)
114
- @source.key?(key)
121
+ self.keys.include?(key)
115
122
  end
116
123
  end
117
124
 
118
125
  def evaluator
119
- Evaluator.new(builder.to_h)
120
- end
121
-
122
- def each(&block)
123
- builder.to_h.each(&block)
126
+ return Evaluator.wrap(self)
124
127
  end
125
128
 
126
129
  def to_h
@@ -44,7 +44,7 @@ module Async
44
44
 
45
45
  # Create an environment.
46
46
  def environment(**initial, &block)
47
- Environment.new(**initial, &block)
47
+ Environment.build(**initial, &block)
48
48
  end
49
49
 
50
50
  # Define a host with the specified name.
@@ -5,6 +5,6 @@
5
5
 
6
6
  module Async
7
7
  module Service
8
- VERSION = "0.2.1"
8
+ VERSION = "0.3.1"
9
9
  end
10
10
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async-service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -37,7 +37,7 @@ cert_chain:
37
37
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
38
38
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
39
39
  -----END CERTIFICATE-----
40
- date: 2024-02-20 00:00:00.000000000 Z
40
+ date: 2024-03-10 00:00:00.000000000 Z
41
41
  dependencies:
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: async
metadata.gz.sig CHANGED
Binary file