smart_container 0.4.0 → 0.5.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: bb1bc1e5f80263730638b0b0387105364777f87e957b5f673c27c219f206273d
4
- data.tar.gz: 0d1a505edb3b4456ab44ecfe52071b1cb5555f51680d3a48de68fb12bd11dcad
3
+ metadata.gz: 92746ed446b181dc165a597563edc69b62b41550974023342c4693f2c17dee6a
4
+ data.tar.gz: 1cf3862c40c1044510a11e09f19df542a8d0b3a255bd0df5593ad9c531b9f10b
5
5
  SHA512:
6
- metadata.gz: 3959674748c1dcc2564bd6edc9ee0acdc3770b0bd260d50c7a1bba5906fee6725ff577e90a2b36b0a81910c423020431810b2a052f42c65826bfc8dcabe7dc22
7
- data.tar.gz: f01f97eda299d91b22f730de1eba90e546f9f4de0362218bc0b8f86d8800acd6abf3d2214e223c0246f5c1d4b4742b71c7223ada409a3665765c088c88ab207e
6
+ metadata.gz: ec6db288f98f769d0bbe96dd4d35186d1a199f6e046edf1de83a50c4b76149366c94a7c172a005378be88c8e8503436b9dad7e2518bcbae1de939b2a37204600
7
+ data.tar.gz: 9124de765215847cb7bd135a19524043652b9f9d7580027106a43cc6fa8f61a3bfd22c982cf07b1f46ea196b6fdae978d0973682a9c24511eb679d7c310b33a7
data/CHANGELOG.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [0.5.0] - 2020-01-07
5
+ ### Added
6
+ - Key predicates (`#key?(key)`, `#dependency?(path, memoized: nil/true/false)`, `#namespace?(path)`);
7
+
4
8
  ## [0.4.0] - 2020-01-06
5
9
  ### Added
6
10
  - `#keys(all_variants: false)` - return a list of dependency keys
data/README.md CHANGED
@@ -70,6 +70,19 @@ container.fetch('database') # => SmartCore::Container (nested container)
70
70
  container.fetch('database.resolver') # => #<SomeDatabaseResolver:0x00007f0f0f1d6332>
71
71
  ```
72
72
 
73
+ - runtime-level dependency/namespace registration:
74
+
75
+ ```ruby
76
+ container.namespace(:api) do
77
+ register(:provider) { GoogleProvider }
78
+ end
79
+
80
+ container.register('game_api') { 'overwatch' }
81
+
82
+ container['api.provider'] # => GoogleProvider
83
+ container['game_api'] # => 'overwatch'
84
+ ```
85
+
73
86
  - container keys (dependency names):
74
87
 
75
88
  ```ruby
@@ -99,6 +112,42 @@ container.keys(all_variants: true)
99
112
  ]
100
113
  ```
101
114
 
115
+ - key predicates:
116
+ - `key?(key)` - has dependency or namespace?
117
+ - `namespace?(path)` - has namespace?
118
+ - `dependency?(path)` - has dependency?
119
+ - `dependency?(path, memoized: true)` - has memoized dependency?
120
+ - `dependency?(path, memoized: false)` - has non-memoized dependency?
121
+
122
+ ```ruby
123
+ container.key?('database') # => true
124
+ container.key?('database.cache.memcached') # => true
125
+
126
+ container.dependency?('database') # => false
127
+ container.dependency?('database.resolver') # => true
128
+
129
+ container.namespace?('database') # => true
130
+ container.namespace?('database.resolver') # => false
131
+
132
+ container.dependency?('database.resolver', memoized: true) # => true
133
+ container.dependency?('database.resolver', memoized: false) # => false
134
+
135
+ container.dependency?('random', memoized: true) # => false
136
+ container.dependency?('random', memoized: false) # => true
137
+ ```
138
+
139
+ - state freeze:
140
+
141
+ ```ruby
142
+ # documentation is coming;
143
+ ```
144
+
145
+ - reloading:
146
+
147
+ ```ruby
148
+ # documentation is coming;
149
+ ```
150
+
102
151
  ---
103
152
 
104
153
  ## Contributing
@@ -26,6 +26,54 @@ module SmartCore::Container::DependencyResolver
26
26
  container.registry.resolve(dependency_path).reveal
27
27
  end
28
28
 
29
+ # @param container [SmartCore::Container]
30
+ # @param key [String, Symbol]
31
+ # @return [Boolean]
32
+ #
33
+ # @api private
34
+ # @since 0.5.0
35
+ def key?(container, key)
36
+ extract(container, key)
37
+ true
38
+ rescue SmartCore::Container::ResolvingError
39
+ false
40
+ end
41
+
42
+ # @param container [SmartCore::Container]
43
+ # @param namespace_path [String, Symbol]
44
+ # @return [Boolean]
45
+ #
46
+ # @api private
47
+ # @since 0.5.0
48
+ def namespace?(container, namespace_path)
49
+ extract(container, namespace_path).is_a?(SmartCore::Container::Entities::Namespace)
50
+ rescue SmartCore::Container::ResolvingError
51
+ false
52
+ end
53
+
54
+ # @param container [SmartCore::Container]
55
+ # @param dependency_path [String, Symbol]
56
+ # @option memoized [NilClass, Boolean]
57
+ # @return [Boolean]
58
+ #
59
+ # @api private
60
+ # @since 0.5.0
61
+ def dependency?(container, dependency_path, memoized: nil)
62
+ entity = extract(container, dependency_path)
63
+
64
+ case
65
+ when memoized.nil?
66
+ entity.is_a?(SmartCore::Container::Entities::Dependency)
67
+ when !!memoized == true
68
+ entity.is_a?(SmartCore::Container::Entities::MemoizedDependency)
69
+ when !!memoized == false
70
+ entity.is_a?(SmartCore::Container::Entities::Dependency) &&
71
+ !entity.is_a?(SmartCore::Container::Entities::MemoizedDependency)
72
+ end
73
+ rescue SmartCore::Container::ResolvingError
74
+ false
75
+ end
76
+
29
77
  # @param container [SmartCore::Container]
30
78
  # @param dependency_path [String, Symbol]
31
79
  # @return [SmartCore::Container, Any]
@@ -38,12 +86,11 @@ module SmartCore::Container::DependencyResolver
38
86
  #
39
87
  # @api private
40
88
  # @since 0.1.0
41
- # @version 0.1.0
42
89
  def resolve(container, dependency_path)
43
90
  entity = container
44
91
  Route.build(dependency_path).each do |cursor|
45
92
  entity = entity.registry.resolve(cursor.current_path)
46
- prevent_cursor_overflow!(cursor, entity)
93
+ prevent_ambiguous_resolving!(cursor, entity)
47
94
  entity = entity.reveal
48
95
  end
49
96
  entity
@@ -53,6 +100,25 @@ module SmartCore::Container::DependencyResolver
53
100
 
54
101
  private
55
102
 
103
+ # @param container [SmartCore::Container]
104
+ # @param entity_path [String, Symbol]
105
+ # @return [SmartCore::Container::Entities::Base]
106
+ #
107
+ # @api private
108
+ # @since 0.5.0
109
+ def extract(container, entity_path)
110
+ resolved_entity = container
111
+ extracted_entity = container
112
+
113
+ Route.build(entity_path).each do |cursor|
114
+ resolved_entity = resolved_entity.registry.resolve(cursor.current_path)
115
+ extracted_entity = resolved_entity
116
+ resolved_entity = resolved_entity.reveal
117
+ end
118
+
119
+ extracted_entity
120
+ end
121
+
56
122
  # @param cursor [SmartCore::Container::DependencyResolver::Route::Cursor]
57
123
  # @param entity [SmartCore::Container::Entities::Base]
58
124
  # @return [void]
@@ -60,8 +126,8 @@ module SmartCore::Container::DependencyResolver
60
126
  # @raise [SmartCore::Container::ResolvingError]
61
127
  #
62
128
  # @api private
63
- # @since 0.1.0
64
- def prevent_cursor_overflow!(cursor, entity)
129
+ # @since 0.5.0
130
+ def prevent_ambiguous_resolving!(cursor, entity)
65
131
  if cursor.last? && entity.is_a?(SmartCore::Container::Entities::Namespace)
66
132
  raise(
67
133
  SmartCore::Container::ResolvingError.new(
@@ -4,7 +4,7 @@ module SmartCore
4
4
  class Container
5
5
  # @api public
6
6
  # @since 0.1.0
7
- # @version 0.4.0
8
- VERSION = '0.4.0'
7
+ # @version 0.5.0
8
+ VERSION = '0.5.0'
9
9
  end
10
10
  end
@@ -114,6 +114,34 @@ module SmartCore
114
114
  thread_safe { registry.keys(all_variants: all_variants) }
115
115
  end
116
116
 
117
+ # @param key [String, Symbol]
118
+ # @return [Boolean]
119
+ #
120
+ # @api public
121
+ # @since 0.5.0
122
+ def key?(key)
123
+ thread_safe { DependencyResolver.key?(self, key) }
124
+ end
125
+
126
+ # @param namespace_path [String, Symbol]
127
+ # @return [Boolean]
128
+ #
129
+ # @api public
130
+ # @since 0.5.0
131
+ def namespace?(namespace_path)
132
+ thread_safe { DependencyResolver.namespace?(self, namespace_path) }
133
+ end
134
+
135
+ # @param dependency_path [String, Symbol]
136
+ # @option memoized [NilClass, Boolean]
137
+ # @return [Boolean]
138
+ #
139
+ # @api public
140
+ # @since 0.5.0
141
+ def dependency?(dependency_path, memoized: nil)
142
+ thread_safe { DependencyResolver.dependency?(self, dependency_path, memoized: memoized) }
143
+ end
144
+
117
145
  # @option yield_all [Boolean]
118
146
  # @param block [Block]
119
147
  # @yield [dependency_name, dependency_value]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_container
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
  - Rustam Ibragimov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-06 00:00:00.000000000 Z
11
+ date: 2020-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: smart_engine