ruby_beans 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f6dc9f2ea59685d46f6982aa7a68bff52f639b43
4
+ data.tar.gz: c74618ab31ad2afe709cf4cb9ddaf200f3d75d7c
5
+ SHA512:
6
+ metadata.gz: aebe7d7e86dddfcc2aa0466755ff522b58dc856be82a0873e252d2a685d948d287eb389b4a9c76475a1121352f43b0e754db7c80bbfba41f99a96e598b51fd94
7
+ data.tar.gz: 9649481b176e46fce2a5a59d9d2fed9168b51bdb8039e7dc0efab803b2e1a2d5cb18fde827f171a1687d59415f3a1699e0bb88e7d5365e2ac75b9b2cc32b7463
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 documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby_beans.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 sebi
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,152 @@
1
+ # RubyBeans
2
+
3
+ *simple to use and test dependency injection framework*
4
+
5
+ **RubyBeans** is a better alternative to
6
+ ```ruby
7
+ class MyClass
8
+ include Singleton
9
+ end
10
+ #singleton classes
11
+ MyClass.instance
12
+ ```
13
+
14
+ ```ruby
15
+ class MyCache
16
+ class << self
17
+ def my_cached_item
18
+ ...
19
+ end
20
+ end
21
+ end
22
+ # global accessable objects(cacheable)
23
+ MyCache.my_cached_item
24
+ ```
25
+
26
+
27
+ ```ruby
28
+ class MyService
29
+ include Singleton
30
+ def my_method; end
31
+ end
32
+
33
+ class ProductsController < ApplicationController
34
+ def index
35
+ # polute code with hard to test code
36
+ # increases coupling
37
+ MyService.instance.my_method
38
+ end
39
+ end
40
+ ```
41
+
42
+ ### Define beans
43
+
44
+ ```ruby
45
+ class MyContainer < RubyBeans::Container
46
+ # load dependent container
47
+ load_containers 'MyContainer2'
48
+
49
+ def get_first_bean
50
+ 1 + second_bean # => 3
51
+ end
52
+
53
+ def get_second_bean
54
+ 2
55
+ end
56
+
57
+ def get_third_bean
58
+ 3 + forth_bean #=> 7
59
+ end
60
+
61
+ def get_fifth_bean
62
+ sixth_bean #=> nil
63
+ end
64
+ end
65
+
66
+ class MyContainer2 < RubyBeans::Container
67
+ def get_forth_bean
68
+ 4
69
+ end
70
+ end
71
+ ```
72
+
73
+ ### Usage
74
+
75
+ ```ruby
76
+ # get a bean manualy by name
77
+ RubyBeans.get_bean('first_bean') # => 3
78
+
79
+ # inject them into your class instances
80
+
81
+ class MyClass
82
+ extend RubyBeans::Injector
83
+
84
+ inject :first_bean, :second_MyContainerMyContainerbean
85
+ end
86
+
87
+ my_ins = MyClass.new
88
+ my_ins.first_bean #=> 3
89
+ my_ins.second_bean #=> 2
90
+ ```
91
+
92
+ ### Test
93
+
94
+ Load helpers
95
+ ```ruby
96
+ require 'ruby_beans/stub'
97
+ RSpec.configure do |c|
98
+ c.include RubyBeans::Stub
99
+ end
100
+ ```
101
+
102
+ ```ruby
103
+ # test containers
104
+ subject(:container){ MyContainer.instance)
105
+ it 'has the bean' do
106
+ expect(container.first_bean).to eq 3 # to use cache
107
+ expect(container.get_first_bean).to eq 3 # without cache
108
+ end
109
+
110
+ # stub containers
111
+ before do
112
+ @my_container = stub_container do
113
+ def get_my_bean
114
+ 33
115
+ end
116
+ end
117
+ end
118
+
119
+ it 'my test' do
120
+ expect(@my_container.class).to be_a Class # @container is class
121
+ expect(@my_containe.instance).to be_a RubyBeans::Container #access the instance
122
+ end
123
+ # stub_container_main is the same as stub_container, it just sets the # RubyBeans.main_container too.
124
+
125
+ # stub beans
126
+ before do
127
+ stub_bean('test', 1)
128
+ end
129
+
130
+ it 'stubs the bean' do
131
+ expect(RubyBeans.get_bean('test')).to eq 1
132
+ end
133
+ ```
134
+
135
+ ### Installation
136
+
137
+ Add this line to your application's Gemfile:
138
+
139
+ ```ruby
140
+ gem 'ruby_beans'
141
+ ```
142
+ ```ruby
143
+ # load it as soon as possible, for rails create a initializer file
144
+ # main container is the root of all dependent containers
145
+ RubyBeans.main_container = MyContainer
146
+ ```
147
+
148
+
149
+ ## License
150
+
151
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
152
+
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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ruby_beans"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ module RubyBeans
2
+ class Cache
3
+ @@_hash = Hash.new
4
+
5
+ class << self
6
+ def get(name)
7
+ @@_hash[name]
8
+ end
9
+
10
+ def put(name, obj)
11
+ @@_hash[name] = obj
12
+ end
13
+
14
+ def get_or_put(name, obj)
15
+ unless get(name)
16
+ put(name, obj)
17
+ end
18
+ get(name)
19
+ end
20
+
21
+ def hash
22
+ @@_hash
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,51 @@
1
+ require 'singleton'
2
+ module RubyBeans
3
+ class Container
4
+ include Singleton
5
+
6
+ attr_accessor :dependency_loading_called, :dependent_containers, :dependent_containers_names
7
+
8
+ def initialize
9
+ @dependency_loading_called = false
10
+ @dependent_containers_names = []
11
+ @dependent_containers = []
12
+ end
13
+ class << self
14
+ def load_containers(*containers)
15
+ self.instance.dependency_loading_called = true
16
+ self.instance.dependent_containers_names = containers
17
+ end
18
+
19
+ def _load_container_instances
20
+ self.instance.dependent_containers_names.each do |container|
21
+ self.instance.dependent_containers.push(Object.const_get(container).send(:instance))
22
+ end
23
+ end
24
+ end
25
+
26
+ def method_missing(name, *args, &block)
27
+ name = name.to_s
28
+
29
+ if dependency_loading_called && dependent_containers.empty?
30
+ self.class._load_container_instances
31
+ end
32
+
33
+ if name.start_with?('get_') && name.end_with?('_bean')
34
+ result = nil
35
+ dependent_containers.each do |container|
36
+ result = container.send(name)
37
+ end
38
+ result
39
+ elsif name.end_with?('_bean')
40
+ bean = RubyBeans::Cache.get name
41
+ unless bean
42
+ bean = self.send("get_#{name}")
43
+ RubyBeans::Cache.put(name, bean)
44
+ end
45
+ bean
46
+ else
47
+ super
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,12 @@
1
+ module RubyBeans
2
+ module Injector
3
+ def inject(*dependencies)
4
+ dependencies.each do |d|
5
+ ds = d.to_s
6
+ define_method(ds) do
7
+ RubyBeans.get_bean ds
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ module RubyBeans
2
+ module Stub
3
+ def stub_bean(name, obj)
4
+ RubyBeans::Cache.put(name, obj)
5
+ end
6
+
7
+ def stub_container(&block)
8
+ Class.new RubyBeans::Container, &block
9
+ end
10
+
11
+ def stub_main_container(&block)
12
+ klass = stub_container(&block)
13
+ RubyBeans.main_container = klass
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module RubyBeans
2
+ VERSION = "0.1.0"
3
+ end
data/lib/ruby_beans.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'ruby_beans/version'
2
+ require 'ruby_beans/container'
3
+ require 'ruby_beans/cache'
4
+ require 'ruby_beans/injector'
5
+
6
+ module RubyBeans
7
+ class << self
8
+ attr_accessor :main_container
9
+
10
+ def get_bean(name)
11
+ bean = RubyBeans::Cache.get(name)
12
+ unless bean
13
+ bean = main_container.send(name)
14
+ RubyBeans::Cache.put(name, bean)
15
+ end
16
+ bean
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruby_beans/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby_beans"
8
+ spec.version = RubyBeans::VERSION
9
+ spec.authors = ["sebi"]
10
+ spec.email = ["gore.sebyx@yahoo.com"]
11
+
12
+ spec.summary = %q{simple to use and test dependency injection framework}
13
+ spec.description = %q{Dependency injection framework for ruby}
14
+ spec.homepage = "https://github.com/sebyx07/rubybeans"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec"
33
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_beans
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sebi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-24 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Dependency injection framework for ruby
56
+ email:
57
+ - gore.sebyx@yahoo.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/ruby_beans.rb
73
+ - lib/ruby_beans/cache.rb
74
+ - lib/ruby_beans/container.rb
75
+ - lib/ruby_beans/injector.rb
76
+ - lib/ruby_beans/stub.rb
77
+ - lib/ruby_beans/version.rb
78
+ - ruby_beans.gemspec
79
+ homepage: https://github.com/sebyx07/rubybeans
80
+ licenses:
81
+ - MIT
82
+ metadata:
83
+ allowed_push_host: https://rubygems.org
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.4.8
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: simple to use and test dependency injection framework
104
+ test_files: []