lingohub_utils 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,28 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "lh/dependency_injection/context"
3
+
4
+ module LH
5
+ module DependencyInjection
6
+ module Autowire
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ def get_dependency(dependency_name)
12
+ Context.get(dependency_name)
13
+ end
14
+
15
+ module ClassMethods
16
+ def autowire(*accessor_names)
17
+ accessor_names.each do |name|
18
+ send :define_method, name do
19
+ Context.get(name)
20
+ end
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,51 @@
1
+ module LH
2
+ module DependencyInjection
3
+ class ContextHash
4
+
5
+ def initialize
6
+ @context = { }
7
+ end
8
+
9
+ def merge!(other_context)
10
+ @context.merge!(other_context.context)
11
+ end
12
+
13
+ def [](key)
14
+ if @context[key].respond_to? :call
15
+ @context[key] = @context[key].call
16
+ end
17
+ @context[key]
18
+ end
19
+
20
+ def has_key?(key)
21
+ @context.has_key?(key)
22
+ end
23
+
24
+ def specify(key, value=nil, &block)
25
+ @context[key] = value || block
26
+ end
27
+
28
+ protected
29
+
30
+ def context
31
+ @context
32
+ end
33
+ end
34
+
35
+ class Context
36
+ @@registry = LH::DependencyInjection::ContextHash.new
37
+
38
+ def self.register
39
+ r = LH::DependencyInjection::ContextHash.new
40
+ yield r
41
+ @@registry.merge!(r)
42
+ end
43
+
44
+ def self.get(name)
45
+ raise ArgumentError.new("No dependency registered under this name: '#{name}'.") unless @@registry.has_key?(name)
46
+ @@registry[name]
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LH
3
+ module Exceptions
4
+ class BaseError < ::StandardError
5
+ @nested
6
+
7
+ def initialize(msg_or_exc = nil)
8
+ if msg_or_exc.nil?
9
+ super
10
+ elsif msg_or_exc.is_a?(String)
11
+ super(msg_or_exc)
12
+ else
13
+ @nested = msg_or_exc
14
+ super(msg_or_exc.message)
15
+ set_backtrace(@nested.backtrace)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'lh/exceptions/base_error'
3
+
4
+ module LH
5
+ module Exceptions
6
+ class NotFoundError < BaseError
7
+ def initialize(message = nil)
8
+ super(message || "errors.messages.not_found")
9
+ end
10
+ end
11
+
12
+ class AccessDenied < BaseError;
13
+ end
14
+
15
+ class ParameterError < BaseError;
16
+ end
17
+
18
+ class NotAcceptableError < BaseError;
19
+ end
20
+
21
+ class IllegalStateError < BaseError;
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module LingohubUtils
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
@@ -0,0 +1,28 @@
1
+ require "lh/dependency_injection/autowire"
2
+
3
+ describe LH::DependencyInjection::Autowire do
4
+ before :each do
5
+ LH::DependencyInjection::Context.register do |r|
6
+ r[:injected] = Injected.new
7
+ end
8
+ end
9
+
10
+ it "should the specified injected class be present" do
11
+ injected_in = InjectedIn.new
12
+
13
+ injected_in.injected.should be_a Injected
14
+ end
15
+
16
+
17
+ class Injected
18
+ end
19
+
20
+ class InjectedIn
21
+ include LH::DependencyInjection::Autowire
22
+
23
+ autowire :injected
24
+ end
25
+ end
26
+
27
+
28
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lingohub_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -31,6 +31,10 @@ files:
31
31
  - lib/lh/core_extensions/hash/raise_unless_has_key.rb
32
32
  - lib/lh/core_extensions/object/raise_if_nil.rb
33
33
  - lib/lh/core_extensions/struct/to_hash.rb
34
+ - lib/lh/dependency_injection/autowire.rb
35
+ - lib/lh/dependency_injection/context.rb
36
+ - lib/lh/exceptions/base_error.rb
37
+ - lib/lh/exceptions/general_exceptions.rb
34
38
  - lib/lingohub_utils.rb
35
39
  - lib/lingohub_utils/version.rb
36
40
  - lingohub_utils.gemspec
@@ -40,6 +44,7 @@ files:
40
44
  - spec/lh/core_extensions/hash/raise_if_value_nil_spec.rb
41
45
  - spec/lh/core_extensions/hash/raise_unless_has_key_spec.rb
42
46
  - spec/lh/core_extensions/object/raise_if_nil_spec.rb
47
+ - spec/lh/dependency_injection/autowire_spec.rb
43
48
  - spec/spec_helper.rb
44
49
  homepage: http://lingohub.com
45
50
  licenses: []
@@ -73,4 +78,5 @@ test_files:
73
78
  - spec/lh/core_extensions/hash/raise_if_value_nil_spec.rb
74
79
  - spec/lh/core_extensions/hash/raise_unless_has_key_spec.rb
75
80
  - spec/lh/core_extensions/object/raise_if_nil_spec.rb
81
+ - spec/lh/dependency_injection/autowire_spec.rb
76
82
  - spec/spec_helper.rb