ns-options 0.4.1 → 1.0.0.rc1

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 (51) hide show
  1. data/.gitignore +3 -0
  2. data/Gemfile +2 -1
  3. data/LICENSE +1 -1
  4. data/README.md +187 -301
  5. data/Rakefile +1 -1
  6. data/lib/ns-options/assert_macros.rb +9 -12
  7. data/lib/ns-options/boolean.rb +2 -0
  8. data/lib/ns-options/namespace.rb +34 -134
  9. data/lib/ns-options/namespace_advisor.rb +35 -0
  10. data/lib/ns-options/namespace_data.rb +166 -0
  11. data/lib/ns-options/namespaces.rb +23 -12
  12. data/lib/ns-options/option.rb +50 -24
  13. data/lib/ns-options/options.rb +23 -49
  14. data/lib/ns-options/proxy.rb +40 -53
  15. data/lib/ns-options/proxy_method.rb +54 -0
  16. data/lib/ns-options/root_methods.rb +77 -0
  17. data/lib/ns-options/version.rb +1 -1
  18. data/lib/ns-options.rb +18 -8
  19. data/ns-options.gemspec +3 -4
  20. data/test/helper.rb +3 -10
  21. data/test/support/app.rb +3 -1
  22. data/test/support/proxy.rb +4 -0
  23. data/test/support/type_class_proxy.rb +29 -0
  24. data/test/support/user.rb +5 -5
  25. data/test/{integration/app_test.rb → system/app_tests.rb} +8 -6
  26. data/test/{integration/proxy_test.rb → system/proxy_tests.rb} +12 -0
  27. data/test/system/type_class_proxy_tests.rb +108 -0
  28. data/test/system/user_tests.rb +146 -0
  29. data/test/unit/{ns-options/boolean_test.rb → boolean_tests.rb} +5 -4
  30. data/test/unit/namespace_advisor_tests.rb +69 -0
  31. data/test/unit/namespace_data_tests.rb +336 -0
  32. data/test/unit/namespace_tests.rb +205 -0
  33. data/test/unit/namespaces_tests.rb +99 -0
  34. data/test/unit/{ns-options/option_test.rb → option_tests.rb} +155 -93
  35. data/test/unit/options_tests.rb +152 -0
  36. data/test/unit/proxy_method_tests.rb +87 -0
  37. data/test/unit/{ns-options/proxy_test.rb → proxy_tests.rb} +52 -0
  38. data/test/unit/root_methods_tests.rb +126 -0
  39. metadata +58 -63
  40. data/lib/ns-options/errors/invalid_name.rb +0 -15
  41. data/lib/ns-options/has_options.rb +0 -53
  42. data/lib/ns-options/helper/advisor.rb +0 -88
  43. data/lib/ns-options/helper.rb +0 -87
  44. data/test/integration/user_test.rb +0 -94
  45. data/test/unit/ns-options/has_options_test.rb +0 -90
  46. data/test/unit/ns-options/helper/advisor_test.rb +0 -148
  47. data/test/unit/ns-options/helper_test.rb +0 -56
  48. data/test/unit/ns-options/namespace_test.rb +0 -432
  49. data/test/unit/ns-options/namespaces_test.rb +0 -55
  50. data/test/unit/ns-options/options_test.rb +0 -221
  51. /data/test/unit/{ns-options/assert_macros_test.rb → assert_macros_tests.rb} +0 -0
@@ -1,94 +0,0 @@
1
- require 'assert'
2
-
3
- class User
4
-
5
- class BaseTest < Assert::Context
6
- desc "the User class"
7
- setup do
8
- @class = User
9
- end
10
- subject{ @class }
11
-
12
- should have_instance_methods :options, :preferences
13
-
14
- end
15
-
16
- class ClassPreferencesTest < BaseTest
17
- desc "preferences"
18
- subject{ @class.preferences }
19
-
20
- should have_instance_methods :namespace, :option, :define, :options, :metaclass
21
- should have_accessors :home_url, :show_messages, :font_size
22
-
23
- end
24
-
25
- class InstanceTest < BaseTest
26
- desc "instance"
27
- setup do
28
- @instance = @class.new
29
- @class_preferences = @class.preferences
30
- @class_preferences.home_url = "/something"
31
- @preferences = @instance.preferences
32
- end
33
- subject{ @instance }
34
-
35
- should have_instance_methods :preferences
36
-
37
- should "have a new namespace that is a different object than the class namespace" do
38
- assert_equal @class_preferences.options.key, @preferences.options.key
39
- assert_equal @class_preferences.options.parent, @preferences.options.parent
40
- assert_not_same @class_preferences, @preferences
41
- end
42
- should "have the same options as the class namespace, but different objects" do
43
- @class_preferences.options.each do |key, class_option|
44
- option = @preferences.options[key]
45
- assert_equal class_option.name, option.name
46
- assert_equal class_option.type_class, option.type_class
47
- assert_equal class_option.rules, option.rules
48
- assert_not_same class_option, option
49
- end
50
- assert_not_equal @class_preferences.home_url, @preferences.home_url
51
- end
52
- should "have the same namespaces as the class namespace, but different objects" do
53
- @class_preferences.options.namespaces.each do |name, class_namespace|
54
- namespace = @preferences.options.namespaces[name]
55
- assert_equal class_namespace.options.key, namespace.options.key
56
- assert_equal class_namespace.options.parent, namespace.options.parent
57
- assert_not_same class_namespace, namespace
58
- end
59
- end
60
- end
61
-
62
- class PreferencesTest < InstanceTest
63
- desc "preferences"
64
- setup do
65
- @preferences = @instance.preferences
66
- @preferences.home_url = "/home"
67
- @preferences.show_messages = false
68
- @preferences.font_size = 15
69
- @preferences.view.color = "green"
70
- end
71
- subject{ @preferences }
72
-
73
- should have_accessors :home_url, :show_messages, :font_size
74
- should have_instance_methods :namespace, :option, :define, :options, :metaclass
75
- should have_instance_methods :view
76
-
77
- should "have set the home_url" do
78
- assert_equal "/home", subject.home_url
79
- end
80
-
81
- should "have set show_messages" do
82
- assert_equal false, subject.show_messages
83
- end
84
-
85
- should "have set the font_size" do
86
- assert_equal 15, subject.font_size
87
- end
88
-
89
- should "have set the color option on the view sub namespace" do
90
- assert_equal "green", subject.view.color
91
- end
92
- end
93
-
94
- end
@@ -1,90 +0,0 @@
1
- require 'assert'
2
-
3
- module NsOptions::HasOptions
4
-
5
- class BaseTest < Assert::Context
6
- desc "NsOptions::HasOptions"
7
- setup do
8
- @class = Class.new do
9
- include NsOptions::HasOptions
10
-
11
- def configs_key
12
- "random_class_#{self.object_id}"
13
- end
14
- end
15
- @instance = @class.new
16
- end
17
- subject{ @instance }
18
-
19
- should have_class_methods :options, :opts
20
-
21
- end
22
-
23
- class OptionsTests < BaseTest
24
- desc "options method"
25
-
26
- class WithAKeyTest < OptionsTests
27
- desc "with a key"
28
- setup do
29
- @key = "configs-key"
30
- @class.options(:configs, @key) do
31
- option :something
32
- end
33
- @instance = @class.new
34
- end
35
- subject{ @instance }
36
-
37
- should have_class_method :configs
38
- should have_instance_method :configs
39
-
40
- should "have used the provided key when creating the namespace" do
41
- assert_kind_of NsOptions::Namespace, subject.class.configs
42
- assert_kind_of NsOptions::Namespace, subject.configs
43
- assert_equal @key, subject.class.configs.options.key
44
- assert_match @key, subject.configs.options.key
45
- end
46
-
47
- should "have used the provided block to define the namespace" do
48
- assert_respond_to :something, subject.configs
49
- assert_respond_to :something=, subject.configs
50
- assert subject.configs.options.fetch(:something)
51
- end
52
-
53
- end
54
-
55
- class WithoutAKeyTest < OptionsTests
56
- desc "without a key"
57
- setup do
58
- @name = "configs"
59
- @class.options(@name.to_sym)
60
- @instance = @class.new
61
- end
62
- subject{ @instance }
63
-
64
- should "have used the name for the key when creating the namespace" do
65
- assert_equal @name, subject.class.configs.options.key
66
- assert_match @name, subject.configs.options.key
67
- end
68
-
69
- end
70
-
71
- class AdvisorTests < OptionsTests
72
- desc "creating a root namespace with a bad name"
73
- setup do
74
- @ns_name = "options"
75
- @output = NsOptions::TestOutput.capture do
76
- @ns = @class.options(@ns_name.to_sym)
77
- end
78
- @advisor = NsOptions::Helper::Advisor.new(@ns)
79
- end
80
-
81
- should "output a message warning using the method name as a namespace name" do
82
- expected = @advisor.not_recommended_method_message("namespace", "options")
83
- assert_match expected, @output.string
84
- end
85
-
86
- end
87
-
88
- end
89
-
90
- end
@@ -1,148 +0,0 @@
1
- require 'assert'
2
-
3
- class NsOptions::Helper::Advisor
4
-
5
- class BaseTest < Assert::Context
6
- desc "NsOptions::Helper::Advisor"
7
- setup do
8
- @namespace = NsOptions::Namespace.new("something")
9
- @advisor = NsOptions::Helper::Advisor.new(@namespace)
10
- end
11
- subject{ @advisor }
12
-
13
- should have_accessors :namespace
14
- should have_instance_methods :is_this_ok?, :is_this_option_ok?, :is_this_sub_namespace_ok?,
15
- :is_already_defined?, :bad_methods, :not_recommended_methods, :bad_method_message,
16
- :duplicate_message, :not_recommended_method_message
17
-
18
- should "return methods that cannot be overwritten with a call to #bad_methods" do
19
- expected = [ :define, :namespace, :option, :options ].map(&:to_s).sort
20
- assert_equal(expected, subject.bad_methods.map(&:to_s).sort)
21
- end
22
- should "return methods that shouldn't be overwritten with a call to #not_recommended_methods" do
23
- expected = NsOptions::Namespace.instance_methods(false).map(&:to_sym)
24
- assert_equal(expected, subject.not_recommended_methods)
25
- end
26
- should "return if an option/namespace is defined with a call to #is_already_defined?" do
27
- assert_equal false, subject.is_already_defined?(:first)
28
- end
29
- end
30
-
31
- class BadOptionTest < BaseTest
32
- desc "with a bad option"
33
- setup do
34
- begin
35
- @advisor.is_this_option_ok?("namespace")
36
- rescue Exception => @exception
37
- end
38
- end
39
-
40
- should "raise an exception when an option matches one of the bad methods" do
41
- assert_kind_of NsOptions::Errors::InvalidName, @exception
42
- expected = subject.bad_method_message("option", "namespace")
43
- assert_equal expected, @exception.message
44
- end
45
- end
46
-
47
- class DuplicateOptionTest < BaseTest
48
- desc "with a duplicate option"
49
- setup do
50
- @namespace.option(:duplicate)
51
- @output = NsOptions::TestOutput.capture do
52
- @advisor.is_this_option_ok?("duplicate")
53
- end
54
- end
55
-
56
- should "output a message warning that they are re-defining an option" do
57
- expected = subject.duplicate_message("duplicate")
58
- assert_match expected, @output.string
59
- assert_match "From: ", @output.string
60
- end
61
-
62
- should "return true with a call to #is_already_defined?" do
63
- assert_equal true, subject.is_already_defined?(:duplicate)
64
- end
65
- end
66
-
67
- class NotRecommendedOptionTest < BaseTest
68
- desc "with a not recommended option"
69
- setup do
70
- @output = NsOptions::TestOutput.capture do
71
- @advisor.is_this_option_ok?("apply")
72
- end
73
- end
74
-
75
- should "output a message warning using the method name as an option name" do
76
- expected = subject.not_recommended_method_message("option", "apply")
77
- assert_match expected, @output.string
78
- assert_match "From: ", @output.string
79
- end
80
- end
81
-
82
- class NotRecommendedNamespaceTest < BaseTest
83
- desc "with a not recommended root namespace"
84
- setup do
85
- @output = NsOptions::TestOutput.capture do
86
- @advisor.is_this_namespace_ok?("options")
87
- end
88
- end
89
-
90
- should "output a message warning using the method name as a namespace name" do
91
- expected = subject.not_recommended_method_message("namespace", "options")
92
- assert_match expected, @output.string
93
- assert_match "From: ", @output.string
94
- end
95
- end
96
-
97
- class NotRecommendedSubNamespaceTest < BaseTest
98
- desc "with a not recommended sub namespace"
99
- setup do
100
- @output = NsOptions::TestOutput.capture do
101
- @advisor.is_this_sub_namespace_ok?("apply")
102
- end
103
- end
104
-
105
- should "output a message warning using the method name as a namespace name" do
106
- expected = subject.not_recommended_method_message("sub-namespace", "apply")
107
- assert_match expected, @output.string
108
- assert_match "From: ", @output.string
109
- end
110
- end
111
-
112
- class BadSubNamespaceTest < BaseTest
113
- desc "with a bad sub namespace"
114
- setup do
115
- begin
116
- @advisor.is_this_sub_namespace_ok?("options")
117
- rescue Exception => @exception
118
- end
119
- end
120
-
121
- should "raise an exception when a namespace matches one of the bad methods" do
122
- assert_kind_of NsOptions::Errors::InvalidName, @exception
123
- expected = subject.bad_method_message("sub-namespace", "options")
124
- assert_equal expected, @exception.message
125
- end
126
- end
127
-
128
- class DuplicateSubNamespaceTest < BaseTest
129
- desc "with a duplicate sub namespace"
130
- setup do
131
- @namespace.namespace(:duplicate)
132
- @output = NsOptions::TestOutput.capture do
133
- @advisor.is_this_sub_namespace_ok?("duplicate")
134
- end
135
- end
136
-
137
- should "output a message warning that they are re-defining a namespace" do
138
- expected = subject.duplicate_message("duplicate")
139
- assert_match expected, @output.string
140
- assert_match "From: ", @output.string
141
- end
142
-
143
- should "return true with a call to #is_already_defined?" do
144
- assert_equal true, subject.is_already_defined?(:duplicate)
145
- end
146
- end
147
-
148
- end
@@ -1,56 +0,0 @@
1
- require 'assert'
2
-
3
- module NsOptions::Helper
4
-
5
- class BaseTest < Assert::Context
6
- desc "NsOptions::Helper"
7
- setup do
8
- @module = NsOptions::Helper
9
- end
10
- subject{ @module }
11
-
12
- should have_instance_method :find_and_define_namespace
13
- should have_instance_method :find_and_define_option
14
- should have_instance_method :define_namespace_methods
15
- should have_instance_method :define_option_methods
16
- should have_instance_method :advisor
17
- should have_instance_method :define_root_namespace_methods
18
-
19
- should "return an instance of NsOptions::Helper::Advisor with a call to #advisor" do
20
- advisor = subject.advisor(NsOptions::Namespace.new("something"))
21
- assert_instance_of NsOptions::Helper::Advisor, advisor
22
- end
23
- end
24
-
25
- class FindAndDefineOptionTest < BaseTest
26
- desc "find_and_define_option method"
27
- setup do
28
- @namespace = NsOptions::Namespace.new("something")
29
- @option = @namespace.options.add(:anything)
30
- @result = @module.find_and_define_option(@namespace, @option.name)
31
- end
32
- subject{ @namespace }
33
-
34
- should "have defined reader/writer methods for the option and returned the option" do
35
- assert_respond_to @option.name, subject
36
- assert_respond_to "#{@option.name}=", subject
37
- assert_equal @option, @result
38
- end
39
- end
40
-
41
- class FindAndDefineNamespaceTest < BaseTest
42
- desc "find_and_define_namespace method"
43
- setup do
44
- @namespace = NsOptions::Namespace.new("something")
45
- @namespace.options.add_namespace(:else, "something:else")
46
- @result = @module.find_and_define_namespace(@namespace, :else)
47
- end
48
- subject{ @namespace }
49
-
50
- should "have defined reader method for the namespace and returned the namespace" do
51
- assert_respond_to :else, subject
52
- assert_equal subject.options.get_namespace(:else), @result
53
- end
54
- end
55
-
56
- end