cantango-config 0.1.9.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,27 @@
1
+ module CanTango
2
+ class Config
3
+ module ClassMethods
4
+ def self.extended base
5
+ base.class_eval do
6
+ components.each do |conf_module|
7
+ class_eval %{
8
+ def #{conf_module} &block
9
+ conf = conf::#{conf_module.to_s.camelize}.instance
10
+ yield conf if block
11
+ conf
12
+ end
13
+ }
14
+ end
15
+ end
16
+ end
17
+
18
+ def components
19
+ [
20
+ :guest, :autoload, :user, :account, :models, :modes, :roles, :role_groups,
21
+ :engines, :users, :accounts, :categories, :adapters, :debug,
22
+ :localhosts, :orms, :hooks, :helpers
23
+ ]
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,5 +1,5 @@
1
1
  module CanTango
2
- class Configuration
2
+ class Config
3
3
  # Note: This config feature is currently not used, but could potentially be of use in the future
4
4
  class Debug
5
5
  include Singleton
@@ -0,0 +1,47 @@
1
+ module CanTango
2
+ class Config
3
+ class Engines
4
+ sweetload :Order, :Available
5
+
6
+ include Singleton
7
+ include Enumerable
8
+ include Order
9
+ include Available
10
+ include CanTango::Registry::Hash::InstanceMethods
11
+
12
+ # engine registry is a simple hash
13
+ def register hash
14
+ hash.each_pair do |name, engine_class|
15
+ raise "Class must implement the CanTango::Engine API." unless self.class.valid_engine?(engine_class)
16
+ raise "Name of engine must be a String or Symbol" if !name.kind_of_label?
17
+ registered[name.to_s] = engine_class
18
+ end
19
+ end
20
+
21
+ def clear!
22
+ each {|engine| engine.reset! }
23
+ @registered = nil
24
+ clear_order!
25
+ end
26
+
27
+ def each
28
+ available.each {|engine| yield send(engine) if respond_to?(engine) }
29
+ end
30
+
31
+ def active? name
32
+ active.include? name.to_sym
33
+ end
34
+
35
+ def active_list
36
+ available.select {|engine| send(engine).on? if respond_to?(engine) }
37
+ end
38
+
39
+ protected
40
+
41
+ # does it implement the basic Engine API?
42
+ def self.valid_engine? engine_class
43
+ [:execute!, :ability].all? {|meth| engine_class.instance_methods.include? meth }
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,19 @@
1
+ class CanTango::Config::Engines
2
+ module Available
3
+ def available
4
+ registered_names
5
+ end
6
+
7
+ def available? name
8
+ available.include? name.to_s
9
+ end
10
+
11
+ def all state
12
+ available.each {|engine| send(engine).set state }
13
+ end
14
+
15
+ def any? state
16
+ available.any? {|engine| send(engine).send(:"#{state}?") if respond_to?(engine) }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,48 @@
1
+ module CanTango
2
+ class Config
3
+ class Engines
4
+ module Order
5
+ def clear_order!
6
+ @execution_order = nil
7
+ end
8
+
9
+ # defines the order of execution of engine in ability
10
+ def set_execution_order *names
11
+ @execution_order = names.flatten.uniq.map(&:to_s).select {|name| available? name }
12
+ end
13
+
14
+ def dont_execute name
15
+ execution_order.delete(name.to_s)
16
+ end
17
+
18
+ def execute_first name
19
+ dont_execute name
20
+ execution_order.insert(0, name.to_s)
21
+ end
22
+
23
+ def execute_last name
24
+ dont_execute name
25
+ execution_order.insert(-1, name.to_s)
26
+ end
27
+
28
+ def execute_before existing, name
29
+ dont_execute name
30
+ index = execution_order.index(existing.to_s) || 0
31
+ execution_order.insert(index, name.to_s)
32
+ execution_order.compact!
33
+ end
34
+
35
+ def execute_after existing, name
36
+ dont_execute name
37
+ index = execution_order.index(existing.to_s)
38
+ index ? execution_order.insert(index +1, name.to_s) : execute_last(name)
39
+ execution_order.compact!
40
+ end
41
+
42
+ def execution_order
43
+ @execution_order ||= (available - [:cache])
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,5 +1,5 @@
1
1
  module CanTango
2
- class Configuration
2
+ class Config
3
3
  module Factory
4
4
  def clear_factory!
5
5
  @factory = nil
@@ -1,5 +1,5 @@
1
1
  module CanTango
2
- class Configuration
2
+ class Config
3
3
  class Guest
4
4
  include Singleton
5
5
 
@@ -15,11 +15,10 @@ module CanTango
15
15
 
16
16
  alias_method :user=, :user
17
17
 
18
- def account account = nil, &block
19
- return (@account || guest_account) if !account && !block
20
- @account = account || yield
18
+ def account acc = nil, &block
19
+ return (@account || acc) if !acc && !block
20
+ @account = acc || yield
21
21
  end
22
- alias_method :user_account, :account
23
22
  alias_method :account=, :account
24
23
 
25
24
  def default_user?
@@ -49,7 +48,7 @@ module CanTango
49
48
  end
50
49
 
51
50
  def base_account_class
52
- CanTango.config.user_account.base_class
51
+ CanTango.config.account.base_class
53
52
  end
54
53
  end
55
54
  end
@@ -0,0 +1,17 @@
1
+ module CanTango
2
+ class Config
3
+ class Helpers
4
+ include Singleton
5
+
6
+ def enable *names
7
+ names = names.to_symbols
8
+ enable_rest if names.include? :rest
9
+ end
10
+
11
+ def enable_rest
12
+ raise 'ApplicationController not defined' if !defined?(::ApplicationController)
13
+ ::ApplicationController.send :include, CanTango::Rails::Helpers::RestHelper
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,8 +1,8 @@
1
1
  module CanTango
2
- class Configuration
3
- class Hooks < Registry::Hash
2
+ class Config
3
+ class Hooks < CanTango::Registry::Hash
4
4
  include Singleton
5
-
5
+
6
6
  def value_methods
7
7
  [:call]
8
8
  end
@@ -1,12 +1,11 @@
1
1
  module CanTango
2
- class Configuration
3
- class Localhosts < Registry::Base
2
+ class Config
3
+ class Localhosts < CanTango::Registry::Base
4
4
  include Singleton
5
-
5
+
6
6
  def default
7
7
  @default ||= ['localhost', '0.0.0.0', '127.0.0.1']
8
8
  end
9
9
  end
10
10
  end
11
- end
12
-
11
+ end
@@ -1,15 +1,24 @@
1
1
  module CanTango
2
- class Configuration
2
+ class Config
3
3
  class Models
4
- autoload_modules :Generic, :ActiveRecord, :DataMapper, :Mongo, :MongoMapper, :Mongoid
5
- autoload_modules :Actions
4
+ sweetload :Generic, :ActiveRecord, :DataMapper, :Mongo, :MongoMapper, :Mongoid
5
+ sweetload :Actions
6
6
 
7
7
  include CanTango::Helpers::Debug
8
8
  include Singleton
9
9
  include ClassExt
10
10
 
11
+ def use *names
12
+ names = names.select_symbols
13
+ if names.include? :guest_user
14
+ require 'cantango/model/user/guest'
15
+ end
16
+ rescue LoadError => e
17
+ debug e.message
18
+ end
19
+
11
20
  def actions
12
- @actions ||= Registry::Hash.new
21
+ @actions ||= CanTango::Registry::Hash.new
13
22
  end
14
23
 
15
24
  def by_reg_exp reg_exp
@@ -34,13 +43,13 @@ module CanTango
34
43
  @excluded ||= []
35
44
  end
36
45
 
37
- def available_models
38
- all_models - excluded.map {|m| m.to_s.camelize}
46
+ def available
47
+ all - excluded.map {|m| m.to_s.camelize}
39
48
  end
40
49
 
41
50
  protected
42
51
 
43
- def all_models
52
+ def all
44
53
  CanTango.config.orms.registered.compact.inject([]) do |result, orm|
45
54
  adapter = adapter_for(orm)
46
55
  result << (adapter.models.map(&:name) if adapter)
@@ -1,6 +1,6 @@
1
1
  module CanTango
2
- class Configuration
3
- class Orms < Registry::Base
2
+ class Config
3
+ class Orms < CanTango::Registry::Base
4
4
  include Singleton
5
5
  end
6
6
  end
@@ -1,5 +1,5 @@
1
1
  module CanTango
2
- class Configuration
2
+ class Config
3
3
  class User
4
4
  include Singleton
5
5
 
@@ -1,6 +1,6 @@
1
1
  module CanTango
2
- class Configuration
3
- class Users < Registry::Clazz
2
+ class Config
3
+ class Users < CanTango::Registry::Clazz
4
4
  include Singleton
5
5
 
6
6
  def register_user clazz
@@ -0,0 +1,5 @@
1
+ module CanTango
2
+ module Registry
3
+ sweetload :Base, :Hash, :Clazz
4
+ end
5
+ end
@@ -2,10 +2,10 @@ require 'singleton'
2
2
  require 'sugar-high/kind_of'
3
3
 
4
4
  module CanTango
5
- class Configuration
6
- module Registry
7
- class Base
8
- attr_writer :default
5
+ module Registry
6
+ class Base
7
+ module InstanceMethods
8
+ attr_writer :default, :available
9
9
  attr_accessor :registered
10
10
 
11
11
  def types= *types
@@ -21,6 +21,10 @@ module CanTango
21
21
  @registered = []
22
22
  end
23
23
 
24
+ def reset!
25
+ @registered = default
26
+ end
27
+
24
28
  alias_method :clear!, :clean!
25
29
 
26
30
  def default!
@@ -49,7 +53,17 @@ module CanTango
49
53
  def default
50
54
  @default ||= []
51
55
  end
56
+
57
+ def valid? label
58
+ return available.include?(label) if respond_to?(:available) && !available.empty?
59
+ true
60
+ end
61
+
62
+ def available
63
+ []
64
+ end
52
65
  end
66
+ include InstanceMethods
53
67
  end
54
68
  end
55
69
  end
@@ -0,0 +1,92 @@
1
+ require 'singleton'
2
+ require 'sugar-high/kind_of'
3
+
4
+ module CanTango::Registry
5
+ class Clazz
6
+ module InstanceMethods
7
+ def register label, value
8
+ raise "first arg must be a label, was: #{label}" if !label.kind_of_label?
9
+ raise "second arg must be a valid Class, was: #{value}" if !valid? value
10
+ name_registry.register label.to_sym
11
+ class_registry.register value
12
+ end
13
+ alias_method :[]=, :register
14
+
15
+ def << hash
16
+ raise "Must be a hash" if !hash.is_a?(::Hash)
17
+ hash.each_pair do |key, value|
18
+ register key, value
19
+ end
20
+ end
21
+
22
+ def registered
23
+ name_registry.registered
24
+ end
25
+ alias_method :registered_names, :registered
26
+
27
+ def clean!
28
+ name_registry.clean!
29
+ class_registry.clean!
30
+ end
31
+ alias_method :clear!, :clean!
32
+
33
+ def registered_classes
34
+ class_registry.registered
35
+ end
36
+
37
+ def registered? name
38
+ name_registry.registered? name
39
+ end
40
+
41
+ def registered_class? name
42
+ class_registry.registered? name
43
+ end
44
+
45
+ def name_registry
46
+ @name_registry ||=NameRegistry.new
47
+ end
48
+
49
+ def class_registry
50
+ @class_registry ||= ClassRegistry.new
51
+ end
52
+
53
+ def value_methods
54
+ class_registry.value_methods
55
+ end
56
+
57
+ def value_types
58
+ class_registry.types
59
+ end
60
+
61
+ class NameRegistry < CanTango::Registry::Base
62
+ end
63
+
64
+ class ClassRegistry < CanTango::Registry::Base
65
+ def types
66
+ [Class]
67
+ end
68
+
69
+ def value_methods
70
+ []
71
+ end
72
+ end
73
+
74
+ protected
75
+
76
+ def valid? value
77
+ valid_by_type?(value) && valid_by_methods?(value)
78
+ end
79
+
80
+ def valid_by_type? value
81
+ return true if value_types.blank?
82
+ value.any_kind_of?(*value_types)
83
+ end
84
+
85
+ def valid_by_methods? value
86
+ return true if value_methods.blank?
87
+ value_methods.all?{|m| value.respond_to(m)}
88
+ end
89
+ end
90
+ include InstanceMethods
91
+ end
92
+ end