narrative 0.0.2 → 0.0.3

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: 4be14aa29aa4368e0cd86bfce8682eb9e01bf261
4
- data.tar.gz: 107c00177921a2d3ea4d96059bb15cf9110363fe
3
+ metadata.gz: b71ff082b5a80892b4c77691c0b434703c50e884
4
+ data.tar.gz: f6a1ec6fa9b164d800d3b67900305d99e42f588d
5
5
  SHA512:
6
- metadata.gz: 1cfb7f996f64b1a7a8b37628a4325e962ee3b44614585fd5def54e7f66522f2b2b49210ba82df718dccfe14231731d6a8b52d6bb3bd0b43a4d30d85206d85832
7
- data.tar.gz: 7a6ae0d04bb3e8d0130ba937ca7c28b0c1a63d606daf73bc91691805eec2f1bd2fcd169e08f72ee18979abf7383b15cfd68fe21cc13e5ac725d856f40cb8e220
6
+ metadata.gz: e839c91bd4310fd209cfaa56a5af781f4208ba076a5c0e7b66e9fa601d5c3522e422a5919ae5bfd46cc16acbd05b32b7d6664e31f19ab649907d3ff962d0fafe
7
+ data.tar.gz: 8406e009e03aa09a616eeda6771f83b411fdc5f179ab6bd831dbbcd0904297ae5dce191465be69e287e3f79598d3cb5f5520e55509f53aa8566f456c6ff8abd5
data/README.md CHANGED
@@ -1,10 +1,9 @@
1
1
  [![Build Status](https://travis-ci.org/esminc/narrative.svg?branch=master)](https://travis-ci.org/esminc/narrative)
2
2
  [![Code Climate](https://codeclimate.com/github/esminc/narrative.png)](https://codeclimate.com/github/esminc/narrative)
3
- [![PullReview stats](https://www.pullreview.com/github/esminc/narrative/badges/master.svg?)](https://www.pullreview.com/github/esminc/narrative/reviews/master)
4
3
 
5
4
  # Narrative
6
5
 
7
- TODO: Write a gem description
6
+ A simple implementation of DCI.
8
7
 
9
8
  ## Installation
10
9
 
data/lib/narrative.rb CHANGED
@@ -1,5 +1,4 @@
1
- require 'narrative/context'
2
- require 'narrative/context/teller'
1
+ require 'narrative/scene'
3
2
 
4
3
  module Narrative
5
4
  end
@@ -1,35 +1,27 @@
1
1
  module Narrative
2
- module Context
3
- class RoleDefinition
4
- attr_reader :name
2
+ class RoleDefinition
3
+ attr_reader :name
5
4
 
6
- def initialize(name, partners, &responsibilities)
7
- @name = name
8
- @partners = partners
9
- @responsibilities = responsibilities
10
- end
11
-
12
- def cast!(actors)
13
- actor = actor!(actors)
14
- relationship!(actor, actors)
5
+ def initialize(name, partners, &responsibilities)
6
+ @name = name
7
+ @partners = partners
8
+ @responsibilities = responsibilities
9
+ end
15
10
 
16
- actor
17
- end
11
+ def cast!(actors)
12
+ role = Module.new(&@responsibilities)
13
+ acquaint! role, actors.slice(*@partners)
18
14
 
19
- private
15
+ actors[@name].extend(role)
16
+ end
20
17
 
21
- def actor!(actors)
22
- actor = actors[@name]
23
- actor.instance_eval(&@responsibilities)
24
- actor
25
- end
18
+ private
26
19
 
27
- def relationship!(actor, actors)
28
- klass = class << actor; self end
29
- actors.slice(*@partners).each do |role_name, partner|
30
- klass.instance_eval do
31
- define_method(role_name) { partner }
32
- end
20
+ def acquaint!(role_module, partners)
21
+ role_module.instance_eval do
22
+ partners.each do |role_name, partner|
23
+ define_method(role_name) { partner }
24
+ private role_name
33
25
  end
34
26
  end
35
27
  end
@@ -2,11 +2,11 @@ require 'active_support'
2
2
  require_relative 'role_definition'
3
3
 
4
4
  module Narrative
5
- module Context
5
+ module Scene
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
- cattr_reader(:roles) { [] }
9
+ private(*cattr_accessor(:roles) { [] })
10
10
  end
11
11
 
12
12
  module ClassMethods
@@ -14,6 +14,13 @@ module Narrative
14
14
  roles << RoleDefinition.new(name, partners, &block)
15
15
  define_method(name.to_sym) { @actors[name] }
16
16
  end
17
+
18
+ def principal(name, partners: [], &block)
19
+ raise 'principal is defined twice' if method_defined?(:principal)
20
+
21
+ role name, partners: partners, &block
22
+ alias_method :principal, name
23
+ end
17
24
  end
18
25
 
19
26
  def initialize(data)
@@ -23,12 +30,13 @@ module Narrative
23
30
  end
24
31
 
25
32
  def perform(&block)
26
- block.call @actors.slice(*block.parameters.map(&:last))
33
+ block.call principal
27
34
  end
28
35
 
29
36
  private
30
37
 
31
38
  def validate!(data)
39
+ raise 'principal definition is required' unless self.class.method_defined?(:principal)
32
40
  raise 'data and role definition did not same' if data.keys.to_set != roles.map(&:name).to_set
33
41
  raise 'data did not allow to contain nil' if data.values.include?(nil)
34
42
  end
@@ -1,3 +1,3 @@
1
1
  module Narrative
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -1,17 +1,23 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Narrative::Context do
4
- describe '.role' do
5
- class ProjectContext
6
- include Narrative::Context
3
+ describe Narrative::Scene do
4
+ class ProjectContext
5
+ include Narrative::Scene
6
+
7
+ principal :programmer do
8
+ def coding; end
9
+ end
7
10
 
8
- role :programmer do; end
11
+ role :tester do
12
+ def report_bug; end
9
13
  end
14
+ end
10
15
 
16
+ describe '.role' do
11
17
  class AnotherProjectContext
12
- include Narrative::Context
18
+ include Narrative::Scene
13
19
 
14
- role :product_owner do; end
20
+ principal :product_owner do; end
15
21
  end
16
22
 
17
23
  subject { ProjectContext.roles.map(&:name) }
@@ -21,18 +27,6 @@ describe Narrative::Context do
21
27
  end
22
28
 
23
29
  describe 'cast roles to data' do
24
- class ProjectContext
25
- include Narrative::Context
26
-
27
- role :programmer do
28
- def coding; end
29
- end
30
-
31
- role :tester do
32
- def report_bug; end
33
- end
34
- end
35
-
36
30
  let(:data) { {programmer: 'alice', tester: 'bob' } }
37
31
 
38
32
  subject { ProjectContext.new(data) }
@@ -57,15 +51,20 @@ describe Narrative::Context do
57
51
 
58
52
  describe 'can refer to other roles' do
59
53
  class BugfixContext
60
- include Narrative::Context
54
+ include Narrative::Scene
61
55
 
62
56
  role :programmer do
63
57
  def correct!(code)
58
+ add_unit_test_for code
64
59
  code.modified!
65
60
  end
61
+
62
+ private
63
+
64
+ def add_unit_test_for(code); end
66
65
  end
67
66
 
68
- role :tester, partners: [:programmer] do
67
+ principal :tester, partners: [:programmer] do
69
68
  def report_bug(code)
70
69
  programmer.correct!(code)
71
70
  end
@@ -76,7 +75,7 @@ describe Narrative::Context do
76
75
  code = double('defected code')
77
76
  allow(code).to receive(:modified!)
78
77
 
79
- BugfixContext.new(programmer: 'alice', tester: 'bob').perform do |programmer:, tester:|
78
+ BugfixContext.new(programmer: 'alice', tester: 'bob').perform do |tester|
80
79
  tester.report_bug(code)
81
80
  end
82
81
 
@@ -85,25 +84,13 @@ describe Narrative::Context do
85
84
  end
86
85
 
87
86
  describe '#perform' do
88
- class ProjectContext
89
- include Narrative::Context
90
-
91
- role :programmer do
92
- def coding; end
93
- end
94
-
95
- role :tester do
96
- def report_bug; end
97
- end
98
- end
99
-
100
87
  specify do
101
88
  called = false
102
- context = ProjectContext.new({programmer: 'alice', tester: 'bob'})
89
+ alice = 'alice'
90
+ context = ProjectContext.new({programmer: alice, tester: 'bob'})
103
91
 
104
- context.perform do |programmer:, tester:|
105
- expect(programmer).to eq(context.programmer)
106
- expect(tester).to eq(context.tester)
92
+ context.perform do |programmer|
93
+ expect(programmer).to be(alice)
107
94
 
108
95
  called = true
109
96
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: narrative
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takuya Watanabe
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-18 00:00:00.000000000 Z
12
+ date: 2014-05-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -41,13 +41,11 @@ files:
41
41
  - README.md
42
42
  - Rakefile
43
43
  - lib/narrative.rb
44
- - lib/narrative/context.rb
45
- - lib/narrative/context/teller.rb
46
44
  - lib/narrative/role_definition.rb
45
+ - lib/narrative/scene.rb
47
46
  - lib/narrative/version.rb
48
47
  - narrative.gemspec
49
- - spec/lib/narrative/context/teller_spec.rb
50
- - spec/lib/narrative/context_spec.rb
48
+ - spec/lib/narrative/scene_spec.rb
51
49
  - spec/spec_helper.rb
52
50
  homepage: https://github.com/esminc/narrative
53
51
  licenses:
@@ -74,6 +72,5 @@ signing_key:
74
72
  specification_version: 4
75
73
  summary: a simple implementation of DCI.
76
74
  test_files:
77
- - spec/lib/narrative/context/teller_spec.rb
78
- - spec/lib/narrative/context_spec.rb
75
+ - spec/lib/narrative/scene_spec.rb
79
76
  - spec/spec_helper.rb
@@ -1,19 +0,0 @@
1
- require 'active_support/core_ext/string/inflections'
2
-
3
- module Narrative
4
- module Context
5
- module Teller
6
- def with_context(context_name, data, &block)
7
- context_for(context_name, data).perform(&block)
8
- end
9
-
10
- private
11
-
12
- def context_for(name, data)
13
- context_names = name.split << 'context'
14
- context_class = context_names.map(&:capitalize).join.constantize
15
- context_class.new(data)
16
- end
17
- end
18
- end
19
- end
@@ -1,40 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Narrative::Context::Teller do
4
- describe '#with_context' do
5
- class RefactorContext
6
- include Narrative::Context
7
-
8
- role :programmer do
9
- def refactor!(code)
10
- code.thrown_away!
11
- end
12
- end
13
- end
14
-
15
- class ContextConsumer
16
- include Narrative::Context::Teller
17
-
18
- attr_accessor :code
19
-
20
- def initialize(code)
21
- self.code = code
22
- end
23
-
24
- def consume
25
- with_context 'refactor', programmer: 'alice' do |programmer:|
26
- programmer.refactor! code
27
- end
28
- end
29
- end
30
-
31
- specify do
32
- code = double('code')
33
- allow(code).to receive(:thrown_away!)
34
-
35
- ContextConsumer.new(code).consume
36
-
37
- expect(code).to have_received(:thrown_away!).once
38
- end
39
- end
40
- end