dumpling 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 27b87ea50d627fb4bf537b97334435165f0550dc
4
+ data.tar.gz: 084c021f548b392e1e0aa749d7a5566e5ff9b4a2
5
+ SHA512:
6
+ metadata.gz: 6e9fceb60dcfb9464d028d6fb34ab1f7358f14ff21abd93b65f7dbef581353ba2bc445895120fa1ebac5bc57d9276b00fb941f0063801d7610293b10e52e98be
7
+ data.tar.gz: e7dcdc818d2a6df4eeea40032bbf2cb85ab0db228b3350d4db1361991fe943de2c0752d17c6a6271f509e9f3fafe8e4e81a16bdf4e90726a5957b799eeac8822
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format progress
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ Metrics/LineLength:
2
+ Max: 100
3
+
4
+ Documentation:
5
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at antonkuzmenko.dev@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dumpling.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Anton Kuzmenko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Dumpling
2
+
3
+ TODO: Describe your gem
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'dumpling'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dumpling
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/antonkuzmenko/dumpling. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
39
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'dumpling'
5
+
6
+ require 'irb'
7
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/dumpling.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dumpling/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'dumpling'
8
+ spec.version = Dumpling::Version::STRING
9
+ spec.author = 'Anton Kuzmenko'
10
+ spec.email = 'antonkuzmenko.dev@gmail.com'
11
+ spec.homepage = 'https://github.com/antonkuzmenko/dumpling'
12
+ spec.license = 'MIT'
13
+
14
+ spec.summary = 'Dumpling is a simple Dependency Injection Container'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\Aspec/}) }
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.required_ruby_version = '>= 2.2.2'
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.11'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+ spec.add_development_dependency 'rubocop', '~> 0.36.0'
25
+ end
@@ -0,0 +1,51 @@
1
+ module Dumpling
2
+ class Container
3
+ def initialize
4
+ @id_list = Set.new
5
+ @registry = {}
6
+ end
7
+
8
+ def get(id)
9
+ id = id.to_sym
10
+ fail(Errors::Container::Missing, id) unless @id_list.include?(id)
11
+ specification = @registry[id]
12
+ instance = build_instance(specification)
13
+ inject_dependencies(instance, specification)
14
+ instance
15
+ end
16
+
17
+ alias :[] get
18
+
19
+ def set(id)
20
+ id = id.to_sym
21
+ fail(Errors::Container::Duplicate, id) if @id_list.include?(id)
22
+
23
+ specification = Specification.new
24
+ yield specification
25
+ SpecificationValidator.new(@id_list, specification).validate!
26
+ @registry[id] = specification
27
+ @id_list << id
28
+
29
+ nil
30
+ end
31
+
32
+ alias :[]= set
33
+
34
+ def configure(&block)
35
+ instance_eval(&block)
36
+ self
37
+ end
38
+
39
+ private
40
+
41
+ def build_instance(specification)
42
+ specification.class.nil? ? specification.instance : specification.class.new
43
+ end
44
+
45
+ def inject_dependencies(instance, specification)
46
+ specification.dependencies.each do |dependency|
47
+ instance.send("#{dependency[:attr]}=", get(dependency[:id]))
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,6 @@
1
+ module Dumpling
2
+ module Errors
3
+ class BaseError < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,11 @@
1
+ module Dumpling
2
+ module Errors
3
+ module Container
4
+ class Missing < BaseError
5
+ end
6
+
7
+ class Duplicate < BaseError
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ module Dumpling
2
+ module Errors
3
+ module Specification
4
+ class Invalid < BaseError
5
+ end
6
+
7
+ class MissingDependencies < Invalid
8
+ end
9
+
10
+ class NotClass < Invalid
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ module Dumpling
2
+ class Specification < BasicObject
3
+ attr_accessor :dependencies
4
+
5
+ def initialize
6
+ self.dependencies = []
7
+ end
8
+
9
+ def class(klass = nil)
10
+ klass.nil? ? @class : (@class = klass)
11
+ end
12
+
13
+ def instance(instance = nil)
14
+ instance.nil? ? @instance : (@instance = instance)
15
+ end
16
+
17
+ def inject(id, attr: nil)
18
+ dependencies << { id: id.to_sym, attr: (attr || guess_attribute(id)).to_sym }
19
+ end
20
+
21
+ private
22
+
23
+ def guess_attribute(id)
24
+ /(?<attr>\w+)\z/i =~ id
25
+ attr || id
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,36 @@
1
+ module Dumpling
2
+ class SpecificationValidator
3
+ def initialize(id_list, specification)
4
+ @id_list = id_list
5
+ @specification = specification
6
+ end
7
+
8
+ def validate!
9
+ validate_value
10
+ validate_dependencies
11
+ end
12
+
13
+ private
14
+
15
+ def validate_value
16
+ if @specification.class.nil? && @specification.instance.nil?
17
+ fail Errors::Specification::Invalid, 'You must define #class or #instance'
18
+ end
19
+ end
20
+
21
+ def validate_dependencies
22
+ missing_deps = missing_dependencies
23
+
24
+ unless missing_deps.empty?
25
+ fail Errors::Specification::MissingDependencies, missing_deps.join(', ')
26
+ end
27
+ end
28
+
29
+ def missing_dependencies
30
+ @specification
31
+ .dependencies
32
+ .reject { |e| @id_list.include?(e[:id]) }
33
+ .map { |e| e[:id] }
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,10 @@
1
+ module Dumpling
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+ PRE = nil
7
+
8
+ STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.').freeze
9
+ end
10
+ end
data/lib/dumpling.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'set'
2
+
3
+ module Dumpling
4
+ require 'dumpling/version'
5
+ require 'dumpling/errors/base_error'
6
+ require 'dumpling/errors/container'
7
+ require 'dumpling/errors/specification'
8
+ require 'dumpling/specification'
9
+ require 'dumpling/specification_validator'
10
+ require 'dumpling/container'
11
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dumpling
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anton Kuzmenko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-28 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.36.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.36.0
69
+ description:
70
+ email: antonkuzmenko.dev@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - ".rspec"
77
+ - ".rubocop.yml"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - dumpling.gemspec
87
+ - lib/dumpling.rb
88
+ - lib/dumpling/container.rb
89
+ - lib/dumpling/errors/base_error.rb
90
+ - lib/dumpling/errors/container.rb
91
+ - lib/dumpling/errors/specification.rb
92
+ - lib/dumpling/specification.rb
93
+ - lib/dumpling/specification_validator.rb
94
+ - lib/dumpling/version.rb
95
+ homepage: https://github.com/antonkuzmenko/dumpling
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: 2.2.2
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.5.1
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Dumpling is a simple Dependency Injection Container
119
+ test_files: []