chickadee 6.0.1 → 6.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7c45a1f20bf614dc238cf437da29378262cf125f
4
- data.tar.gz: f9e20b5cf1f972a23c706f5d9a9d37ff81c83cbd
3
+ metadata.gz: 9ae3ac8453e16a736b621287feb437dffcd6f9f5
4
+ data.tar.gz: 346b0090f2fd811fbfba8eab282695f97ec743c1
5
5
  SHA512:
6
- metadata.gz: 1d4afff9ff43424b25c81fa10c890355f77077f2a63c04197069d6dec37f14d4b169d7f269834950b8a67bcb4ed76025a02f9a69ca5e7ea70b9d6ed869986d03
7
- data.tar.gz: 6068df3441f7d418813d51da6ff6fdef9052b2d656f1aad18dd40295a4492dd6dc6fffc28ccbba47ffcd410e45660c2562e9f4e2ffe61ad8c24d1442e549f18c
6
+ metadata.gz: 7a678a42ca376f9555ad31c3a1672f82ad636c97095997dc4912dcb4ae26ce71414709a47798943a626a1735462c445e1f609e6d0220592598aaefc1e93db4ed
7
+ data.tar.gz: 166b7822c624fbbe1b6acc4f100310c7606c00db771fcbe7ee778c2f923583634a4b0e4e8f2f3243cc8ded7bff491bc9cd5d65b7dc982fccaa32560cdb5ebe23
@@ -8,6 +8,4 @@ module Chickadee
8
8
  c.instance_eval(&@block) if @block
9
9
  end
10
10
  end
11
- end
12
-
13
-
11
+ end
@@ -1,12 +1,12 @@
1
1
  module Chickadee
2
2
  class Bootstrapper
3
- def initialize(container_class, installer)
3
+ def initialize(container_class, *installers)
4
4
  @container_class = container_class
5
- @installer = installer
5
+ @installers = installers
6
6
  end
7
7
 
8
8
  def new_bootstrap_container
9
- @container_class.install(@installer)
9
+ @container_class.install(*@installers)
10
10
  end
11
11
  end
12
12
  end
@@ -0,0 +1,15 @@
1
+ require 'ioc'
2
+ require 'chickadee/utils/string_inflections'
3
+
4
+ module Chickadee
5
+ class Container < ::IOC::Container
6
+ def register_service(service_name, service_object)
7
+ register(service_name, service_object, :as => :instance)
8
+ end
9
+
10
+ def register_handler(klass)
11
+ name = StringInflections.underscore(StringInflections.demodulize(klass.name)).to_sym
12
+ register(name, klass)
13
+ end
14
+ end
15
+ end
@@ -1,33 +1,31 @@
1
- require 'chickadee/container'
2
- require 'chickadee/bootstrapper'
3
- require 'chickadee/block_installer'
1
+ require 'chickadee/application/container'
2
+ require 'chickadee/application/bootstrapper'
3
+ require 'chickadee/application/block_installer'
4
+ require 'chickadee/utils/string_inflections'
4
5
 
5
6
  module Chickadee
6
7
  class Application
7
- def self.handler_map
8
- @handler_map ||= {}
8
+ def self.install(&block)
9
+ @installer = BlockInstaller.new(&block)
9
10
  end
10
11
 
11
- def self.map(hash)
12
- handler_map.update(hash)
13
- end
14
-
15
- def self.handler_class_for(request)
16
- handler_map.fetch(request.class)
17
- rescue KeyError => e
18
- raise(ArgumentError, "Unrecognized request class: '#{request.class.name}'")
12
+ def self.installer
13
+ @installer
19
14
  end
20
15
 
21
16
  def initialize(&block)
22
- @bootstrapper = Bootstrapper.new(Container, BlockInstaller.new(&block))
17
+ installers = []
18
+ installers << self.class.installer if self.class.installer
19
+ installers << BlockInstaller.new(&block) if block_given?
20
+ @bootstrapper = Bootstrapper.new(Container, *installers)
23
21
  end
24
22
 
25
23
  def handle(request)
24
+ handler_registry_name = StringInflections.underscore(StringInflections.demodulize("#{request.class.name}Handler")).to_sym
26
25
  @bootstrapper
27
26
  .new_bootstrap_container
28
- .resolve_anonymous(handler_class_for(request))
27
+ .resolve(handler_registry_name)
29
28
  .handle(request)
30
- nil
31
29
  end
32
30
 
33
31
  def queue(request)
@@ -35,13 +33,6 @@ module Chickadee
35
33
  .new_bootstrap_container
36
34
  .resolve(:request_queue)
37
35
  .queue(request)
38
- nil
39
- end
40
-
41
- private
42
-
43
- def handler_class_for(request)
44
- self.class.handler_class_for(request)
45
36
  end
46
37
  end
47
38
  end
@@ -1,3 +1,4 @@
1
1
  module Chickadee
2
2
  class ChickadeeError < StandardError; end
3
+ class RoutingError < StandardError; end
3
4
  end
@@ -0,0 +1,20 @@
1
+ module Chickadee
2
+ module StringInflections
3
+ def self.underscore(str)
4
+ str
5
+ .gsub(/::/, '/')
6
+ .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
7
+ .gsub(/([a-z\d])([A-Z])/,'\1_\2')
8
+ .tr("-", "_")
9
+ .downcase
10
+ end
11
+
12
+ def self.deconstantize(constant_name)
13
+ constant_name.split( '::' )[0,constant_name.split( '::' ).length-1].join('::')
14
+ end
15
+
16
+ def self.demodulize(constant_name)
17
+ constant_name.split('::').last
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Chickadee
2
- VERSION = "6.0.1"
2
+ VERSION = "6.0.2"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
- require 'chickadee/block_installer'
3
- require 'chickadee/container'
2
+ require 'chickadee/application/block_installer'
3
+ require 'chickadee/application/container'
4
4
 
5
5
  describe Chickadee::BlockInstaller do
6
6
  it 'can install from block' do
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
- require 'chickadee/bootstrapper'
3
- require 'chickadee/container'
4
- require 'chickadee/block_installer'
2
+ require 'chickadee/application/bootstrapper'
3
+ require 'chickadee/application/container'
4
+ require 'chickadee/application/block_installer'
5
5
 
6
6
  describe Chickadee::Bootstrapper do
7
7
  describe '#new_bootstrap_container' do
@@ -2,48 +2,26 @@ require 'spec_helper'
2
2
  require 'chickadee/application'
3
3
 
4
4
  describe Chickadee::Application do
5
- describe '::handle' do
6
- it 'can map a request to a handler' do
7
- request_class = Class.new
8
- handler_class = Class.new
9
- app_class = Class.new(Chickadee::Application)
10
- app_class.map(request_class => handler_class)
11
- app_class.handler_class_for(request_class.new).must_equal(handler_class)
12
- end
13
- end
14
-
15
- describe '#map' do
16
- context 'when the request has been mapped' do
17
- it 'can handle a request' do
18
- request_class = Class.new do
19
- attr_accessor :output
20
- end
21
- handler_class = Class.new do
22
- def handle(request)
23
- request.output = "I have handled the request"
24
- end
25
- end
26
- app_class = Class.new(Chickadee::Application)
27
- app_class.map(request_class => handler_class)
28
- request = request_class.new
29
- app = app_class.new
30
- app.handle(request)
31
- request.output.must_equal('I have handled the request')
5
+ it 'can handle a request' do
6
+ class RequestHandler
7
+ def handle(request)
8
+ request.is_success = true
32
9
  end
33
10
  end
34
11
 
35
- context 'when the request has not been mapped' do
36
- it 'can handle a request' do
37
- request_class = Class.new
38
- handler_class = Class.new
39
- app_class = Class.new(Chickadee::Application)
40
- request = request_class.new
41
- app = app_class.new
12
+ class Request
13
+ attr_accessor :is_success
14
+ end
42
15
 
43
- proc {
44
- app.handle(request).must_raise(ArgumentError)
45
- }
16
+ class App < Chickadee::Application
17
+ install do
18
+ register_handler(RequestHandler)
46
19
  end
47
20
  end
21
+
22
+ app = App.new
23
+ request = Request.new
24
+ app.handle(request)
25
+ request.is_success.must_equal(true)
48
26
  end
49
27
  end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+ require 'chickadee/utils/string_inflections'
3
+
4
+ describe Chickadee::StringInflections do
5
+ describe '#underscore' do
6
+ it 'changes string to underscore' do
7
+ inflector.underscore("ThisIsAString").must_equal('this_is_a_string')
8
+ end
9
+
10
+ it 'changes module notation to slash' do
11
+ inflector.underscore("ThisIs::AString").must_equal('this_is/a_string')
12
+ end
13
+ end
14
+
15
+ describe '#deconstantize' do
16
+ it 'gets namespace for current constant' do
17
+ inflector.deconstantize('A::B::C').must_equal('A::B')
18
+ end
19
+ end
20
+
21
+ describe '#demodulize' do
22
+ it 'gets class name' do
23
+ inflector.demodulize('A::B::C').must_equal('C')
24
+ end
25
+ end
26
+
27
+ def inflector
28
+ Chickadee::StringInflections
29
+ end
30
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chickadee
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.1
4
+ version: 6.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Lott
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-05 00:00:00.000000000 Z
11
+ date: 2014-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ioc
@@ -82,15 +82,17 @@ files:
82
82
  - chickadee.gemspec
83
83
  - lib/chickadee.rb
84
84
  - lib/chickadee/application.rb
85
- - lib/chickadee/block_installer.rb
86
- - lib/chickadee/bootstrapper.rb
87
- - lib/chickadee/container.rb
85
+ - lib/chickadee/application/block_installer.rb
86
+ - lib/chickadee/application/bootstrapper.rb
87
+ - lib/chickadee/application/container.rb
88
88
  - lib/chickadee/exceptions.rb
89
+ - lib/chickadee/utils/string_inflections.rb
89
90
  - lib/chickadee/version.rb
91
+ - spec/application/block_installer_spec.rb
92
+ - spec/application/bootstrapper_spec.rb
90
93
  - spec/application_spec.rb
91
- - spec/block_installer_spec.rb
92
- - spec/bootstrapper_spec.rb
93
94
  - spec/spec_helper.rb
95
+ - spec/utils/string_inflections_spec.rb
94
96
  homepage: ''
95
97
  licenses:
96
98
  - MIT
@@ -116,7 +118,8 @@ signing_key:
116
118
  specification_version: 4
117
119
  summary: Lightweight command library
118
120
  test_files:
121
+ - spec/application/block_installer_spec.rb
122
+ - spec/application/bootstrapper_spec.rb
119
123
  - spec/application_spec.rb
120
- - spec/block_installer_spec.rb
121
- - spec/bootstrapper_spec.rb
122
124
  - spec/spec_helper.rb
125
+ - spec/utils/string_inflections_spec.rb
@@ -1,9 +0,0 @@
1
- require 'ioc'
2
-
3
- module Chickadee
4
- class Container < ::IOC::Container
5
- def register_service(service_name, service_object)
6
- register(service_name, service_object, :as => :instance)
7
- end
8
- end
9
- end