blue_print 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +152 -5
- data/Rakefile +47 -1
- data/benchmark/pure.rb +68 -0
- data/benchmark/with_blue_print.rb +57 -0
- data/benchmark/with_extend.rb +42 -0
- data/blue_print.gemspec +3 -2
- data/lib/blue_print/behavior.rb +8 -1
- data/lib/blue_print/context.rb +17 -5
- data/lib/blue_print/environment.rb +2 -3
- data/lib/blue_print/integration/rspec.rb +4 -0
- data/lib/blue_print/version.rb +1 -1
- data/lib/generators/rspec/templates/context_spec.rb +2 -2
- data/profile/method_call.rb +39 -0
- data/spec/lib/blue_print/behavior_spec.rb +3 -5
- data/spec/lib/blue_print/context_spec.rb +18 -6
- data/spec/lib/blue_print/environment_spec.rb +5 -5
- metadata +24 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 206433cbd4ea20e4889db2b3925b82d6f133bd21
|
4
|
+
data.tar.gz: 37a77da96f4fd80e0646e084cc640c93530dbd60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a94144f0ef54288803e77e932a9dc42e4bbcf621af214a4cb3a7d52c8108119758efe47107603d656a56e3d8ce65d6977ec4e5273b1ffa86e9d420859aacc89
|
7
|
+
data.tar.gz: 4aa19df93eb03d52a7ad5f17a214310339ac9f74fa0da8d65e30a2fe4bb9fce1e2857285fb37468b8dda7bc84576800c232a367ffa8ee87be2aefcf9d87d3dec
|
data/README.md
CHANGED
@@ -1,23 +1,170 @@
|
|
1
1
|
# BluePrint
|
2
2
|
|
3
|
-
[![
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/blue_print.png)](http://badge.fury.io/rb/blue_print)
|
4
|
+
[![Build Status](https://travis-ci.org/magnet-inc/blue_print.png?branch=master)](https://travis-ci.org/magnet-inc/blue_print)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/magnet-inc/blue_print/badge.png)](https://coveralls.io/r/magnet-inc/blue_print)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/magnet-inc/blue_print.png)](https://codeclimate.com/github/magnet-inc/blue_print)
|
4
7
|
|
5
|
-
The behavior switching framework
|
8
|
+
The behavior switching framework. inspired by [DCI](http://en.wikipedia.org/wiki/Data,_context_and_interaction).
|
6
9
|
|
7
10
|
## Requirements
|
8
11
|
|
9
12
|
- __Ruby__ >= _2.0.0_
|
10
|
-
-
|
13
|
+
- Tested ruby version:
|
14
|
+
- 2.0.0
|
15
|
+
- 2.1.0
|
16
|
+
- 2.1.1
|
17
|
+
- head
|
11
18
|
|
12
19
|
## Installation
|
13
20
|
|
14
21
|
Add this line to your application's Gemfile:
|
15
22
|
|
16
|
-
|
23
|
+
```ruby
|
24
|
+
gem 'blue_print', '~> 1.2.0'
|
25
|
+
```
|
17
26
|
|
18
27
|
## Usage
|
19
28
|
|
20
|
-
|
29
|
+
### Integration
|
30
|
+
|
31
|
+
BluePrint provides some integrations. Now supported libraries:
|
32
|
+
|
33
|
+
- [Ruby on Rails](http://rubyonrails.org/) >= 3.1
|
34
|
+
- [Grape](http://intridea.github.io/grape/)
|
35
|
+
- [Draper](https://github.com/drapergem/draper)
|
36
|
+
- [RSpec](https://github.com/rspec)
|
37
|
+
|
38
|
+
### Rails Generators
|
39
|
+
|
40
|
+
BluePrint provides a generator to create templates of a context and behaviros.
|
41
|
+
|
42
|
+
```bash
|
43
|
+
$ rails generate blue_print staff user:staff_user
|
44
|
+
create app/blue_prints/staff_context.rb
|
45
|
+
create app/blue_prints/staff_context/staff_user.rb
|
46
|
+
invoke rspec
|
47
|
+
create spec/blue_prints/staff_context_spec.rb
|
48
|
+
create spec/blue_prints/staff_context/staff_user_spec.rb
|
49
|
+
```
|
50
|
+
|
51
|
+
### Contexts
|
52
|
+
|
53
|
+
Contexts inherit from `BluePrint::Context`, live in your `app/blue_prints` directory.
|
54
|
+
|
55
|
+
#### active_if
|
56
|
+
|
57
|
+
This block is used to decide if this context active or not. `env` is the `BluePrint.env`. `self` is passed via `BluePrint::Environment#context`. By default, this is set as `active_if { false }`.
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
class StaffContext < BluePrint::Context
|
61
|
+
active_if do |env|
|
62
|
+
current_user.try(:staff?)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
And you can define named active if:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
# lib/active_ifs/staff.rb
|
71
|
+
BluePrint::ActiveIf.new(:staff) do |env|
|
72
|
+
current_user.try(:staff?)
|
73
|
+
end
|
74
|
+
|
75
|
+
# app/blue_prints/staff_context.rb
|
76
|
+
class StaffContext < BluePrint::Context
|
77
|
+
active_if :staff
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
#### Casting
|
82
|
+
|
83
|
+
Contexts has casting. This is used to decide classes cast as behaviors if this context active.
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
class StaffContext < BluePrint::Context
|
87
|
+
cast ::User, as: [StaffUser]
|
88
|
+
end
|
89
|
+
|
90
|
+
StaffContext.casting
|
91
|
+
# =>
|
92
|
+
# {
|
93
|
+
# User => [ StaffUser ]
|
94
|
+
# }
|
95
|
+
```
|
96
|
+
|
97
|
+
### Behaviors
|
98
|
+
|
99
|
+
Behaviors extended by `BluePrint::Behavior`, live in your `app/blue_prints/{context}` directory.
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
# app/blue_prints/staff_context/staff_user.rb
|
103
|
+
module StaffContext::StaffUser
|
104
|
+
extend BluePrint::Behavior
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
108
|
+
#### Methods
|
109
|
+
|
110
|
+
Behaviors can have some methods.
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
# app/blue_prints/staff_context/staff_user.rb
|
114
|
+
module StaffContext::StaffUser
|
115
|
+
extend BluePrint::Behavior
|
116
|
+
|
117
|
+
def user_name
|
118
|
+
"staff"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# app/models/user.rb
|
123
|
+
class User < ActiveRecord::Base
|
124
|
+
def user_name
|
125
|
+
"#{name} san"
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
user = User.new(name: "Magnet")
|
130
|
+
user.user_name # => "Magnet san"
|
131
|
+
within_context_of(StaffContext) do
|
132
|
+
user.user_name # => "staff"
|
133
|
+
end
|
134
|
+
```
|
135
|
+
|
136
|
+
#### Class Methods
|
137
|
+
|
138
|
+
Behaviors can have some class methods.
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
# app/blue_prints/staff_context/staff_user.rb
|
142
|
+
module StaffContext::StaffUser
|
143
|
+
extend BluePrint::Behavior
|
144
|
+
|
145
|
+
module ClassMethods
|
146
|
+
def find(id)
|
147
|
+
where(id: id, staff: true).first!
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
# app/models/user.rb
|
153
|
+
class User < ActiveRecord::Base
|
154
|
+
end
|
155
|
+
|
156
|
+
user = User.create(staff: false)
|
157
|
+
User.find(user.id) == user # => true
|
158
|
+
within_context_of(StaffContext) do
|
159
|
+
User.find(user.id) # => raise ActiveRecord::NotFound
|
160
|
+
end
|
161
|
+
```
|
162
|
+
|
163
|
+
### Helper
|
164
|
+
|
165
|
+
`BluePrint::Helper` provides `#within_context_of` method.
|
166
|
+
|
167
|
+
This module includes automatically to some classes. see [lib/blue_print/integration](https://github.com/magnet-inc/blue_print/tree/master/lib/blue_print/integration).
|
21
168
|
|
22
169
|
## Contributing
|
23
170
|
|
data/Rakefile
CHANGED
@@ -1 +1,47 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
|
3
|
+
desc 'Take benchmark'
|
4
|
+
task :benchmark do
|
5
|
+
require File.expand_path('../benchmark/pure', __FILE__)
|
6
|
+
require File.expand_path('../benchmark/with_blue_print', __FILE__)
|
7
|
+
require File.expand_path('../benchmark/with_extend', __FILE__)
|
8
|
+
|
9
|
+
result = {
|
10
|
+
ruby: {
|
11
|
+
version: RUBY_VERSION,
|
12
|
+
platform: RUBY_PLATFORM
|
13
|
+
},
|
14
|
+
benchmark_iteration: BENCHMARK_ITERATION,
|
15
|
+
method_call_iteration: NUM_ITERATION,
|
16
|
+
benchmarks: {
|
17
|
+
pure: PURE_RESULT,
|
18
|
+
blue_print: BLUE_PRINT_RESULT,
|
19
|
+
extend: EXTEND_RESULT
|
20
|
+
}
|
21
|
+
}
|
22
|
+
|
23
|
+
result[:benchmarks].each_pair do |label, scores|
|
24
|
+
puts "#{label}:"
|
25
|
+
totals = scores.map(&:total)
|
26
|
+
avg = totals.inject(&:+).to_f / totals.size
|
27
|
+
puts " min: #{totals.min.round(3)} max: #{totals.max.round(3)} avg: #{avg.round(3)}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
namespace :profile do
|
32
|
+
PROFILES = Dir['profile/*.rb'].map {|f| File.basename(f, '.rb') }
|
33
|
+
|
34
|
+
PROFILES.each do |profile|
|
35
|
+
desc "take #{profile} profile"
|
36
|
+
task profile do
|
37
|
+
require File.expand_path("../profile/#{profile}", __FILE__)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
desc 'take all profiles'
|
42
|
+
task :all do
|
43
|
+
PROFILES.each do |profile|
|
44
|
+
Rake::Task["profile:#{profile}"].invoke
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/benchmark/pure.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'active_support/core_ext/integer/inflections'
|
2
|
+
require 'ruby-progressbar'
|
3
|
+
require 'benchmark'
|
4
|
+
|
5
|
+
LABEL_WIDTH = 10
|
6
|
+
BENCHMARK_ITERATION = 100
|
7
|
+
NUM_ITERATION = 100000
|
8
|
+
|
9
|
+
class Model
|
10
|
+
def self.active?
|
11
|
+
@active
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.activate!
|
15
|
+
@active = true
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.deactivate!
|
19
|
+
@active = false
|
20
|
+
end
|
21
|
+
|
22
|
+
def name
|
23
|
+
if self.class.active?
|
24
|
+
:pure
|
25
|
+
else
|
26
|
+
:pure
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class NoEffect
|
32
|
+
def name
|
33
|
+
:no_effect
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
model = Model.new
|
38
|
+
|
39
|
+
PURE_RESULT = []
|
40
|
+
progress = ProgressBar.create(
|
41
|
+
title: model.name.to_s.ljust(LABEL_WIDTH),
|
42
|
+
total: BENCHMARK_ITERATION
|
43
|
+
)
|
44
|
+
GC.start
|
45
|
+
BENCHMARK_ITERATION.times do |n|
|
46
|
+
n += 1
|
47
|
+
|
48
|
+
PURE_RESULT.push(
|
49
|
+
Benchmark.measure("#{n}#{n.ordinal}") do
|
50
|
+
Model.activate!
|
51
|
+
|
52
|
+
NUM_ITERATION.times do
|
53
|
+
Model.new.name
|
54
|
+
NoEffect.new.name
|
55
|
+
end
|
56
|
+
|
57
|
+
Model.deactivate!
|
58
|
+
|
59
|
+
NUM_ITERATION.times do
|
60
|
+
Model.new.name
|
61
|
+
NoEffect.new.name
|
62
|
+
end
|
63
|
+
end
|
64
|
+
)
|
65
|
+
|
66
|
+
progress.increment
|
67
|
+
end
|
68
|
+
progress.finish
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'blue_print'
|
2
|
+
require File.expand_path('../pure', __FILE__)
|
3
|
+
|
4
|
+
BluePrint.env = BluePrint::Environment.new(nil)
|
5
|
+
|
6
|
+
class Model
|
7
|
+
def name
|
8
|
+
:pure
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class BenchmarkContext < BluePrint::Context
|
13
|
+
activate!
|
14
|
+
|
15
|
+
module BenchmarkModel
|
16
|
+
extend BluePrint::Behavior
|
17
|
+
|
18
|
+
def name
|
19
|
+
:blue_print
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
cast Model, as: [BenchmarkModel]
|
24
|
+
end
|
25
|
+
|
26
|
+
model = Model.new
|
27
|
+
|
28
|
+
BLUE_PRINT_RESULT = []
|
29
|
+
progress = ProgressBar.create(
|
30
|
+
title: model.name.to_s.ljust(LABEL_WIDTH),
|
31
|
+
total: BENCHMARK_ITERATION
|
32
|
+
)
|
33
|
+
GC.start
|
34
|
+
BENCHMARK_ITERATION.times do |n|
|
35
|
+
n += 1
|
36
|
+
|
37
|
+
BLUE_PRINT_RESULT.push(
|
38
|
+
Benchmark.measure("#{n}#{n.ordinal}") do
|
39
|
+
BenchmarkContext.activate!
|
40
|
+
|
41
|
+
NUM_ITERATION.times do
|
42
|
+
Model.new.name
|
43
|
+
NoEffect.new.name
|
44
|
+
end
|
45
|
+
|
46
|
+
BenchmarkContext.deactivate!
|
47
|
+
|
48
|
+
NUM_ITERATION.times do
|
49
|
+
Model.new.name
|
50
|
+
NoEffect.new.name
|
51
|
+
end
|
52
|
+
end
|
53
|
+
)
|
54
|
+
|
55
|
+
progress.increment
|
56
|
+
end
|
57
|
+
progress.finish
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.expand_path('../pure', __FILE__)
|
2
|
+
|
3
|
+
class Model
|
4
|
+
def name
|
5
|
+
:pure
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
module ExtendedUser
|
10
|
+
def name
|
11
|
+
:extended
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
model = Model.new.extend(ExtendedUser)
|
16
|
+
|
17
|
+
EXTEND_RESULT = []
|
18
|
+
progress = ProgressBar.create(
|
19
|
+
title: model.name.to_s.ljust(LABEL_WIDTH),
|
20
|
+
total: BENCHMARK_ITERATION
|
21
|
+
)
|
22
|
+
GC.start
|
23
|
+
BENCHMARK_ITERATION.times do |n|
|
24
|
+
n += 1
|
25
|
+
|
26
|
+
EXTEND_RESULT.push(
|
27
|
+
Benchmark.measure("#{n}#{n.ordinal}") do
|
28
|
+
NUM_ITERATION.times do
|
29
|
+
Model.new.extend(ExtendedUser).name
|
30
|
+
NoEffect.new.name
|
31
|
+
end
|
32
|
+
|
33
|
+
NUM_ITERATION.times do
|
34
|
+
Model.new.name
|
35
|
+
NoEffect.new.name
|
36
|
+
end
|
37
|
+
end
|
38
|
+
)
|
39
|
+
|
40
|
+
progress.increment
|
41
|
+
end
|
42
|
+
progress.finish
|
data/blue_print.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = BluePrint::VERSION
|
9
9
|
spec.authors = ['Sho Kusano']
|
10
10
|
spec.email = ['rosylilly@aduca.org']
|
11
|
-
spec.summary = %q{The behavior switching framework
|
11
|
+
spec.summary = %q{The behavior switching framework}
|
12
12
|
spec.homepage = 'http://magnet-inc.github.io/blue_print'
|
13
13
|
spec.license = 'MIT'
|
14
14
|
|
@@ -30,8 +30,9 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.add_development_dependency 'coveralls'
|
31
31
|
spec.add_development_dependency 'draper'
|
32
32
|
spec.add_development_dependency 'grape'
|
33
|
+
spec.add_development_dependency 'ruby-progressbar'
|
34
|
+
spec.add_development_dependency 'ruby-prof'
|
33
35
|
|
34
36
|
spec.add_dependency 'activesupport'
|
35
|
-
spec.add_dependency 'hashie'
|
36
37
|
spec.add_dependency 'thread-parent'
|
37
38
|
end
|
data/lib/blue_print/behavior.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_support/core_ext/object/try'
|
2
|
+
require 'active_support/core_ext/string/inflections'
|
1
3
|
require 'blue_print'
|
2
4
|
require 'blue_print/helper'
|
3
5
|
|
@@ -47,11 +49,14 @@ module BluePrint::Behavior
|
|
47
49
|
|
48
50
|
def define_safe_method(target, punctuation, method_name)
|
49
51
|
alias_method("#{target}_with_#{behavior_name}#{punctuation}", method_name)
|
52
|
+
remove_method(method_name)
|
50
53
|
|
51
54
|
module_eval <<-EOC
|
52
55
|
def #{method_name}(*args)
|
53
|
-
|
56
|
+
if #{context}.active?
|
54
57
|
#{target}_with_#{behavior_name}#{punctuation}(*args)
|
58
|
+
else
|
59
|
+
super(*args)
|
55
60
|
end
|
56
61
|
end
|
57
62
|
EOC
|
@@ -67,6 +72,8 @@ module BluePrint::Behavior
|
|
67
72
|
punctuation = Regexp.last_match ? Regexp.last_match[1] : ''
|
68
73
|
|
69
74
|
define_safe_method(aliased_target, punctuation, method_name)
|
75
|
+
|
76
|
+
context.try(:reaction!)
|
70
77
|
@ignore_added_hook = false
|
71
78
|
end
|
72
79
|
end
|
data/lib/blue_print/context.rb
CHANGED
@@ -27,20 +27,25 @@ class BluePrint::Context
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def self.active?
|
30
|
-
|
31
|
-
|
32
|
-
action!
|
33
|
-
|
34
|
-
BluePrint.env[context_name] =
|
30
|
+
BluePrint.env.fetch(context_name) do
|
35
31
|
!!active_ifs.inject(active_ifs.first.try(:active?)) do |memo, active_if|
|
36
32
|
memo && active_if.active?
|
37
33
|
end
|
34
|
+
end
|
38
35
|
end
|
39
36
|
|
40
37
|
def self.deactive?
|
41
38
|
!active?
|
42
39
|
end
|
43
40
|
|
41
|
+
def self.activate!
|
42
|
+
BluePrint.env[context_name] = true
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.deactivate!
|
46
|
+
BluePrint.env[context_name] = false
|
47
|
+
end
|
48
|
+
|
44
49
|
def self.casting
|
45
50
|
@casting ||= Hash.new { |casting, klass| casting[klass] = [] }
|
46
51
|
end
|
@@ -49,6 +54,8 @@ class BluePrint::Context
|
|
49
54
|
as = [as] unless as.kind_of?(Array)
|
50
55
|
as.map! { |role| role.is_a?(Module) ? role : role.to_s.safe_constantize }
|
51
56
|
casting[actor] = casting[actor] | as
|
57
|
+
|
58
|
+
reaction!
|
52
59
|
end
|
53
60
|
|
54
61
|
def self.action!
|
@@ -62,4 +69,9 @@ class BluePrint::Context
|
|
62
69
|
|
63
70
|
@acted = true
|
64
71
|
end
|
72
|
+
|
73
|
+
def self.reaction!
|
74
|
+
@acted = false
|
75
|
+
action!
|
76
|
+
end
|
65
77
|
end
|
data/lib/blue_print/version.rb
CHANGED
@@ -2,10 +2,10 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe <%= class_name %>Context do
|
4
4
|
context 'in active' do
|
5
|
-
before { described_class.
|
5
|
+
before { described_class.activate! }
|
6
6
|
end
|
7
7
|
|
8
8
|
context 'in deactive' do
|
9
|
-
before { described_class.
|
9
|
+
before { described_class.deactivate! }
|
10
10
|
end
|
11
11
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'blue_print'
|
2
|
+
require 'ruby-prof'
|
3
|
+
|
4
|
+
BluePrint.env = BluePrint::Environment.new({})
|
5
|
+
|
6
|
+
class Base
|
7
|
+
def name
|
8
|
+
:base
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class ProfileContext < BluePrint::Context
|
13
|
+
module Extended
|
14
|
+
def name
|
15
|
+
:extended
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
cast Base, as: Extended
|
20
|
+
end
|
21
|
+
|
22
|
+
ITERATION = 100000
|
23
|
+
|
24
|
+
RubyProf.start
|
25
|
+
|
26
|
+
ProfileContext.activate!
|
27
|
+
ITERATION.times do
|
28
|
+
Base.new.name
|
29
|
+
end
|
30
|
+
|
31
|
+
ProfileContext.deactivate!
|
32
|
+
ITERATION.times do
|
33
|
+
Base.new.name
|
34
|
+
end
|
35
|
+
|
36
|
+
result = RubyProf.stop
|
37
|
+
|
38
|
+
printer = RubyProf::FlatPrinter.new(result)
|
39
|
+
printer.print(STDOUT)
|
@@ -1,10 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
def self.active?
|
5
|
-
true
|
6
|
-
end
|
7
|
-
|
3
|
+
class TestContext < BluePrint::Context
|
8
4
|
module TestBehavior
|
9
5
|
extend BluePrint::Behavior
|
10
6
|
|
@@ -60,6 +56,8 @@ describe BluePrint::Behavior do
|
|
60
56
|
end
|
61
57
|
let(:target) { target_class.new }
|
62
58
|
|
59
|
+
before { TestContext.activate! }
|
60
|
+
|
63
61
|
shared_examples 'as behavior' do
|
64
62
|
describe '#context_name' do
|
65
63
|
subject { behavior.context_name }
|
@@ -52,10 +52,6 @@ describe BluePrint::Context do
|
|
52
52
|
|
53
53
|
subject { context.active? }
|
54
54
|
|
55
|
-
before do
|
56
|
-
BluePrint.env = BluePrint::Environment.new(self)
|
57
|
-
end
|
58
|
-
|
59
55
|
context 'with always actives' do
|
60
56
|
before do
|
61
57
|
context.active_if(always_active, always_active)
|
@@ -103,6 +99,24 @@ describe BluePrint::Context do
|
|
103
99
|
end
|
104
100
|
end
|
105
101
|
|
102
|
+
describe '#activate!' do
|
103
|
+
before do
|
104
|
+
context.active_if { false }
|
105
|
+
context.activate!
|
106
|
+
end
|
107
|
+
|
108
|
+
it { should be_active }
|
109
|
+
end
|
110
|
+
|
111
|
+
describe '#deactivate!' do
|
112
|
+
before do
|
113
|
+
context.active_if { true }
|
114
|
+
context.deactivate!
|
115
|
+
end
|
116
|
+
|
117
|
+
it { should be_deactive }
|
118
|
+
end
|
119
|
+
|
106
120
|
describe '#cast' do
|
107
121
|
let(:klass) { Class.new }
|
108
122
|
let(:role) { Module.new }
|
@@ -124,8 +138,6 @@ describe BluePrint::Context do
|
|
124
138
|
|
125
139
|
before do
|
126
140
|
context.cast(klass, as: role)
|
127
|
-
context.instance_variable_set(:@acted, false)
|
128
|
-
context.action!
|
129
141
|
end
|
130
142
|
|
131
143
|
it 'be act role' do
|
@@ -4,13 +4,13 @@ describe BluePrint::Environment do
|
|
4
4
|
let(:context) { double(a: 1) }
|
5
5
|
subject(:env) { described_class.new(context) }
|
6
6
|
|
7
|
-
describe '#
|
7
|
+
describe '#within' do
|
8
8
|
it 'keeps scope' do
|
9
9
|
b = 2
|
10
|
-
env
|
11
|
-
env.
|
12
|
-
env
|
13
|
-
env
|
10
|
+
env[:rspec] = self
|
11
|
+
env.within do |env|
|
12
|
+
env[:rspec].expect(a).to env[:rspec].eq(1)
|
13
|
+
env[:rspec].expect(b).to env[:rspec].eq(2)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: blue_print
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sho Kusano
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -193,13 +193,13 @@ dependencies:
|
|
193
193
|
- !ruby/object:Gem::Version
|
194
194
|
version: '0'
|
195
195
|
- !ruby/object:Gem::Dependency
|
196
|
-
name:
|
196
|
+
name: ruby-progressbar
|
197
197
|
requirement: !ruby/object:Gem::Requirement
|
198
198
|
requirements:
|
199
199
|
- - ">="
|
200
200
|
- !ruby/object:Gem::Version
|
201
201
|
version: '0'
|
202
|
-
type: :
|
202
|
+
type: :development
|
203
203
|
prerelease: false
|
204
204
|
version_requirements: !ruby/object:Gem::Requirement
|
205
205
|
requirements:
|
@@ -207,7 +207,21 @@ dependencies:
|
|
207
207
|
- !ruby/object:Gem::Version
|
208
208
|
version: '0'
|
209
209
|
- !ruby/object:Gem::Dependency
|
210
|
-
name:
|
210
|
+
name: ruby-prof
|
211
|
+
requirement: !ruby/object:Gem::Requirement
|
212
|
+
requirements:
|
213
|
+
- - ">="
|
214
|
+
- !ruby/object:Gem::Version
|
215
|
+
version: '0'
|
216
|
+
type: :development
|
217
|
+
prerelease: false
|
218
|
+
version_requirements: !ruby/object:Gem::Requirement
|
219
|
+
requirements:
|
220
|
+
- - ">="
|
221
|
+
- !ruby/object:Gem::Version
|
222
|
+
version: '0'
|
223
|
+
- !ruby/object:Gem::Dependency
|
224
|
+
name: activesupport
|
211
225
|
requirement: !ruby/object:Gem::Requirement
|
212
226
|
requirements:
|
213
227
|
- - ">="
|
@@ -248,6 +262,9 @@ files:
|
|
248
262
|
- LICENSE.txt
|
249
263
|
- README.md
|
250
264
|
- Rakefile
|
265
|
+
- benchmark/pure.rb
|
266
|
+
- benchmark/with_blue_print.rb
|
267
|
+
- benchmark/with_extend.rb
|
251
268
|
- blue_print.gemspec
|
252
269
|
- gemfiles/rails-3.x.gemfile
|
253
270
|
- gemfiles/rails-4.x.gemfile
|
@@ -278,6 +295,7 @@ files:
|
|
278
295
|
- lib/generators/rspec/blue_print_generator.rb
|
279
296
|
- lib/generators/rspec/templates/context_spec.rb
|
280
297
|
- lib/generators/rspec/templates/role_spec.rb
|
298
|
+
- profile/method_call.rb
|
281
299
|
- spec/apis/application_api_spec.rb
|
282
300
|
- spec/dummy/.gitignore
|
283
301
|
- spec/dummy/Gemfile
|
@@ -359,7 +377,7 @@ rubyforge_project:
|
|
359
377
|
rubygems_version: 2.2.2
|
360
378
|
signing_key:
|
361
379
|
specification_version: 4
|
362
|
-
summary: The behavior switching framework
|
380
|
+
summary: The behavior switching framework
|
363
381
|
test_files:
|
364
382
|
- spec/apis/application_api_spec.rb
|
365
383
|
- spec/dummy/.gitignore
|