rioc 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
+ SHA256:
3
+ metadata.gz: efed84a16947db44557f740b62addf35ecdef117110fa990d259afcdbf67872c
4
+ data.tar.gz: d14427e1bbaec55297a4f0a168c174754eb8f66daf24a849048bb0c9604e58d7
5
+ SHA512:
6
+ metadata.gz: 74d8089c4ee489fad5f0b8318d8ba2c69dec13d49a4ffc15da27bd9c07377740f78e168793175ca0bd4571e18fbffd1b55a20ae3d2e871d9b6b77055e6f907b7
7
+ data.tar.gz: a962bd56ee4deae777cf76b089789abd195ccbc1167720af78981ab056d861fff182776c2c43748839fd077fc6f9214345d0b773e0802846ac34c9452371828e
@@ -0,0 +1,35 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+ strategy:
21
+ matrix:
22
+ ruby-version: ['2.6', '2.7', '3.0']
23
+
24
+ steps:
25
+ - uses: actions/checkout@v2
26
+ - name: Set up Ruby
27
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
28
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
29
+ # uses: ruby/setup-ruby@v1
30
+ uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
31
+ with:
32
+ ruby-version: ${{ matrix.ruby-version }}
33
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
34
+ - name: Run tests
35
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ .idea
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ Style/EndOfLine:
2
+ EnforcedStyle: native
3
+
4
+ Style/FrozenStringLiteralComment:
5
+ EnforcedStyle: never
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.2
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at dwcaa0633@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rioc.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rioc (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.14.4)
10
+ rake (12.3.3)
11
+
12
+ PLATFORMS
13
+ ruby
14
+ x64-mingw32
15
+ x86_64-linux
16
+
17
+ DEPENDENCIES
18
+ minitest (~> 5.0)
19
+ rake (~> 12.0)
20
+ rioc!
21
+
22
+ BUNDLED WITH
23
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 cty
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,90 @@
1
+ # Rioc
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'rioc'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rioc
18
+
19
+ ## Usage
20
+
21
+ The user experience will be closer to C# Autofac framework. Suppose you want to build a webapp using the repository pattern, here's a typical usecase,
22
+
23
+ ```ruby
24
+ # Initialize a container
25
+ container = Rioc::RiocContainer.new
26
+
27
+ # Specify the controller to the IoC container, suppose your controller looks like the following
28
+ #
29
+ # class BusinessController
30
+ # def initialize(repository)
31
+ # @repository = repository
32
+ # end
33
+ #
34
+ # def save(text)
35
+ # @repository.save(text)
36
+ # end
37
+ # end
38
+ #
39
+ # Typically for most web application frameworks, the controller is one instance per request, and it
40
+ # would translate to Transient scope in RIOC
41
+ container.register(:biz_controller, scope: Rioc::Bean::Scope::TRANSIENT) do |c|
42
+ BusinessController.new(c.resolve(:repository))
43
+ end
44
+
45
+ # Now you need to have the repository dependency setup so that it can be consumed by the controller
46
+ # and typically repository is Singleton as in Guice or Spring in Java. Suppose the repository looks
47
+ # like this
48
+ #
49
+ # class Repository
50
+ # def initialize(credential)
51
+ # @credential = credential
52
+ # end
53
+ # end
54
+ #
55
+ # And RIOC uses Singleton scope by default.
56
+ container.register(:repository) { |c| Repository.new(c.resolve(:credential)) }
57
+
58
+ # Now register the credential, let's assume the credential is an parameterless object
59
+ container.register(:credential) { |_| Credential.create("username", "password") }
60
+
61
+ # Build container
62
+ container.build_container
63
+
64
+ # Now we have all dependencies we need registered into the container, we can use them by
65
+ biz_con = container.resolve(:biz_controller)
66
+ biz_con.save("Hello RIOC")
67
+
68
+ # Of course you can also resolve repository and credential
69
+ repo = container.resolve(:repository)
70
+ cred = container.resolve(:credential)
71
+ ```
72
+
73
+ ## Development
74
+
75
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
76
+
77
+ 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).
78
+
79
+ ## Contributing
80
+
81
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rioc. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/rioc/blob/master/CODE_OF_CONDUCT.md).
82
+
83
+
84
+ ## License
85
+
86
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
87
+
88
+ ## Code of Conduct
89
+
90
+ Everyone interacting in the Rioc project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rioc/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rioc"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
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/lib/rioc.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'rioc/container'
2
+ require 'rioc/application'
3
+ require 'rioc/version'
4
+
5
+ # Root module of Rioc library
6
+ module Rioc
7
+
8
+ end
@@ -0,0 +1,15 @@
1
+ require 'rioc/errors'
2
+
3
+ module Rioc
4
+
5
+ # RiocApplication class
6
+ class RiocApplication
7
+
8
+ # This function should be implemented by user
9
+ def application_start
10
+ raise Errors::ApplicationUndeclaredError
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,27 @@
1
+ module Rioc
2
+ module Bean
3
+ # Defines the scope of the bean
4
+ class Scope
5
+ # Single instance throughout the entire lifetime of the framework.
6
+ SINGLETON = :singleton
7
+
8
+ # Create a new instance every time the bean is requested to be resolved.
9
+ TRANSIENT = :transient
10
+ end
11
+
12
+ # RiocBean class
13
+ class RiocBean
14
+
15
+ attr_reader :name, :factory, :scope, :lazy
16
+
17
+ def initialize(name, factory, scope, lazy)
18
+ @name = name
19
+ @factory = factory
20
+ @scope = scope
21
+ @lazy = lazy
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ module Rioc
2
+ module Bean
3
+ class BeanFactory
4
+
5
+ # Initialize the dependency factory class to build the dependency
6
+ # whenever the dependency is needed.
7
+ def initialize(container, name, block)
8
+ @container = container
9
+ @name = name
10
+ @block = block
11
+ end
12
+
13
+ def build_instance
14
+ @block.call(@container)
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,98 @@
1
+ require 'rioc/bean/bean'
2
+ require 'rioc/bean/factory'
3
+ require 'rioc/errors'
4
+ require 'set'
5
+
6
+ module Rioc
7
+ # Rioc container class that is responsible for storing all the beans definitions as well as live instance
8
+ # of the beans declared.
9
+ class RiocContainer
10
+
11
+ # Initialize the IoC container
12
+ def initialize
13
+ @container = {}
14
+ @beans = {}
15
+ @in_recursion = false
16
+
17
+ # Used to resolve dependencies
18
+ @visited = Set.new
19
+ @built = Set.new
20
+ end
21
+
22
+ # Register a instance without any need of resolving dependencies
23
+ def register(name, scope: Rioc::Bean::Scope::SINGLETON, lazy: false, &block)
24
+ @beans[name] = Rioc::Bean::RiocBean.new(name,
25
+ Rioc::Bean::BeanFactory.new(self, name, block),
26
+ scope,
27
+ lazy)
28
+
29
+ end
30
+
31
+ # Resolve bean with the provided bean name
32
+ # @param name - The name of the bean to resolve
33
+ # @return A live instance of the bean definition
34
+ def resolve(name)
35
+ # Should panic if the bean name is never registered
36
+ raise UnknownDependencyNameError, name unless @beans[name]
37
+
38
+ bean = @beans[name]
39
+
40
+ # If the bean already exists in the container and the scope is singleton,
41
+ # directly return the bean instance
42
+ return @container[name] if @container[name] && bean.scope == Rioc::Bean::Scope::SINGLETON
43
+
44
+ # Call the internal function to create the bean instance and return it
45
+ resolve_bean(name)
46
+ end
47
+
48
+ # Build container
49
+ def build_container
50
+ @beans
51
+ .reject { |name| @beans[name].lazy }
52
+ .each { |name, _| resolve(name) }
53
+ end
54
+
55
+ # Start application
56
+ def start_application; end
57
+
58
+ private
59
+
60
+ # Internal method to resolve bean if the bean doesn't have an instance
61
+ # in container or the bean is transient.
62
+ def resolve_bean(bean)
63
+ # We need to know if we are recursively resolving the dependencies
64
+ # in order to detect cyclic dependencies
65
+ unless @in_recursion
66
+ @in_recursion = true
67
+ clear_dfs_history
68
+
69
+ instance = build_bean(bean)
70
+
71
+ @in_recursion = false
72
+ clear_dfs_history
73
+ return instance
74
+ end
75
+
76
+ # Check if it has cyclic dependency
77
+ raise Rioc::Errors::CyclicDependencyError, bean if @visited.include?(bean) && !@built.include?(bean)
78
+
79
+ # Resolve current dependencies before building the instance
80
+ build_bean(bean)
81
+ end
82
+
83
+ # Internal method to instantiate a new bean
84
+ def build_bean(bean)
85
+ @visited.add(bean)
86
+ instance = @beans[bean].factory.build_instance
87
+ @container[bean] = instance if @beans[bean].scope == Rioc::Bean::Scope::SINGLETON
88
+ @built.add(bean)
89
+ instance
90
+ end
91
+
92
+ # Internal method to clear the history of the DFS search to resolve a bean
93
+ def clear_dfs_history
94
+ @visited.clear
95
+ @built.clear
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,26 @@
1
+ module Rioc
2
+
3
+ module Errors
4
+
5
+ # ApplicationUndeclaredError
6
+ class ApplicationUndeclaredError < RuntimeError
7
+ def initialize
8
+ super('Application is not declared by user')
9
+ end
10
+ end
11
+
12
+ class CyclicDependencyError < RuntimeError
13
+ def initialize(name)
14
+ super("Cyclic dependency detected at #{name}")
15
+ end
16
+ end
17
+
18
+ # UnknownDependencyNameError
19
+ class UnknownDependencyNameError < StandardError
20
+ def initialize(name)
21
+ super(msg: "Unknown dependency: #{name}, #{name} is not registered in RIOC container, ")
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Rioc
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/rioc.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ require_relative 'lib/rioc/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'rioc'
5
+ spec.version = Rioc::VERSION
6
+ spec.authors = ['cty']
7
+ spec.email = ['ctychen2216@gmail.com']
8
+
9
+ spec.summary = 'Ruby IoC container framework'
10
+ spec.description = 'Ruby Ioc container framework'
11
+ spec.homepage = 'https://github.com/cty123/RIOC'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = 'https://github.com/cty123/RIOC'
17
+ spec.metadata['changelog_uri'] = 'https://github.com/cty123/RIOC/blob/master/CHANGELOG.md'
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rioc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - cty
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-04-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby Ioc container framework
14
+ email:
15
+ - ctychen2216@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".github/workflows/ruby.yml"
21
+ - ".gitignore"
22
+ - ".rubocop.yml"
23
+ - ".travis.yml"
24
+ - CODE_OF_CONDUCT.md
25
+ - Gemfile
26
+ - Gemfile.lock
27
+ - LICENSE.txt
28
+ - README.md
29
+ - Rakefile
30
+ - bin/console
31
+ - bin/setup
32
+ - lib/rioc.rb
33
+ - lib/rioc/application.rb
34
+ - lib/rioc/bean/bean.rb
35
+ - lib/rioc/bean/factory.rb
36
+ - lib/rioc/container.rb
37
+ - lib/rioc/errors.rb
38
+ - lib/rioc/version.rb
39
+ - rioc.gemspec
40
+ homepage: https://github.com/cty123/RIOC
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ homepage_uri: https://github.com/cty123/RIOC
45
+ source_code_uri: https://github.com/cty123/RIOC
46
+ changelog_uri: https://github.com/cty123/RIOC/blob/master/CHANGELOG.md
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.3.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.1.4
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Ruby IoC container framework
66
+ test_files: []