cantango-config 0.1.9.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. data/README.mdown +16 -0
  2. data/VERSION +1 -1
  3. data/cantango-config.gemspec +68 -62
  4. data/lib/cantango/category.rb +18 -0
  5. data/lib/cantango/class_methods.rb +10 -0
  6. data/lib/cantango/config.rb +43 -2
  7. data/lib/cantango/{configuration → config}/ability.rb +8 -3
  8. data/lib/cantango/config/ability/engine.rb +45 -0
  9. data/lib/cantango/config/ability/modes.rb +16 -0
  10. data/lib/cantango/config/ability/modes/execution.rb +19 -0
  11. data/lib/cantango/{configuration → config}/account.rb +1 -1
  12. data/lib/cantango/{configuration → config}/accounts.rb +2 -3
  13. data/lib/cantango/{configuration → config}/adapters.rb +4 -4
  14. data/lib/cantango/{configuration → config}/autoload.rb +1 -1
  15. data/lib/cantango/{configuration → config}/categories.rb +3 -4
  16. data/lib/cantango/config/class_methods.rb +27 -0
  17. data/lib/cantango/{configuration → config}/debug.rb +1 -1
  18. data/lib/cantango/config/engines.rb +47 -0
  19. data/lib/cantango/config/engines/available.rb +19 -0
  20. data/lib/cantango/config/engines/order.rb +48 -0
  21. data/lib/cantango/{configuration → config}/factory.rb +1 -1
  22. data/lib/cantango/{configuration → config}/guest.rb +5 -6
  23. data/lib/cantango/config/helpers.rb +17 -0
  24. data/lib/cantango/{configuration → config}/hooks.rb +3 -3
  25. data/lib/cantango/{configuration → config}/localhosts.rb +4 -5
  26. data/lib/cantango/{configuration → config}/models.rb +16 -7
  27. data/lib/cantango/{configuration → config}/models/actions.rb +0 -0
  28. data/lib/cantango/{configuration → config}/models/active_record.rb +0 -0
  29. data/lib/cantango/{configuration → config}/models/data_mapper.rb +0 -0
  30. data/lib/cantango/{configuration → config}/models/generic.rb +0 -0
  31. data/lib/cantango/{configuration → config}/models/mongo.rb +0 -0
  32. data/lib/cantango/{configuration → config}/models/mongo_mapper.rb +0 -0
  33. data/lib/cantango/{configuration → config}/models/mongoid.rb +0 -0
  34. data/lib/cantango/{configuration → config}/orms.rb +2 -2
  35. data/lib/cantango/{configuration → config}/user.rb +1 -1
  36. data/lib/cantango/{configuration → config}/users.rb +2 -2
  37. data/lib/cantango/registry.rb +5 -0
  38. data/lib/cantango/{configuration/registry → registry}/base.rb +18 -4
  39. data/lib/cantango/registry/clazz.rb +92 -0
  40. data/lib/cantango/{configuration/registry → registry}/hash.rb +8 -10
  41. data/spec/cantango/category_spec.rb +21 -0
  42. data/spec/cantango/config/ability/modes/execution_spec.rb +33 -0
  43. data/spec/cantango/{configuration → config/ability}/modes_spec.rb +3 -3
  44. data/spec/cantango/{configuration → config}/ability_spec.rb +3 -3
  45. data/spec/cantango/{configuration → config}/account_spec.rb +1 -1
  46. data/spec/cantango/{configuration → config}/accounts_spec.rb +3 -3
  47. data/spec/cantango/{configuration → config}/adapters_spec.rb +1 -1
  48. data/spec/cantango/{configuration → config}/autoload_spec.rb +1 -2
  49. data/spec/cantango/{configuration → config}/categories_spec.rb +2 -10
  50. data/spec/cantango/config/class_methods_spec.rb +12 -0
  51. data/spec/cantango/{configuration → config}/debug_spec.rb +2 -7
  52. data/spec/cantango/config/engines/available_spec.rb +29 -0
  53. data/spec/cantango/{configuration → config}/engines/engine_shared.rb +0 -0
  54. data/spec/cantango/{configuration/engines_spec.rb → config/engines/order_spec.rb} +9 -54
  55. data/spec/cantango/config/engines_spec.rb +59 -0
  56. data/spec/cantango/{configuration → config}/factory_spec.rb +3 -3
  57. data/spec/cantango/{configuration → config}/guest/find_guest_default_way_spec.rb +1 -1
  58. data/spec/cantango/{configuration → config}/guest_spec.rb +2 -2
  59. data/spec/cantango/config/helpers_spec.rb +15 -0
  60. data/spec/cantango/config/localhosts_spec.rb +8 -0
  61. data/spec/cantango/{configuration → config}/models_spec.rb +10 -3
  62. data/spec/cantango/{configuration → config}/orms_spec.rb +2 -2
  63. data/spec/cantango/{configuration → config}/shared/factory_ex.rb +0 -0
  64. data/spec/cantango/{configuration → config}/user_spec.rb +2 -2
  65. data/spec/cantango/{configuration → config}/users_spec.rb +4 -4
  66. data/spec/cantango/config_spec.rb +86 -5
  67. data/spec/cantango/{configuration/registry → registry}/base_spec.rb +2 -2
  68. data/spec/cantango/{configuration/registry/candidate_spec.rb → registry/clazz_spec.rb} +2 -2
  69. data/spec/cantango/{configuration/registry → registry}/hash_spec.rb +2 -2
  70. data/spec/cantango/{configuration/shared/registry → registry/shared}/base_ex.rb +0 -0
  71. data/spec/cantango/{configuration/shared/registry → registry/shared}/clazz_ex.rb +0 -0
  72. data/spec/cantango/{configuration/shared/registry → registry/shared}/hash_ex.rb +0 -0
  73. metadata +87 -81
  74. data/lib/cantango/configuration.rb +0 -110
  75. data/lib/cantango/configuration/category.rb +0 -20
  76. data/lib/cantango/configuration/engine.rb +0 -33
  77. data/lib/cantango/configuration/engines.rb +0 -95
  78. data/lib/cantango/configuration/execution_modes.rb +0 -21
  79. data/lib/cantango/configuration/modes.rb +0 -8
  80. data/lib/cantango/configuration/registry.rb +0 -7
  81. data/lib/cantango/configuration/registry/clazz.rb +0 -94
  82. data/spec/cantango/configuration/execution_modes_spec.rb +0 -15
  83. data/spec/cantango/configuration/localhosts_spec.rb +0 -9
  84. data/spec/cantango/configuration/shared/execution_modes_ex.rb +0 -14
  85. data/spec/cantango/configuration_spec.rb +0 -112
@@ -1,110 +0,0 @@
1
- require 'set'
2
- require 'singleton'
3
-
4
- require 'cantango/configuration'
5
-
6
- module CanTango
7
- # The main entry point to configure CanTango
8
- class << self
9
- def configure &block
10
- conf = CanTango::Configuration.instance
11
- yield conf if block
12
- conf
13
- end
14
-
15
- alias_method :config, :configure
16
- end
17
-
18
- class Configuration
19
- autoload_modules :Models, :Registry, :Engines, :Factory, :ExecutionModes, :Modes, :Ability
20
- autoload_modules :Categories, :Category
21
- autoload_modules :Autoload, :Adapters, :Debug, :Orms, :Localhosts, :Hooks
22
- autoload_modules :Account, :Accounts
23
- autoload_modules :User, :Guest, :Users
24
-
25
- include Singleton
26
-
27
- def ability
28
- @ability ||= conf::Ability.instance
29
- @ability.default_class ||= CanTango::Ability::Executor::Modal # from cantango-core
30
- @ability
31
- end
32
-
33
- def self.components
34
- [
35
- :guest, :autoload, :user, :account, :models, :modes, :roles, :role_groups,
36
- :engines, :users, :accounts, :categories, :adapters, :debug,
37
- :localhosts, :orms, :hooks
38
- ]
39
- end
40
-
41
- components.each do |conf_module|
42
- class_eval %{
43
- def #{conf_module} &block
44
- conf = conf::#{conf_module.to_s.camelize}.instance
45
- yield conf if block
46
- conf
47
- end
48
- }
49
- end
50
-
51
- def debug!
52
- debug.set :on
53
- end
54
-
55
- # Turn on default engines and enable compile adapter
56
- # i.e compilation of rules via sourcify
57
- def enable_defaults!
58
- engines.all :off
59
- adapters.use :compiler
60
- end
61
-
62
- def enable_helpers *names
63
- names = names.to_symbols
64
- enable_rest_helper if names.include? :rest
65
- end
66
-
67
- def enable_rest_helper
68
- raise 'ApplicationController not defined' if !defined?(::ApplicationController)
69
- ::ApplicationController.send :include, CanTango::Rails::Helpers::RestHelper
70
- end
71
-
72
- def clear!
73
- CanTango::Configuration.components.each do |component|
74
- if respond_to? component
75
- component = send(component)
76
- component.send(:clear!) if component.respond_to? :clear!
77
- end
78
- end
79
- engines.clear!
80
- end
81
-
82
- def include_models *names
83
- names = names.select_symbols
84
- if names.include? :default_guest_user
85
- require 'cantango/user/guest'
86
- end
87
- end
88
-
89
- # allow either block or direct access
90
- # engine(:permission) do |permission|
91
- # engine(:permission).config_path
92
- def engine name, &block
93
- engine = find_engine(name)
94
- yield engine if block
95
- engine
96
- end
97
-
98
- protected
99
-
100
- def find_engine name
101
- raise ArgumentError, "Must be label for an engine" if !name.kind_of_label?
102
- name = name.to_s.singularize
103
- engines.send(name) if engines.available? name
104
- end
105
-
106
- def conf
107
- CanTango::Configuration
108
- end
109
- end
110
- end
@@ -1,20 +0,0 @@
1
- module CanTango
2
- class Configuration
3
- class Category
4
- def initialize *subjects
5
- @subjects = subjects.flatten
6
- end
7
-
8
- def subjects
9
- @subjects ||= []
10
- end
11
-
12
- # test if a particular category has a certain subject
13
- def has_any? subject, &block
14
- found = subjects.include? subject
15
- yield if found && block
16
- found
17
- end
18
- end
19
- end
20
- end
@@ -1,33 +0,0 @@
1
- require 'singleton'
2
-
3
- module CanTango
4
- class Configuration
5
- class Engine
6
- include Singleton
7
- include CanTango::Configuration::ExecutionModes
8
-
9
- def set state = :on
10
- raise ArgumentError, "Must be :on or :off" unless !state || [:on, :off].include?(state)
11
- @state = state || :on
12
- end
13
-
14
- def reset!
15
- @state = nil
16
- end
17
-
18
- def on?
19
- @state == :on
20
- end
21
-
22
- def off?
23
- !on?
24
- end
25
-
26
- protected
27
-
28
- def ns
29
- CanTango::Configuration::Engines
30
- end
31
- end
32
- end
33
- end
@@ -1,95 +0,0 @@
1
- module CanTango
2
- class Configuration
3
- class Engines < Registry::Hash
4
- include Singleton
5
- include Enumerable
6
-
7
- # engine registry is a simple hash
8
- def register hash
9
- hash.each_pair do |name, engine_class|
10
- raise "Class must implement the CanTango Engine API. You can start by sublclassing CanTango::Engine" if !engine? engine_class
11
- raise "Name of engine must be a String or Symbol" if !name.kind_of_label?
12
- registered[name.to_s] = engine_class
13
- end
14
- end
15
-
16
- # defines the order of execution of engine in ability
17
- def set_execution_order *names
18
- @execution_order = names.flatten.uniq.map(&:to_s).select {|name| available? name }
19
- end
20
-
21
- def dont_execute name
22
- execution_order.delete(name.to_s)
23
- end
24
-
25
- def execute_first name
26
- dont_execute name
27
- execution_order.insert(0, name.to_s)
28
- end
29
-
30
- def execute_last name
31
- dont_execute name
32
- execution_order.insert(-1, name.to_s)
33
- end
34
-
35
- def execute_before existing, name
36
- dont_execute name
37
- index = execution_order.index(existing.to_s) || 0
38
- execution_order.insert(index, name.to_s)
39
- execution_order.compact!
40
- end
41
-
42
- def execute_after existing, name
43
- dont_execute name
44
- index = execution_order.index(existing.to_s)
45
- index ? execution_order.insert(index +1, name.to_s) : execute_last(name)
46
- execution_order.compact!
47
- end
48
-
49
- def execution_order
50
- @execution_order ||= (available - [:cache])
51
- end
52
-
53
- def available
54
- registered_names
55
- end
56
-
57
- def available? name
58
- available.include? name.to_s
59
- end
60
-
61
- def all state
62
- available.each {|engine| send(engine).set state }
63
- end
64
-
65
- def any? state
66
- available.any? {|engine| send(engine).send(:"#{state}?") if respond_to?(engine) }
67
- end
68
-
69
- def clear!
70
- each {|engine| engine.reset! }
71
- @registered = nil
72
- @execution_order = nil
73
- end
74
-
75
- def each
76
- available.each {|engine| yield send(engine) if respond_to?(engine) }
77
- end
78
-
79
- def active? name
80
- active.include? name.to_sym
81
- end
82
-
83
- def active
84
- available.select {|engine| send(engine).on? if respond_to?(engine) }
85
- end
86
-
87
- protected
88
-
89
- # does it implement the basic Engine API?
90
- def engine? engine_class
91
- [:execute!, :ability].all? {|meth| engine_class.instance_methods.include? meth }
92
- end
93
- end
94
- end
95
- end
@@ -1,21 +0,0 @@
1
- module CanTango
2
- class Configuration
3
- module ExecutionModes
4
- def modes
5
- @modes ||= [:no_cache]
6
- end
7
-
8
- def mode= mode_name
9
- mode_name = mode_name.to_sym
10
- raise ArgumentError, "Not a valid mode name" if !valid_mode_names.include? mode_name
11
- @modes = (mode_name == :both) ? [:cache, :no_cache] : [mode_name]
12
- end
13
-
14
- private
15
-
16
- def valid_mode_names
17
- [:cache, :no_cache]
18
- end
19
- end
20
- end
21
- end
@@ -1,8 +0,0 @@
1
- module CanTango
2
- class Configuration
3
- class Modes < Registry::Clazz
4
- include Singleton
5
- end
6
- end
7
- end
8
-
@@ -1,7 +0,0 @@
1
- module CanTango
2
- class Configuration
3
- module Registry
4
- autoload_modules :Base, :Hash, :Clazz
5
- end
6
- end
7
- end
@@ -1,94 +0,0 @@
1
- require 'singleton'
2
- require 'sugar-high/kind_of'
3
-
4
- module CanTango
5
- class Configuration
6
- module Registry
7
- class Clazz
8
- def register label, value
9
- raise "first arg must be a label, was: #{label}" if !label.kind_of_label?
10
- raise "second arg must be a valid Class, was: #{value}" if !valid? value
11
- name_registry.register label.to_sym
12
- class_registry.register value
13
- end
14
- alias_method :[]=, :register
15
-
16
- def << hash
17
- raise "Must be a hash" if !hash.is_a?(::Hash)
18
- hash.each_pair do |key, value|
19
- register key, value
20
- end
21
- end
22
-
23
- def registered
24
- name_registry.registered
25
- end
26
- alias_method :registered_names, :registered
27
-
28
- def clean!
29
- name_registry.clean!
30
- class_registry.clean!
31
- end
32
- alias_method :clear!, :clean!
33
-
34
- def registered_classes
35
- class_registry.registered
36
- end
37
-
38
- def registered? name
39
- name_registry.registered? name
40
- end
41
-
42
- def registered_class? name
43
- class_registry.registered? name
44
- end
45
-
46
- def name_registry
47
- @name_registry ||=NameRegistry.new
48
- end
49
-
50
- def class_registry
51
- @class_registry ||= ClassRegistry.new
52
- end
53
-
54
- def value_methods
55
- class_registry.value_methods
56
- end
57
-
58
- def value_types
59
- class_registry.types
60
- end
61
-
62
- class NameRegistry < Registry::Base
63
- end
64
-
65
- class ClassRegistry < Registry::Base
66
-
67
- def types
68
- [Class]
69
- end
70
-
71
- def value_methods
72
- []
73
- end
74
- end
75
-
76
- protected
77
-
78
- def valid? value
79
- valid_by_type?(value) && valid_by_methods?(value)
80
- end
81
-
82
- def valid_by_type? value
83
- return true if value_types.blank?
84
- value.any_kind_of?(*value_types)
85
- end
86
-
87
- def valid_by_methods? value
88
- return true if value_methods.blank?
89
- value_methods.all?{|m| value.respond_to(m)}
90
- end
91
- end
92
- end
93
- end
94
- end
@@ -1,15 +0,0 @@
1
- require 'spec_helper'
2
- require 'cantango/configuration/shared/execution_modes_ex'
3
-
4
- class User
5
- end
6
-
7
- class MyAbility
8
- include CanTango::Configuration::ExecutionModes
9
- end
10
-
11
- describe CanTango::Configuration::ExecutionModes do
12
- subject { MyAbility.new }
13
-
14
- it_should_behave_like "Execution Modes"
15
- end
@@ -1,9 +0,0 @@
1
- require 'spec_helper'
2
- require 'cantango/configuration/shared/registry/base_ex'
3
-
4
- describe CanTango::Configuration::Localhosts do
5
- subject { CanTango.config.localhosts }
6
-
7
- it_should_behave_like "Registry" do
8
- end
9
- end
@@ -1,14 +0,0 @@
1
- shared_examples_for 'Execution Modes' do
2
- describe 'valid mode=' do
3
- before do
4
- subject.mode = :cache
5
- end
6
- specify { subject.modes.should == [:cache] }
7
- end
8
-
9
- describe 'invalid mode=' do
10
- specify do
11
- lambda { subject.mode = :cachy! }.should raise_error
12
- end
13
- end
14
- end
@@ -1,112 +0,0 @@
1
- require 'spec_helper'
2
- require 'fixtures/models'
3
-
4
- @user = User.new('kris', 'kris@gmail.com')
5
-
6
- class CanTango::CustomAbility < CanTango::Ability::Base
7
- def initialize candidate, options = {}
8
- 'custom'
9
- end
10
- end
11
-
12
- class ApplicationController
13
- end
14
-
15
- describe CanTango::Configuration do
16
- subject { CanTango.config }
17
-
18
- describe 'ability' do
19
- specify { subject.ability.should be_a CanTango::Configuration::Ability }
20
- end
21
-
22
- describe 'hooks' do
23
- specify { subject.hooks.registered.should be_empty }
24
-
25
- describe 'register hooks' do
26
- before do
27
- subject.hooks.register :nime => Proc.new { 2 }
28
- end
29
- specify { subject.hooks.registered.should_not be_empty }
30
- specify { subject.hooks[:nime].should be_a Proc }
31
- end
32
- end
33
-
34
- describe 'include_models :default_guest_user' do
35
- before do
36
- subject.include_models :default_guest_user
37
- end
38
- specify { defined?(::Guest).should be_true }
39
- end
40
-
41
- describe 'enable defaults' do
42
- before do
43
- subject.enable_defaults!
44
- end
45
- specify { subject.engine(:permit).should be_nil }
46
- specify { subject.engine(:permission).should be_nil }
47
- end
48
-
49
- describe 'enable_helpers' do
50
- before do
51
- subject.enable_helpers :rest
52
- end
53
- specify { ::ApplicationController.instance_methods.should include(:link_to_new) }
54
- end
55
-
56
- describe 'localhosts' do
57
- specify { subject.localhosts.registered.should include('localhost') }
58
- end
59
-
60
- describe 'orms' do
61
- specify { subject.orms.registered.should be_empty }
62
-
63
- describe 'register' do
64
- before do
65
- subject.orms.register :mongoid
66
- end
67
- specify { subject.orms.registered.should include(:mongoid) }
68
- end
69
-
70
- describe '<<' do
71
- before do
72
- subject.orms << :mongo_mapper
73
- end
74
- specify { subject.orms.registered.should include(:mongo_mapper) }
75
- end
76
- end
77
-
78
- describe "engines DSL" do
79
- before(:all) {
80
- CanTango.configure do |config|
81
- CanTango.config.engines.each do |engine|
82
- engine.set :off
83
- end
84
- end
85
- }
86
-
87
- describe 'engine(name)' do
88
- CanTango.config.engines.registered_names.each do |name|
89
- specify { subject.engine(name).off?.should be_true}
90
- end
91
- end
92
-
93
- describe 'each' do
94
- CanTango.config.engines.each do |engine|
95
- specify { engine.on?.should be_false}
96
- specify { engine.off?.should be_true}
97
- end
98
- end
99
- end
100
-
101
- describe 'clear!' do
102
- before do
103
- subject.roles.exclude :user
104
- subject.role_groups.exclude :admins
105
-
106
- subject.clear!
107
- end
108
-
109
- specify { subject.roles.excluded.should be_empty }
110
- specify { subject.role_groups.excluded.should be_empty }
111
- end
112
- end