leather-satchel 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 15ee87d76e6b5c7aa9316453df972a619fddf82971d21208137edf3a3d0b7ebf
4
+ data.tar.gz: 9dadbe1159d2e3a9ba5ad40c40c45b80ac65b532751b4a4028c24f6032067fe5
5
+ SHA512:
6
+ metadata.gz: 668268e41263032d6736113f0756c4348bd7e38d2e4464a6af1152c5b3a0951d4686a6007d6c350c442a5f4ba2a20e4af2e3b89606c9216897e90737d313b25a
7
+ data.tar.gz: 55eb24fcc57552e6e00ed1ae3551c016153dafa761e4c107c5e0cd903d304314d3ed3552afb035b8e1fe969a8c81590cd0ea8875e94ef1418c1297654382ec78
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org/>
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'leather/satchel/version'
4
+ require 'leather/satchel/error/unknown_identifier_error'
5
+ require 'leather/satchel/error/invalid_factory_error'
6
+
7
+ module Leather
8
+ module Satchel
9
+ # IOC Container Class
10
+ class Container
11
+ # Container constructor
12
+ # @param values [Hash]
13
+ def initialize(values = {})
14
+ @keys = {}
15
+ @values = {}
16
+ @cached_services = {}
17
+ @factories = []
18
+
19
+ values.each do |key, value|
20
+ self[key] = value
21
+ end
22
+ end
23
+
24
+ # Inserts a service into the container under the name @key
25
+ # @param key [String|Symbol] The name of the service
26
+ # @param value [Any] The value of the container
27
+ def []=(key, value)
28
+ @keys[key] = true
29
+ @values[key] = value
30
+ end
31
+
32
+ # Gets a service from the container
33
+ # @param key [String|Symbol] The name of the service to look for
34
+ # @return [Any] The stored service value
35
+ # @throw Error::UnknownIdentifierError
36
+ def [](key)
37
+ raise Error::UnknownIdentifierError, key unless @keys.key?(key)
38
+
39
+ service = @values[key]
40
+ return service.call(self) if @factories.include?(service)
41
+
42
+ if service.is_a?(Proc)
43
+ return @cached_services[key] if @cached_services.key?(key)
44
+
45
+ value = service.call(self)
46
+ @cached_services[key] = value
47
+ return value
48
+ end
49
+
50
+ service
51
+ end
52
+
53
+ # Defines a factory service
54
+ # @param service [Proc]
55
+ # @return [Proc]
56
+ def factory(service)
57
+ raise Error::InvalidFactoryError, service.class unless service.is_a?(Proc)
58
+
59
+ @factories << service
60
+ service
61
+ end
62
+
63
+ # Deletes a stored service from the container if it exists
64
+ # @param key [String|Symbol]
65
+ def remove(key)
66
+ return unless @keys.key?(key)
67
+
68
+ service = @values.delete(key)
69
+ @factories.delete(service)
70
+ @keys.delete(key)
71
+ @cached_services.delete(key)
72
+ end
73
+
74
+ # Clears all stored services from the container
75
+ def clear
76
+ @keys.clear
77
+ @values.clear
78
+ @cached_services.clear
79
+ @factories.clear
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Leather
4
+ module Satchel
5
+ module Error
6
+ class InvalidFactoryError < StandardError
7
+ # InvalidFactoryError constructor
8
+ # @param type [String]
9
+ def initialize(type)
10
+ message = "Invalid factory service of type #{type} provided. Must be either a lambda or proc"
11
+ super(message)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Leather
4
+ module Satchel
5
+ module Error
6
+ # Error to be raised if an identifier is unknown
7
+ class UnknownIdentifierError < StandardError
8
+ # UnknownIdentifierError constructor
9
+ # @param identifier [String]
10
+ def initialize(identifier)
11
+ message = "Unknown service identifier #{identifier}"
12
+ super(message)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Leather
4
+ module Satchel
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: leather-satchel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Birmingham
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-10-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: A fairly simple IOC container based off of silexphp/Pimple
56
+ email:
57
+ - chris.birmingham@hotmail.co.uk
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - lib/leather/satchel.rb
64
+ - lib/leather/satchel/error/invalid_factory_error.rb
65
+ - lib/leather/satchel/error/unknown_identifier_error.rb
66
+ - lib/leather/satchel/version.rb
67
+ homepage: https://github.com/chrisBirmingham/leather-satchel
68
+ licenses:
69
+ - Unlicense
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '2.3'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubygems_version: 3.0.3
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: IOC Container for Ruby
90
+ test_files: []