dic 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - jruby-19mode
5
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in dic.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,16 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dic (1.0.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ rake (10.0.2)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ dic!
16
+ rake
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Thomas Sonntag
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
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
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Dic
2
+
3
+ [![Build History][2]][1]
4
+
5
+ [1]: http://travis-ci.org/tracksun/di
6
+ [2]: https://secure.travis-ci.org/tracksun/di.png?branch=master
7
+
8
+ Simple Dependency injection container
9
+
10
+
11
+ ### Example
12
+
13
+ require 'dic'
14
+
15
+ class MyDic < Dic
16
+ def initialize
17
+ super
18
+ # set name to 'Thomas'
19
+ name 'Thomas'
20
+
21
+ # use proc to computer values lazily
22
+ answer { long_computation() }
23
+
24
+ # properties can be defined in any order
25
+ upfoo { foo.upcase }
26
+ foo { 'bar' }
27
+
28
+ # you can use #set
29
+ set answer, 42
30
+ end
31
+ end
32
+
33
+ mc = MyDic.new
34
+ mc.foo # => 'bar'
35
+ mc[:foo] # => 'bar'
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+
6
+ task default: [:test]
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.pattern = "test/*.rb"
10
+ end
data/dic.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "dic/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "dic"
7
+ s.version = Dic::VERSION
8
+ s.authors = ["Thomas Sonntag"]
9
+ s.email = ["git@sonntagsbox.de"]
10
+ s.homepage = ""
11
+ s.summary = %q{Simple dependency injection container}
12
+ s.description = %q{Simple dependency injection container}
13
+
14
+ s.rubyforge_project = "dic"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,3 @@
1
+ module Dic
2
+ VERSION = "1.0.0"
3
+ end
data/lib/dic.rb ADDED
@@ -0,0 +1,87 @@
1
+ class DicError < StandardError; end
2
+ class Dic
3
+ attr_reader :procs
4
+
5
+ def initialize
6
+ @procs = {}
7
+ @values = {}
8
+ @log_activity = false
9
+ @stack = []
10
+ end
11
+
12
+ def method_missing(name, *args, &proc)
13
+ # setter
14
+ if args.size == 1 || !proc.nil?
15
+ name = name.to_s.gsub(/=$/,'').intern
16
+ self[name]= args.first || proc
17
+ # getter
18
+ elsif args.empty? && proc.nil?
19
+ self[name] or raise DicError, "#{self.class}: undefined entry #{name}"
20
+ else
21
+ raise ArgumentError, "invalid argument #{name}, #{args.inspect}"
22
+ end
23
+ end
24
+
25
+ def [](key)
26
+ unless @values.has_key?(key)
27
+ raise Dic::DicError, "recursively resolving key #{key}, stack=#{@stack.inspect}" if @stack.include?(key)
28
+ @stack.push key
29
+ @values[key] = create(key)
30
+ debug{"resolved key=#{key}=#{@values[key].inspect}"}
31
+ @stack.pop
32
+ end
33
+ @values[key]
34
+ end
35
+
36
+ def []=(name,proc)
37
+ debug{"setting #{name}=#{proc.inspect}"}
38
+ @procs[name] = proc
39
+ @values.delete(name)
40
+ end
41
+
42
+ def create(key)
43
+ if (proc = @procs[key])
44
+ res = proc.respond_to?(:call) ? proc.call : proc
45
+ debug{"created for key=#{key}=#{res.inspect}, proc=#{proc}"}
46
+ else
47
+ res = nil
48
+ debug{"created for key=#{key} => nil"}
49
+ end
50
+ res
51
+ end
52
+
53
+ alias_method :set, :[]=
54
+
55
+ def keys
56
+ @procs.keys
57
+ end
58
+
59
+ def values
60
+ keys.each{|key|self[key]}
61
+ @values
62
+ end
63
+
64
+ def reset
65
+ puts "#{self}: reset"
66
+ @values.clear
67
+ end
68
+
69
+ def dump
70
+ puts "dumping #{self}"
71
+ puts "=========================="
72
+ keys.sort{|a,b|a.to_s <=> b.to_s}.each do |key|
73
+ value = begin
74
+ self[key]
75
+ rescue Exception => e
76
+ "ERROR: #{e.inspect}"
77
+ end
78
+ puts "%-20s = %s" % [ key, value ]
79
+ end
80
+ end
81
+
82
+ protected
83
+ def debug(msg=nil)
84
+ msg = msg || yield
85
+ puts "#{self}: #{msg}" if $DEBUG
86
+ end
87
+ end
data/test/dic_test.rb ADDED
@@ -0,0 +1,41 @@
1
+ # encoding: UTF-8
2
+ $:.unshift File.expand_path( '../lib/', File.dirname( __FILE__))
3
+
4
+ require 'minitest/autorun'
5
+ require 'dic'
6
+
7
+ class DicTest < MiniTest::Unit::TestCase
8
+
9
+ class MyDic < Dic
10
+ def initialize
11
+ super
12
+
13
+ foo 'bar'
14
+ once { @once||=0; @once=+1 }
15
+ name { 'Thomas' }
16
+ upfoo { foo.upcase }
17
+
18
+ circle1 { circle2 }
19
+ circle2 { circle1 }
20
+ end
21
+ end
22
+
23
+ def test_dic
24
+ dic = MyDic.new
25
+ assert_equal 'bar', dic.foo
26
+ assert_equal 'bar', dic[:foo]
27
+ end
28
+
29
+ def test_once
30
+ dic = MyDic.new
31
+ assert_equal 1, dic.once
32
+ assert_equal 1, dic.once
33
+ end
34
+
35
+ def test_circular
36
+ assert_raises DicError do
37
+ dic = MyDic.new
38
+ dic.circle1
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dic
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thomas Sonntag
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Simple dependency injection container
31
+ email:
32
+ - git@sonntagsbox.de
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .travis.yml
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - dic.gemspec
44
+ - lib/dic.rb
45
+ - lib/dic/version.rb
46
+ - test/dic_test.rb
47
+ homepage: ''
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project: dic
67
+ rubygems_version: 1.8.24
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Simple dependency injection container
71
+ test_files: []