cantango-config 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +40 -0
  4. data/Gemfile.lock +163 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.mdown +21 -0
  7. data/Rakefile +49 -0
  8. data/VERSION +1 -0
  9. data/cantango-config.gemspec +145 -0
  10. data/lib/cantango/adapter/compiler.rb +9 -0
  11. data/lib/cantango/adapter/moneta.rb +23 -0
  12. data/lib/cantango/config.rb +12 -0
  13. data/lib/cantango/configuration/ability.rb +14 -0
  14. data/lib/cantango/configuration/account.rb +23 -0
  15. data/lib/cantango/configuration/accounts.rb +8 -0
  16. data/lib/cantango/configuration/adapters.rb +24 -0
  17. data/lib/cantango/configuration/autoload.rb +38 -0
  18. data/lib/cantango/configuration/candidate_registry.rb +94 -0
  19. data/lib/cantango/configuration/categories.rb +57 -0
  20. data/lib/cantango/configuration/debug.rb +41 -0
  21. data/lib/cantango/configuration/engine.rb +33 -0
  22. data/lib/cantango/configuration/engines.rb +97 -0
  23. data/lib/cantango/configuration/factory.rb +59 -0
  24. data/lib/cantango/configuration/guest.rb +57 -0
  25. data/lib/cantango/configuration/hash_registry.rb +91 -0
  26. data/lib/cantango/configuration/hooks.rb +12 -0
  27. data/lib/cantango/configuration/localhosts.rb +12 -0
  28. data/lib/cantango/configuration/models/actions.rb +26 -0
  29. data/lib/cantango/configuration/models/active_record.rb +22 -0
  30. data/lib/cantango/configuration/models/data_mapper.rb +12 -0
  31. data/lib/cantango/configuration/models/generic.rb +12 -0
  32. data/lib/cantango/configuration/models/mongo.rb +12 -0
  33. data/lib/cantango/configuration/models/mongo_mapper.rb +11 -0
  34. data/lib/cantango/configuration/models/mongoid.rb +13 -0
  35. data/lib/cantango/configuration/models.rb +77 -0
  36. data/lib/cantango/configuration/modes.rb +21 -0
  37. data/lib/cantango/configuration/orms.rb +8 -0
  38. data/lib/cantango/configuration/registry.rb +56 -0
  39. data/lib/cantango/configuration/user.rb +47 -0
  40. data/lib/cantango/configuration/users.rb +8 -0
  41. data/lib/cantango/configuration.rb +99 -0
  42. data/spec/cantango/config_spec.rb +8 -0
  43. data/spec/cantango/configuration/ability_spec.rb +13 -0
  44. data/spec/cantango/configuration/account_spec.rb +21 -0
  45. data/spec/cantango/configuration/accounts_spec.rb +18 -0
  46. data/spec/cantango/configuration/adapters_spec.rb +30 -0
  47. data/spec/cantango/configuration/autoload_spec.rb +52 -0
  48. data/spec/cantango/configuration/candidate_registry_spec.rb +12 -0
  49. data/spec/cantango/configuration/categories_spec.rb +58 -0
  50. data/spec/cantango/configuration/debug_spec.rb +39 -0
  51. data/spec/cantango/configuration/engines/engine_shared.rb +22 -0
  52. data/spec/cantango/configuration/engines_spec.rb +102 -0
  53. data/spec/cantango/configuration/factory_spec.rb +18 -0
  54. data/spec/cantango/configuration/guest/find_guest_default_way_spec.rb +32 -0
  55. data/spec/cantango/configuration/guest_spec.rb +61 -0
  56. data/spec/cantango/configuration/hash_registry_spec.rb +22 -0
  57. data/spec/cantango/configuration/localhosts_spec.rb +9 -0
  58. data/spec/cantango/configuration/models_spec.rb +47 -0
  59. data/spec/cantango/configuration/orms_spec.rb +9 -0
  60. data/spec/cantango/configuration/registry_spec.rb +15 -0
  61. data/spec/cantango/configuration/shared/candidate_registry_ex.rb +47 -0
  62. data/spec/cantango/configuration/shared/factory_ex.rb +40 -0
  63. data/spec/cantango/configuration/shared/hash_registry_ex.rb +55 -0
  64. data/spec/cantango/configuration/shared/modes_ex.rb +14 -0
  65. data/spec/cantango/configuration/shared/registry_ex.rb +40 -0
  66. data/spec/cantango/configuration/shared/role_registry_ex.rb +22 -0
  67. data/spec/cantango/configuration/user_spec.rb +45 -0
  68. data/spec/cantango/configuration/users_spec.rb +18 -0
  69. data/spec/cantango/configuration_spec.rb +112 -0
  70. data/spec/db/database.yml +4 -0
  71. data/spec/factories/project.rb +6 -0
  72. data/spec/fixtures/models/admin.rb +2 -0
  73. data/spec/fixtures/models/admin_account.rb +22 -0
  74. data/spec/fixtures/models/items.rb +8 -0
  75. data/spec/fixtures/models/permission.rb +12 -0
  76. data/spec/fixtures/models/project.rb +2 -0
  77. data/spec/fixtures/models/simple_roles.rb +49 -0
  78. data/spec/fixtures/models/user.rb +52 -0
  79. data/spec/fixtures/models/user_account.rb +21 -0
  80. data/spec/fixtures/models.rb +2 -0
  81. data/spec/migrations/001_create_projects.rb +12 -0
  82. data/spec/spec_helper.rb +35 -0
  83. metadata +231 -0
@@ -0,0 +1,77 @@
1
+ module CanTango
2
+ class Configuration
3
+ class Models
4
+ autoload_modules :Generic, :ActiveRecord, :DataMapper, :MongoMapper, :Mongoid
5
+ autoload_modules :Actions
6
+
7
+ include CanTango::Helpers::Debug
8
+ include Singleton
9
+ include ClassExt
10
+
11
+ def actions
12
+ @actions ||= HashRegistry.new
13
+ end
14
+
15
+ def by_reg_exp reg_exp
16
+ raise "Must be a Regular Expression like: /xyz/ was #{reg_exp.inspect}" if !reg_exp.kind_of? Regexp
17
+
18
+ grep(reg_exp).map do |model_string|
19
+ try_model(model_string)
20
+ end
21
+ end
22
+
23
+ def by_category label
24
+ categories[label].map do |model|
25
+ model.class == String ? try_model(model) : model
26
+ end
27
+ end
28
+
29
+ def exclude *names
30
+ @excluded = names.flatten.select_labels
31
+ end
32
+
33
+ def excluded
34
+ @excluded ||= []
35
+ end
36
+
37
+ def available_models
38
+ all_models - excluded.map {|m| m.to_s.camelize}
39
+ end
40
+
41
+ protected
42
+
43
+ def all_models
44
+ CanTango.config.orms.registered.compact.inject([]) do |result, orm|
45
+ adapter = adapter_for(orm)
46
+ result << (adapter.models.map(&:name) if adapter)
47
+ result
48
+ end.flatten.compact
49
+ end
50
+
51
+ private
52
+
53
+ def adapter_for orm
54
+ clazz_name = "CanTango::Configuration::Models::#{orm.to_s.camelize}"
55
+ clazz = clazz_name.constantize
56
+ clazz.new
57
+ rescue Exception => e
58
+ debug "Unknown ORM: #{orm} - no adapter defined for this ORM - #{e}"
59
+ end
60
+
61
+ def try_model model_string
62
+ model = find_first_class(model_string.singularize, model_string)
63
+ raise "No model #{model_string} defined!" if !model
64
+ model
65
+ end
66
+
67
+ def grep reg_exp
68
+ available_models.grep reg_exp
69
+ end
70
+
71
+ def categories
72
+ CanTango.config.categories
73
+ end
74
+ end
75
+ end
76
+ end
77
+
@@ -0,0 +1,21 @@
1
+ module CanTango
2
+ class Configuration
3
+ module Modes
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
@@ -0,0 +1,8 @@
1
+ module CanTango
2
+ class Configuration
3
+ class Orms < Registry
4
+ include Singleton
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,56 @@
1
+ require 'singleton'
2
+ require 'sugar-high/kind_of'
3
+
4
+ module CanTango
5
+ class Configuration
6
+ class Registry
7
+
8
+ attr_writer :default
9
+ attr_accessor :registered
10
+
11
+ def types= *types
12
+ @types = types.select {|t| t.is_a? Class }
13
+ end
14
+
15
+ def types
16
+ @types ||= [Symbol, String]
17
+ @types
18
+ end
19
+
20
+ def clean!
21
+ @registered = []
22
+ end
23
+
24
+ alias_method :clear!, :clean!
25
+
26
+ def default!
27
+ @registered = default
28
+ end
29
+
30
+ def register *list
31
+ registered << list.select_kinds_of(*types)
32
+ @registered.flat_uniq!
33
+ end
34
+
35
+ alias_method :<<, :register
36
+
37
+ def [] index
38
+ registered[index]
39
+ end
40
+
41
+ def registered
42
+ @registered ||= default
43
+ end
44
+
45
+ def registered? label
46
+ registered.map(&:to_s).include? label.to_s
47
+ end
48
+
49
+ def default
50
+ @default ||= []
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+
@@ -0,0 +1,47 @@
1
+ module CanTango
2
+ class Configuration
3
+ class User
4
+ include Singleton
5
+
6
+ include ClassExt
7
+
8
+ def clear!
9
+ @clazz = nil
10
+ @unique_key_field = nil
11
+ @relations = nil
12
+ end
13
+
14
+ def base_class
15
+ @clazz ||= (::User if defined? ::User)
16
+ end
17
+
18
+ def base_class= clazz
19
+ raise ArgumentError, "Must be a class, was: #{clazz}" unless is_class? clazz
20
+ @clazz = clazz
21
+ end
22
+
23
+ def unique_key_field
24
+ @unique_key_field || :email
25
+ end
26
+
27
+ def unique_key_field= key
28
+ raise ArgumentError, "Not a valid key" unless key.kind_of_label?
29
+ @unique_key_field = key.to_sym
30
+ end
31
+
32
+ def relations= *relations
33
+ @relations = relations.select_labels
34
+ end
35
+
36
+ def relations
37
+ @relations ||= default_relations
38
+ end
39
+
40
+ def default_relations
41
+ [:owner, :author, :writer, :user]
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+
@@ -0,0 +1,8 @@
1
+ module CanTango
2
+ class Configuration
3
+ class Users < CandidateRegistry
4
+ include Singleton
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,99 @@
1
+ require 'set'
2
+ require 'singleton'
3
+
4
+ module CanTango
5
+ class Configuration
6
+ autoload_modules :Categories
7
+ autoload_modules :Models, :Engines, :Ability
8
+ autoload_modules :Registry, :RoleRegistry, :HashRegistry, :CandidateRegistry
9
+ autoload_modules :Roles, :RoleGroups, :SpecialPermits
10
+ autoload_modules :Factory, :Autoload, :Adapters, :Debug, :Modes, :Orms, :Localhosts, :Hooks
11
+ autoload_modules :Account, :Accounts
12
+ autoload_modules :User, :Guest, :Users
13
+
14
+ include Singleton
15
+
16
+ def ability
17
+ @ability ||= conf::Ability.instance
18
+ @ability.default_class ||= CanTango::Ability::Executor::Base
19
+ @ability
20
+ end
21
+
22
+ def self.components
23
+ [
24
+ :guest, :autoload, :user, :account, :models, :modes, :roles, :role_groups,
25
+ :engines, :users, :accounts, :categories, :adapters, :debug,
26
+ :localhosts, :orms, :hooks
27
+ ]
28
+ end
29
+
30
+ components.each do |conf_module|
31
+ class_eval %{
32
+ def #{conf_module} &block
33
+ conf = conf::#{conf_module.to_s.camelize}.instance
34
+ yield conf if block
35
+ conf
36
+ end
37
+ }
38
+ end
39
+
40
+ def debug!
41
+ debug.set :on
42
+ end
43
+
44
+ # Turn on default engines and enable compile adapter
45
+ # i.e compilation of rules via sourcify
46
+ def enable_defaults!
47
+ engines.all :off
48
+ adapters.use :compiler
49
+ end
50
+
51
+ def enable_helpers *names
52
+ names = names.to_symbols
53
+ enable_rest_helper if names.include? :rest
54
+ end
55
+
56
+ def enable_rest_helper
57
+ raise 'ApplicationController not defined' if !defined?(::ApplicationController)
58
+ ::ApplicationController.send :include, CanTango::Rails::Helpers::RestHelper
59
+ end
60
+
61
+ def clear!
62
+ CanTango::Configuration.components.each do |component|
63
+ if respond_to? component
64
+ component = send(component)
65
+ component.send(:clear!) if component.respond_to? :clear!
66
+ end
67
+ end
68
+ engines.clear!
69
+ end
70
+
71
+ def include_models *names
72
+ names = names.select_symbols
73
+ if names.include? :default_guest_user
74
+ require 'cantango/user/guest'
75
+ end
76
+ end
77
+
78
+ # allow either block or direct access
79
+ # engine(:permission) do |permission|
80
+ # engine(:permission).config_path
81
+ def engine name, &block
82
+ engine = find_engine(name)
83
+ yield engine if block
84
+ engine
85
+ end
86
+
87
+ protected
88
+
89
+ def find_engine name
90
+ raise ArgumentError, "Must be label for an engine" if !name.kind_of_label?
91
+ name = name.to_s.singularize
92
+ engines.send(name) if engines.available? name
93
+ end
94
+
95
+ def conf
96
+ CanTango::Configuration
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,8 @@
1
+ require 'rspec'
2
+ require 'cantango/config'
3
+
4
+ describe 'cantango/config' do
5
+ specify do
6
+ lambda { CanTango::Configuration }.should_not raise_error
7
+ end
8
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+ require 'cantango/configuration/shared/factory_ex'
3
+ require 'cantango/configuration/shared/modes_ex'
4
+
5
+ describe CanTango::Configuration::Ability do
6
+ subject { CanTango.config.ability }
7
+
8
+ it_should_behave_like 'Factory'
9
+
10
+ it_should_behave_like 'Modes'
11
+ end
12
+
13
+
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ class BaseAccount
4
+ def initialize
5
+ end
6
+ end
7
+
8
+
9
+ describe CanTango::Configuration::Account do
10
+ subject { CanTango.config.account }
11
+
12
+ describe 'set base class' do
13
+ before do
14
+ subject.base_class = BaseAccount
15
+ end
16
+
17
+ its(:base_class) { should == BaseAccount }
18
+ end
19
+ end
20
+
21
+
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+ require 'cantango/configuration/shared/candidate_registry_ex'
3
+
4
+ class UserAccount
5
+ end
6
+
7
+ class AdminAccount
8
+ end
9
+
10
+ describe CanTango::Configuration::Accounts do
11
+ subject { CanTango.config.accounts }
12
+
13
+ it_should_behave_like "Candidate Registry" do
14
+ let(:hash1) do
15
+ {:a => UserAccount, :b => AdminAccount}
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe CanTango::Configuration::Adapters do
4
+ subject { CanTango.config.adapters }
5
+
6
+ describe 'should run adapter for moneta' do
7
+ before do
8
+ subject.adapter :moneta
9
+ end
10
+
11
+ specify { CanTango.config.adapters.registered?(:moneta).should be_true }
12
+ # specify { lambda { CanTango::MonetaCache }.should_not raise_error }
13
+ # specify { lambda { CanTango::MonetaAbilityCache }.should_not raise_error }
14
+ # specify { lambda { CanTango::MonetaPermitStore }.should_not raise_error }
15
+ end
16
+
17
+ specify { lambda { CanTango::Ability::Cache::Kompiler }.should raise_error }
18
+
19
+ describe 'should run adapter for sourcify compiler' do
20
+
21
+ before do
22
+ subject.adapter :compiler
23
+ end
24
+
25
+ specify { CanTango.config.adapters.registered?(:compiler).should be_true }
26
+ # specify { lambda { CanTango::Ability::Cache::Kompiler }.should_not raise_error }
27
+ end
28
+ end
29
+
30
+
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+ require 'fixtures/models'
3
+
4
+ describe CanTango::Configuration::Autoload do
5
+ subject { CanTango.config.autoload }
6
+
7
+ describe 'default settings' do
8
+ its(:permits) { should be_true }
9
+ its(:models) { should be_true }
10
+ end
11
+
12
+ describe 'permits' do
13
+ describe 'turn off' do
14
+ before do
15
+ subject.permits :off
16
+ end
17
+
18
+ its(:permits) { should be_false }
19
+ its(:permits?) { should be_false }
20
+ end
21
+
22
+ describe 'turn on' do
23
+ before do
24
+ subject.permits :on
25
+ end
26
+
27
+ its(:permits) { should be_true }
28
+ its(:permits?) { should be_true }
29
+ end
30
+ end
31
+
32
+ describe 'models' do
33
+ describe 'turn off' do
34
+ before do
35
+ subject.models :off
36
+ end
37
+
38
+ its(:models) { should be_false }
39
+ its(:models?) { should be_false }
40
+ end
41
+
42
+ describe 'turn on' do
43
+ before do
44
+ subject.models :on
45
+ end
46
+
47
+ its(:models) { should be_true }
48
+ its(:models?) { should be_true }
49
+ end
50
+ end
51
+ end
52
+
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+ require 'cantango/configuration/shared/candidate_registry_ex'
3
+
4
+ class MyReg < CanTango::Configuration::CandidateRegistry
5
+ end
6
+
7
+ describe MyReg do
8
+ subject { MyReg.new }
9
+
10
+ it_should_behave_like "Candidate Registry" do
11
+ end
12
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+ require 'cantango/configuration/shared/hash_registry_ex'
3
+
4
+ describe CanTango::Configuration::Categories::Category do
5
+ subject { CanTango::Configuration::Categories::Category.new }
6
+ specify { subject.has_any?(:x).should_not be_true }
7
+
8
+ subject { CanTango::Configuration::Categories::Category.new :a, :b }
9
+ specify { subject.has_any?(:a).should be_true }
10
+ end
11
+
12
+ describe CanTango::Configuration::Categories do
13
+ subject { CanTango.config.categories }
14
+
15
+ describe 'API' do
16
+ before(:each) do
17
+ categories = {:a => ['B', 'C'], 'x' => ['Y', 'Z'], 'v' => ['B', 'Z']}
18
+
19
+ subject.clean!
20
+ subject.register categories
21
+ end
22
+
23
+ describe 'get index []' do
24
+ specify {
25
+ subject.register(:w => 'something non-array!')
26
+ lambda { subject.category('w') }.should raise_error
27
+ }
28
+ end
29
+
30
+ describe 'category_has_subject?' do
31
+ specify {
32
+ subject.category('a').has_any?('B').should be_true
33
+ }
34
+ end
35
+
36
+ describe 'has_subject?' do
37
+ specify { subject.has_any?('Y').should be_true }
38
+ end
39
+
40
+ describe 'category_names_of_subject' do
41
+ specify { subject.category_names_of_subject('B').last.should == 'v' }
42
+
43
+ specify { subject.category_names_of_subject('c').first.should == nil }
44
+ end
45
+
46
+ describe 'categories_of_subject' do
47
+ specify do
48
+ subject.categories_of_subject('B').should == {'a' => ['B', 'C'], 'v' => ['B', 'Z'],}
49
+ end
50
+
51
+ specify do
52
+ subject.categories_of_subject('blip').should == {}
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ class Context
4
+ include CanTango::Api::Ability::User
5
+ end
6
+
7
+ # Note: This config feature is currently not used, but could potentially be of use in the future
8
+ describe CanTango::Configuration::Debug do
9
+ let(:context) { Context.new }
10
+ subject { CanTango.config.debug }
11
+
12
+ describe 'should set debug mode :on' do
13
+ before do
14
+ subject.set :on
15
+ end
16
+
17
+ its(:on?) { should be_true }
18
+ its(:off?) { should be_false }
19
+ end
20
+
21
+ describe 'should set debug mode :off' do
22
+ before do
23
+ subject.set :on
24
+ end
25
+
26
+ its(:on?) { should be_true }
27
+ its(:off?) { should be_false }
28
+ end
29
+
30
+ describe 'debug! should set debug mode :on' do
31
+ before do
32
+ CanTango.debug!
33
+ end
34
+
35
+ its(:on?) { should be_true }
36
+ its(:off?) { should be_false }
37
+ end
38
+ end
39
+
@@ -0,0 +1,22 @@
1
+ shared_examples_for 'Engine' do
2
+ describe 'turn off' do
3
+ before :each do
4
+ subject.set :off
5
+ end
6
+
7
+ its(:on?) { should be_false }
8
+ its(:off?) { should be_true }
9
+ end
10
+
11
+ describe 'turn on' do
12
+ before :each do
13
+ subject.set :on
14
+ end
15
+
16
+ its(:on?) { should be_true }
17
+ its(:off?) { should be_false }
18
+ end
19
+ end
20
+
21
+
22
+