habu 0.1.1

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
+ SHA256:
3
+ metadata.gz: ab6fc4542b67b4368a2082e9d9b21928efe7855d0c47d7898c28f35ec4079fa1
4
+ data.tar.gz: 3b5cb02a41e93f87f653e86ad027e6f97937b8644a3f1f080f75b51a490c8d2a
5
+ SHA512:
6
+ metadata.gz: cb7de9a127c3d3f680248c9788445e3a35510c9cdfbe3638a24108fc23f361e25638f85752cb0e8868acf265c1822a4e996776a75a0bcdd32fea7739759f0a23
7
+ data.tar.gz: 3f0f8c4e979f0f61a8c3d908805169e20906471fc1b78605f8b5091d4bd7814155fb41c7b62c4e67c0514ccf7b535fec6e9e565045d73d0e570ae215e7455edc
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.0.pre.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in habu.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ habu (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (12.3.2)
11
+ rspec (3.8.0)
12
+ rspec-core (~> 3.8.0)
13
+ rspec-expectations (~> 3.8.0)
14
+ rspec-mocks (~> 3.8.0)
15
+ rspec-core (3.8.0)
16
+ rspec-support (~> 3.8.0)
17
+ rspec-expectations (3.8.3)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-mocks (3.8.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-support (3.8.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 2.1.a)
30
+ habu!
31
+ rake (~> 12.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 2.1.0.pre.1
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # Habu
2
+
3
+ DI container
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'habu'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install habu
20
+
21
+ If you are using Rails, add this line to `config/boot.rb` immediately after `require 'bundler/setup'`.
22
+
23
+ require 'habu/setup'
24
+
25
+ If you are not using Rails, add this line before using DI container.
26
+
27
+ require 'habu/setup'
28
+
29
+ ## Usage
30
+
31
+ ```ruby
32
+ # user.rb
33
+ User = Struct.new(:name)
34
+
35
+ # new_user_service.rb
36
+ class NewUserService
37
+ @Inject
38
+ def initialize(user_repository)
39
+ @user_repository = user_repository
40
+ end
41
+
42
+ def call(*params)
43
+ @user_repository.new(*params)
44
+ end
45
+ end
46
+
47
+ # app.rb
48
+ require 'habu/setup'
49
+ require_relative 'user'
50
+ require_relative 'new_user_service'
51
+
52
+ # Create a new container
53
+ container = Habu::Container.new
54
+
55
+ # Register user_repository service by passing the block as service factory
56
+ container[:user_repository] { User }
57
+
58
+ # Call Habu::Container#new to get instance
59
+ new_user = container.new(NewUserService).call("hanachin")
60
+ # => #<struct User name="hanachin">
61
+
62
+ # Factory block take a container as argument
63
+ container[:new_user] do |c|
64
+ # You can get the service object by calling Container#[](service_name)
65
+ NewUserService.new(c[:user_repository])
66
+ end
67
+ new_user = container[:new_user].call("hanachin")
68
+ # => #<struct User name="hanachin">
69
+
70
+ # Using container as refinements for shorthand for container.new
71
+ using container.to_refinements
72
+ new_user = NewUserService.new.call("hanachin")
73
+ # => #<struct User name="hanachin">
74
+ ```
75
+
76
+ ## Development
77
+
78
+ 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.
79
+
80
+ 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).
81
+
82
+ ## Contributing
83
+
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hanachin/habu.
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 "habu"
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/example/app.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'habu/setup'
2
+ require_relative 'user'
3
+ require_relative 'new_user_service'
4
+
5
+ # Create a new container
6
+ container = Habu::Container.new
7
+
8
+ # Register user_repository service by passing the block as service factory
9
+ container[:user_repository] { User }
10
+
11
+ # Call Habu::Container#new to get instance
12
+ new_user = container.new(NewUserService).call("hanachin")
13
+ # => #<struct User name="hanachin">
14
+
15
+ # Factory block take a container as argument
16
+ container[:new_user] do |c|
17
+ # You can get the service object by calling Container#[](service_name)
18
+ NewUserService.new(c[:user_repository])
19
+ end
20
+ new_user = container[:new_user].call("hanachin")
21
+ # => #<struct User name="hanachin">
22
+
23
+ # Using container as refinements for shorthand for container.new
24
+ using container.to_refinements
25
+ new_user = NewUserService.new.call("hanachin")
26
+ # => #<struct User name="hanachin">
@@ -0,0 +1,10 @@
1
+ class NewUserService
2
+ @Inject
3
+ def initialize(user_repository)
4
+ @user_repository = user_repository
5
+ end
6
+
7
+ def call(*params)
8
+ @user_repository.new(*params)
9
+ end
10
+ end
data/example/user.rb ADDED
@@ -0,0 +1 @@
1
+ User = Struct.new(:name)
data/habu.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "habu/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "habu"
7
+ spec.version = Habu::VERSION
8
+ spec.authors = ["Seiei Miyagi"]
9
+ spec.email = ["hanachin@gmail.com"]
10
+
11
+ spec.summary = %q{DI container}
12
+ spec.homepage = "https://github.com/hanachin/habu"
13
+
14
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/hanachin/habu.git"
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('..', __FILE__)) 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
+
28
+ spec.required_ruby_version = ">= 2.7.0"
29
+
30
+ spec.add_development_dependency "bundler", "~> 2.1.a"
31
+ spec.add_development_dependency "rake", "~> 12.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ end
@@ -0,0 +1,35 @@
1
+ require 'habu/annotation_collector_helper'
2
+
3
+ module Habu
4
+ class AnnotationCollector
5
+ using ::Habu::AnnotationCollectorHelper
6
+
7
+ attr_reader :constructor_annotations
8
+
9
+ def initialize
10
+ @constructor_annotations = []
11
+ @iseq_interceptor = new_iseq_interceptor
12
+ end
13
+
14
+ def install
15
+ RubyVM::InstructionSequence.singleton_class.prepend(@iseq_interceptor)
16
+ end
17
+
18
+ private
19
+
20
+ def add_constructor_annotation(klass)
21
+ @constructor_annotations << klass
22
+ end
23
+
24
+ def new_iseq_interceptor
25
+ add_constructor_annotation = self.:add_constructor_annotation
26
+ Module.new do
27
+ define_method :load_iseq do |fname|
28
+ ast = RubyVM::AbstractSyntaxTree.parse_file(fname)
29
+ ast.each_constructor_annotations(&add_constructor_annotation)
30
+ RubyVM::InstructionSequence.compile_file(fname)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,42 @@
1
+ module Habu
2
+ module AnnotationCollectorHelper
3
+ refine(Object) do
4
+ def traversable?
5
+ false
6
+ end
7
+ end
8
+
9
+ refine(RubyVM::AbstractSyntaxTree::Node) do
10
+ def traversable?
11
+ true
12
+ end
13
+
14
+ def traverse(&block)
15
+ block.call(self)
16
+ children.each { |child| child.traverse(&block) if child.traversable? }
17
+ end
18
+
19
+ def deconstruct
20
+ [type, *children]
21
+ end
22
+
23
+ def each_constructor_annotations(&block)
24
+ klass_name = nil
25
+ inject_annotation_found = false
26
+ traverse do |ast|
27
+ case ast
28
+ in :CLASS, [:COLON2, nil, klass_name], *_
29
+ # TODO: Support namespace
30
+ in :IVAR, :@Inject
31
+ inject_annotation_found = true
32
+ in :DEFN, :initialize, *_ if inject_annotation_found == true
33
+ block.call(klass_name)
34
+ inject_annotation_found = false
35
+ else
36
+ inject_annotation_found = false
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,35 @@
1
+ module Habu
2
+ class Container
3
+ def initialize
4
+ @factories = {}
5
+ end
6
+
7
+ def [](key, &block)
8
+ if block
9
+ @factories[key] = block
10
+ else
11
+ @factories.fetch(key).call(self)
12
+ end
13
+ end
14
+
15
+ def new(klass, &block)
16
+ params = klass.instance_method(:initialize).parameters
17
+ klass.new(*params.filter_map { @1 == :req && self[@2] }, &block)
18
+ end
19
+
20
+ def to_refinements
21
+ refinements = Module.new
22
+ refinements.instance_exec(self) do |container|
23
+ Habu.annotation_collector.constructor_annotations.each do |klass_name|
24
+ klass = const_get(klass_name)
25
+ refine(klass.singleton_class) do
26
+ define_method(:new) do |&block|
27
+ container.new(klass, &block)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ refinements
33
+ end
34
+ end
35
+ end
data/lib/habu/setup.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'habu'
2
+
3
+ module Habu
4
+ module Setup
5
+ class << self
6
+ def install(annotation_collector)
7
+ ::Habu.annotation_collector = annotation_collector
8
+ ::Habu.annotation_collector.install
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ Habu::Setup.install(::Habu::AnnotationCollector.new)
@@ -0,0 +1,3 @@
1
+ module Habu
2
+ VERSION = "0.1.1"
3
+ end
data/lib/habu.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "habu/version"
2
+ require "habu/container"
3
+ require "habu/annotation_collector"
4
+
5
+ module Habu
6
+ class Error < StandardError; end
7
+
8
+ class << self
9
+ attr_accessor :annotation_collector
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: habu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Seiei Miyagi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-05-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: 2.1.a
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.a
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.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
+ description:
56
+ email:
57
+ - hanachin@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - example/app.rb
72
+ - example/new_user_service.rb
73
+ - example/user.rb
74
+ - habu.gemspec
75
+ - lib/habu.rb
76
+ - lib/habu/annotation_collector.rb
77
+ - lib/habu/annotation_collector_helper.rb
78
+ - lib/habu/container.rb
79
+ - lib/habu/setup.rb
80
+ - lib/habu/version.rb
81
+ homepage: https://github.com/hanachin/habu
82
+ licenses: []
83
+ metadata:
84
+ allowed_push_host: https://rubygems.org/
85
+ homepage_uri: https://github.com/hanachin/habu
86
+ source_code_uri: https://github.com/hanachin/habu.git
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: 2.7.0
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.0.3
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: DI container
106
+ test_files: []