neo-dci 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Neo::DCI [![Build Status](https://secure.travis-ci.org/neopoly/neo-dci.png?branch=master)](http://travis-ci.org/neopoly/neo-dci)
2
2
 
3
- Simple DCI
3
+ Simple DCI (Data Context Interaction).
4
+ Includes Data, Roles and Context.
5
+
6
+ [Gem](https://rubygems.org/gems/neo-dci) |
7
+ [Source](https://github.com/neopoly/neo-dci) |
8
+ [Documentation](http://rubydoc.info/github/neopoly/neo-dci/master/file/README.md)
4
9
 
5
10
  ## Installation
6
11
 
@@ -39,17 +44,20 @@ class Context < Neo::DCI::Context
39
44
  end
40
45
 
41
46
  class RenameUserContext < Context
47
+ # Define callbacks called inside a context.
48
+ callbacks :success, :failure
49
+
42
50
  def initialize(user_id, new_name)
43
51
  @user = User.find(user_id)
44
52
  @new_name = new_name
45
53
  end
46
54
 
47
- def call(result)
55
+ def call
48
56
  @user.role_as(Renamer)
49
57
  if @user.rename_to(@new_name)
50
- result.success! :new_name => @new_name
58
+ callback.call :success, @new_name
51
59
  else
52
- result.failure! "renaming failed"
60
+ callback.call :failure, "renaming failed"
53
61
  end
54
62
  end
55
63
  end
@@ -73,13 +81,13 @@ end
73
81
  ```ruby
74
82
  class UsersController < ApplicationController
75
83
  def rename
76
- result = RenameUserContext.call(current_user.id, params[:name])
77
- if result.success?
78
- new_name = result.data.new_name
79
- redirect_to users_path, :notice => "Renamed successfully to #{new_name}"
80
- else
81
- error = result.error
82
- render :alert => "Renaming failed: #{error}!"
84
+ RenameUserContext.call(current_user.id, params[:name]) do |callback|
85
+ callback.on :success do |new_name|
86
+ redirect_to users_path, :notice => "Renamed successfully to #{new_name}"
87
+ end
88
+ callback.on :failure do |error_message|
89
+ render :alert => "Renaming failed: #{error_message}!"
90
+ end
83
91
  end
84
92
  end
85
93
  end
data/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
- #!/usr/bin/env rake
2
- require "bundler/gem_tasks"
1
+ require 'bundler/setup'
2
+ require 'bundler/gem_tasks'
3
3
 
4
4
  task :default => :test
5
5
 
@@ -9,3 +9,12 @@ Rake::TestTask.new do |t|
9
9
  t.test_files = FileList['test/**/*_test.rb']
10
10
  t.verbose = true
11
11
  end
12
+
13
+ # RDoc
14
+ require 'rdoc/task'
15
+ RDoc::Task.new do |rdoc|
16
+ rdoc.title = "Neo::DCI"
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.main = 'README.md'
19
+ rdoc.rdoc_files.include('README.md', 'lib/**/*.rb')
20
+ end
@@ -2,7 +2,6 @@ require 'neo/dci/version'
2
2
 
3
3
  require 'neo/dci/data'
4
4
  require 'neo/dci/role'
5
- require 'neo/dci/context_result'
6
5
  require 'neo/dci/context'
7
6
 
8
7
  require 'neo/dci/task_loader' if defined?(::Rails::Railtie)
@@ -1,27 +1,38 @@
1
+ require 'on'
2
+
1
3
  module Neo
2
4
  module DCI
3
5
  class Context
4
-
5
6
  class << self
6
7
  private :new
7
8
  end
8
9
 
10
+ attr_accessor :callback
11
+
12
+ def self.callbacks(*args)
13
+ @callbacks ||= []
14
+ @callbacks = args unless args.empty?
15
+ @callbacks
16
+ end
17
+
9
18
  def self.call(*args, &block)
10
- context = new(*args, &block)
11
- result = ContextResult.new
12
- context.call(result)
13
- raise UnprocessedError unless result.processed?
14
- result
19
+ context = new(*args)
20
+ context.callback = On.new(*callbacks, &block)
21
+ context.call
22
+ raise NoCallbackCalled, callbacks unless context.callback.callback
15
23
  rescue NotImplementedError
16
24
  raise
17
25
  end
18
26
 
19
-
20
- def call(result)
27
+ def call
21
28
  raise NotImplementedError
22
29
  end
23
30
 
24
- class UnprocessedError < StandardError; end
31
+ class NoCallbackCalled < StandardError
32
+ def initialize(callbacks)
33
+ super("No callback called. Available callbacks: #{callbacks.join(', ')}")
34
+ end
35
+ end
25
36
  end
26
37
  end
27
38
  end
@@ -1,5 +1,5 @@
1
1
  module Neo
2
2
  module DCI
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -15,7 +15,10 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = Neo::DCI::VERSION
17
17
 
18
- gem.add_development_dependency "rake"
19
- gem.add_development_dependency "minitest"
20
- gem.add_development_dependency "simplecov"
18
+ gem.add_development_dependency 'rake'
19
+ gem.add_development_dependency 'rdoc'
20
+ gem.add_development_dependency 'minitest'
21
+ gem.add_development_dependency 'testem'
22
+ gem.add_development_dependency 'simplecov'
23
+ gem.add_development_dependency 'on', '~> 0.2.0'
21
24
  end
@@ -1,56 +1,69 @@
1
1
  require 'helper'
2
2
 
3
3
  class ContextTest < NeoDCICase
4
- class DummyContext < Neo::DCI::Context
5
- def initialize(&block)
6
- @block = block
7
- end
8
-
9
- def call(result)
10
- @block.call(result)
4
+ class TestContext < Neo::DCI::Context
5
+ def initialize(*args)
6
+ @args = args
11
7
  end
12
8
  end
13
9
 
14
- let(:data) { {:foo => :bar } }
15
- let(:error) { RuntimeError.new }
16
-
17
10
  test "initialize is private" do
18
11
  assert_raises NoMethodError do
19
- DummyContext.new
12
+ TestContext.new
20
13
  end
21
14
  end
22
15
 
23
16
  test "raises not implement error" do
24
17
  assert_raises NotImplementedError do
25
- Class.new(Neo::DCI::Context).call
18
+ Class.new(Neo::DCI::Context) do
19
+ callbacks :foo
20
+ end.call {}
26
21
  end
27
22
  end
28
23
 
29
- test "raises unprocessed error if result is not processed" do
30
- assert_raises Neo::DCI::Context::UnprocessedError do
31
- Class.new(Neo::DCI::Context) do
32
- def call(result); end
33
- end.call
24
+ test "yields callback" do
25
+ context = Class.new(TestContext) do
26
+ callbacks :success
27
+
28
+ def call
29
+ callback.call :success, @args
30
+ end
34
31
  end
35
- end
36
32
 
37
- test "returns context result if success" do
38
- result = DummyContext.call do |result|
39
- result.success!(data)
33
+ block_called = false
34
+ result = nil
35
+
36
+ context.call(:foo, :bar) do |callback|
37
+ block_called = true
38
+ callback.on :success do |args|
39
+ result = args
40
+ end
40
41
  end
41
- assert result.success?
42
- refute result.failure?
43
- assert_equal :bar, result.data.foo
42
+
43
+ assert_equal true, block_called
44
+ assert_equal [ :foo, :bar ], result
44
45
  end
45
46
 
46
- test "returns context result if failure" do
47
- result = DummyContext.call do |result|
48
- result.failure!(error, data)
47
+ test "ensure callback called" do
48
+ context = Class.new(TestContext) do
49
+ callbacks :success
50
+
51
+ def call
52
+ end
49
53
  end
50
- assert result.failure?
51
- refute result.success?
52
- assert_same error, result.error
53
- assert_equal :bar, result.data.foo
54
+
55
+ e = assert_raises Neo::DCI::Context::NoCallbackCalled do
56
+ context.call do |callback|
57
+ end
58
+ end
59
+ assert_equal "No callback called. Available callbacks: success", e.message
54
60
  end
55
61
 
62
+ test "do not overwrite callbacks in subclasses" do
63
+ context1 = Class.new(TestContext) { callbacks :foo }
64
+ context2 = Class.new(TestContext) { callbacks :bar }
65
+
66
+ assert_equal [ :foo ], context1.callbacks
67
+ assert_equal [ :bar ], context2.callbacks
68
+ end
56
69
  end
@@ -1,12 +1,9 @@
1
1
  require 'simplecov' if ENV['COVERAGE']
2
2
 
3
3
  require 'minitest/autorun'
4
+ require 'testem'
4
5
 
5
6
  require 'neo/dci'
6
7
 
7
- class NeoDCICase < MiniTest::Spec
8
- class << self
9
- alias :context :describe
10
- alias :test :it
11
- end
8
+ class NeoDCICase < Testem
12
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neo-dci
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-11-06 00:00:00.000000000 Z
14
+ date: 2012-11-29 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rake
@@ -29,6 +29,22 @@ dependencies:
29
29
  - - ! '>='
30
30
  - !ruby/object:Gem::Version
31
31
  version: '0'
32
+ - !ruby/object:Gem::Dependency
33
+ name: rdoc
34
+ requirement: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
32
48
  - !ruby/object:Gem::Dependency
33
49
  name: minitest
34
50
  requirement: !ruby/object:Gem::Requirement
@@ -45,6 +61,22 @@ dependencies:
45
61
  - - ! '>='
46
62
  - !ruby/object:Gem::Version
47
63
  version: '0'
64
+ - !ruby/object:Gem::Dependency
65
+ name: testem
66
+ requirement: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ type: :development
73
+ prerelease: false
74
+ version_requirements: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
48
80
  - !ruby/object:Gem::Dependency
49
81
  name: simplecov
50
82
  requirement: !ruby/object:Gem::Requirement
@@ -61,6 +93,22 @@ dependencies:
61
93
  - - ! '>='
62
94
  - !ruby/object:Gem::Version
63
95
  version: '0'
96
+ - !ruby/object:Gem::Dependency
97
+ name: 'on'
98
+ requirement: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: 0.2.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ~>
110
+ - !ruby/object:Gem::Version
111
+ version: 0.2.0
64
112
  description: Simple DCI
65
113
  email:
66
114
  - ps@neopoly.de
@@ -80,7 +128,6 @@ files:
80
128
  - lib/neo-dci.rb
81
129
  - lib/neo/dci.rb
82
130
  - lib/neo/dci/context.rb
83
- - lib/neo/dci/context_result.rb
84
131
  - lib/neo/dci/data.rb
85
132
  - lib/neo/dci/role.rb
86
133
  - lib/neo/dci/task_loader.rb
@@ -89,7 +136,6 @@ files:
89
136
  - lib/tasks/share/app/contexts/context.rb
90
137
  - lib/tasks/share/app/roles/role.rb
91
138
  - neo-dci.gemspec
92
- - test/context_result_test.rb
93
139
  - test/context_test.rb
94
140
  - test/helper.rb
95
141
  - test/role_test.rb
@@ -105,12 +151,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
105
151
  - - ! '>='
106
152
  - !ruby/object:Gem::Version
107
153
  version: '0'
154
+ segments:
155
+ - 0
156
+ hash: -3362418473646590166
108
157
  required_rubygems_version: !ruby/object:Gem::Requirement
109
158
  none: false
110
159
  requirements:
111
160
  - - ! '>='
112
161
  - !ruby/object:Gem::Version
113
162
  version: '0'
163
+ segments:
164
+ - 0
165
+ hash: -3362418473646590166
114
166
  requirements: []
115
167
  rubyforge_project:
116
168
  rubygems_version: 1.8.24
@@ -118,8 +170,6 @@ signing_key:
118
170
  specification_version: 3
119
171
  summary: Includes Data, Roles and Context.
120
172
  test_files:
121
- - test/context_result_test.rb
122
173
  - test/context_test.rb
123
174
  - test/helper.rb
124
175
  - test/role_test.rb
125
- has_rdoc:
@@ -1,43 +0,0 @@
1
- require 'ostruct'
2
-
3
- module Neo
4
- module DCI
5
- class ContextResult
6
- attr_reader :error, :data
7
-
8
- def success!(data = {})
9
- self.data = data
10
- end
11
-
12
- def failure!(error, data = {})
13
- @error = error
14
- self.data = data
15
- end
16
-
17
- def data= (data)
18
- raise ArgumentError, "Data is already set. Call success! or failure! only once." if @data
19
- @data = Data.new(data)
20
- @data.freeze
21
- end
22
- private :data=
23
-
24
- def success?
25
- processed? && !error
26
- end
27
-
28
- def failure?
29
- processed? && !!error
30
- end
31
-
32
- def processed?
33
- !!@data
34
- end
35
-
36
- class Data < OpenStruct
37
- def to_hash
38
- @table
39
- end
40
- end
41
- end
42
- end
43
- end
@@ -1,73 +0,0 @@
1
- require 'helper'
2
-
3
- class ContextResultTest < NeoDCICase
4
- let(:result) { Neo::DCI::ContextResult.new }
5
- let(:error) { RuntimeError.new }
6
- let(:data) { {:foo => :bar} }
7
-
8
- test "returns success? when no error present" do
9
- result.success!
10
- assert result.success?
11
- assert result.processed?
12
- refute result.failure?
13
- assert_nil result.error
14
- end
15
-
16
- test "returns failure? on error" do
17
- result.failure!(error)
18
- refute result.success?
19
- assert result.failure?
20
- assert result.processed?
21
- assert_same error, result.error
22
- end
23
-
24
- test "returns not processed? on default" do
25
- refute result.processed?
26
- refute result.success?
27
- refute result.failure?
28
- end
29
-
30
- test "returns data as payload on success" do
31
- result.success!(data)
32
- assert_equal data[:foo], result.data.foo
33
- end
34
-
35
- test "returns data as payload on failure" do
36
- result.failure!(error, data)
37
- assert_equal data[:foo], result.data.foo
38
- end
39
-
40
- test "returns data as hash" do
41
- result.success!(data)
42
- assert_equal data, result.data.to_hash
43
- end
44
-
45
- context "frozen data" do
46
- test "after success!" do
47
- result.success!
48
- assert result.data.frozen?
49
- end
50
- test "after failure!" do
51
- result.failure!(error)
52
- assert result.data.frozen?
53
- end
54
- end
55
-
56
- context "can only be called once" do
57
- before do
58
- result.success!
59
- end
60
-
61
- test "with success!" do
62
- assert_raises ArgumentError do
63
- result.success!
64
- end
65
- end
66
-
67
- test "with failure!" do
68
- assert_raises ArgumentError do
69
- result.failure!(error)
70
- end
71
- end
72
- end
73
- end