consul 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of consul might be problematic. Click here for more details.

@@ -35,8 +35,20 @@ module Consul
35
35
  include?(*args) or raise Consul::Powerless.new("No power to #{args.inspect}")
36
36
  end
37
37
 
38
+ def for_record(*args)
39
+ adjective, record = Util.adjective_and_argument(*args)
40
+ for_model(adjective, record.class)
41
+ end
42
+
43
+ def for_model(*args)
44
+ adjective, model_class = Util.adjective_and_argument(*args)
45
+ collection_name = model_class.name.underscore.gsub('/', '_').pluralize
46
+ [adjective, collection_name].select(&:present?).join('_')
47
+ end
48
+
38
49
  private
39
50
 
51
+
40
52
  def boolean_or_nil?(value)
41
53
  [TrueClass, FalseClass, NilClass].include?(value.class)
42
54
  end
@@ -66,6 +78,24 @@ module Consul
66
78
  self.current = old_power
67
79
  end
68
80
 
81
+ def for_model(*args)
82
+ if current
83
+ current.for_model(*args)
84
+ else
85
+ adjective, model = Util.adjective_and_argument(*args)
86
+ model
87
+ end
88
+ end
89
+
90
+ def for_record(*args)
91
+ if current
92
+ current.for_record(*args)
93
+ else
94
+ adjective, record = Util.adjective_and_argument(*args)
95
+ record.class
96
+ end
97
+ end
98
+
69
99
  private
70
100
 
71
101
  def define_power(name, &block)
@@ -34,6 +34,17 @@ module Consul
34
34
  end
35
35
  end
36
36
 
37
+ def adjective_and_argument(*args)
38
+ if args.size > 1
39
+ adjective = args[0]
40
+ record = args[1]
41
+ else
42
+ adjective = nil
43
+ record = args[0]
44
+ end
45
+ [adjective, record]
46
+ end
47
+
37
48
  end
38
49
  end
39
50
 
@@ -1,3 +1,3 @@
1
1
  module Consul
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.0'
3
3
  end
@@ -0,0 +1,3 @@
1
+ class Deal
2
+
3
+ end
@@ -0,0 +1,3 @@
1
+ class Deal::Item
2
+
3
+ end
@@ -73,6 +73,15 @@ class Power
73
73
  Song.recent
74
74
  end
75
75
 
76
+ power :deals do
77
+ 'deals'
78
+ end
79
+
80
+ power :updatable_deals do
81
+ 'updatable_deals'
82
+ end
83
+
84
+
76
85
  private
77
86
 
78
87
  attr_accessor :user
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Consul::Util do
4
+
5
+ describe '.adjective_and_argument' do
6
+
7
+ it 'should return [nil, argument] if given a single argument' do
8
+ Consul::Util.adjective_and_argument(Deal).should == [nil, Deal]
9
+ end
10
+
11
+ it 'should return [adjective, argument] if given an adjective and an argument' do
12
+ Consul::Util.adjective_and_argument('updatable', Deal).should == ['updatable', Deal]
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -403,7 +403,7 @@ describe Consul::Power do
403
403
  spy.observe(Power.current)
404
404
  end
405
405
  Power.current.should == 'outer power'
406
- Power.current = nil # clean up for subsequent specs -- to bad we can't use .with_power :)
406
+ Power.current = nil # clean up for subsequent specs -- too bad we can't use .with_power :)
407
407
  end
408
408
 
409
409
  it 'should restore an existing power even if the block raises an error' do
@@ -417,6 +417,7 @@ describe Consul::Power do
417
417
  # do nothing
418
418
  end
419
419
  Power.current.should == 'outer power'
420
+ Power.current = nil # clean up for subsequent specs -- too bad we can't use .with_power :)
420
421
  end
421
422
 
422
423
  it 'should call instantiate a new Power if the given argument is not already a power' do
@@ -430,4 +431,92 @@ describe Consul::Power do
430
431
 
431
432
  end
432
433
 
434
+ describe '#for_model' do
435
+
436
+ it 'should return the power corresponding to the given model' do
437
+ @user.power.for_model(Deal).should == 'deals'
438
+ end
439
+
440
+ it 'should return the correct power for a namespaced model' do
441
+ @user.power.for_model(Deal::Item).should == 'deal_items'
442
+ end
443
+
444
+ it 'should allow to prefix the power with an adjective' do
445
+ @user.power.for_model(:updatable, Deal).should == 'updatable_deals'
446
+ end
447
+
448
+ end
449
+
450
+ describe '.for_model' do
451
+
452
+ context 'when Power.current is present' do
453
+
454
+ it 'should return the power corresponding to the given model' do
455
+ Power.with_power(Power.new) do
456
+ Power.for_model(Deal).should == 'deals'
457
+ end
458
+ end
459
+
460
+ it 'should allow to prefix the power with an adjective' do
461
+ Power.with_power(Power.new) do
462
+ Power.for_model(:updatable, Deal).should == 'updatable_deals'
463
+ end
464
+ end
465
+
466
+ end
467
+
468
+ context 'when Power.current is nil' do
469
+
470
+ it 'should return the given model' do
471
+ Power.for_model(Deal).should == Deal
472
+ end
473
+
474
+ it 'should return the given model even if the model was prefixed with an adjective' do
475
+ Power.for_model(:updatable, Deal).should == Deal
476
+ end
477
+
478
+ end
479
+
480
+ end
481
+
482
+ describe '#for_record' do
483
+
484
+ it 'should return the power corresponding to the class of the given record' do
485
+ @user.power.for_record(Deal.new).should == 'deals'
486
+ end
487
+
488
+ end
489
+
490
+ describe '.for_record' do
491
+
492
+ context 'when Power.current is present' do
493
+
494
+ it 'should return the power corresponding to the class of the given record' do
495
+ Power.with_power(Power.new) do
496
+ Power.for_record(Deal.new).should == 'deals'
497
+ end
498
+ end
499
+
500
+ it 'should allow to prefix the power with an adjective' do
501
+ Power.with_power(Power.new) do
502
+ Power.for_record(:updatable, Deal.new).should == 'updatable_deals'
503
+ end
504
+ end
505
+
506
+ end
507
+
508
+ context 'when Power.current is nil' do
509
+
510
+ it 'should return the given model' do
511
+ Power.for_record(Deal.new).should == Deal
512
+ end
513
+
514
+ it 'should return the given model even if the model was prefixed with an adjective' do
515
+ Power.for_record(:updatable, Deal.new).should == Deal
516
+ end
517
+
518
+ end
519
+
520
+ end
521
+
433
522
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consul
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 5
8
+ - 6
9
9
  - 0
10
- version: 0.5.0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Henning Koch
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-01-14 00:00:00 +01:00
18
+ date: 2013-01-15 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -130,6 +130,8 @@ files:
130
130
  - spec/shared/app_root/app/controllers/songs_controller.rb
131
131
  - spec/shared/app_root/app/controllers/users_controller.rb
132
132
  - spec/shared/app_root/app/models/client.rb
133
+ - spec/shared/app_root/app/models/deal.rb
134
+ - spec/shared/app_root/app/models/deal/item.rb
133
135
  - spec/shared/app_root/app/models/note.rb
134
136
  - spec/shared/app_root/app/models/power.rb
135
137
  - spec/shared/app_root/app/models/song.rb
@@ -144,6 +146,7 @@ files:
144
146
  - spec/shared/consul/controllers/risks_controller_spec.rb
145
147
  - spec/shared/consul/controllers/songs_controller_spec.rb
146
148
  - spec/shared/consul/controllers/users_controller_spec.rb
149
+ - spec/shared/consul/controllers/util_spec.rb
147
150
  - spec/shared/consul/power_spec.rb
148
151
  has_rdoc: true
149
152
  homepage: https://github.com/makandra/consul