cape 1.6.2 → 1.7.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 (72) hide show
  1. data/Gemfile +7 -6
  2. data/Guardfile +29 -10
  3. data/History.markdown +4 -0
  4. data/README.markdown +38 -9
  5. data/Rakefile +35 -8
  6. data/features/dsl/each_rake_task/{without_arguments.feature → unqualified.feature} +16 -37
  7. data/features/dsl/each_rake_task/{with_defined_namespace_argument.feature → with_defined_namespace.feature} +2 -13
  8. data/features/dsl/each_rake_task/{with_defined_task_argument.feature → with_defined_task.feature} +2 -6
  9. data/features/dsl/each_rake_task/with_undefined_task_or_namespace.feature +24 -0
  10. data/features/dsl/mirror_rake_tasks/inside_capistrano_namespace.feature +2 -2
  11. data/features/dsl/mirror_rake_tasks/{without_arguments.feature → unqualified.feature} +15 -11
  12. data/features/dsl/mirror_rake_tasks/with_cd.feature +63 -0
  13. data/features/dsl/mirror_rake_tasks/with_cd_and_environment_variables.feature +27 -0
  14. data/features/dsl/mirror_rake_tasks/{with_defined_namespace_argument.feature → with_defined_namespace.feature} +6 -8
  15. data/features/dsl/mirror_rake_tasks/{with_defined_task_argument.feature → with_defined_task.feature} +4 -4
  16. data/features/dsl/mirror_rake_tasks/{with_defined_task_argument_and_environment_variables.feature → with_defined_task_and_cd.feature} +8 -8
  17. data/features/dsl/mirror_rake_tasks/with_defined_task_and_cd_and_environment_variables.feature +46 -0
  18. data/features/dsl/mirror_rake_tasks/with_defined_task_and_environment_variables.feature +92 -0
  19. data/features/dsl/mirror_rake_tasks/with_defined_task_and_rename.feature +47 -0
  20. data/features/dsl/mirror_rake_tasks/with_defined_task_and_rename_and_cd.feature +49 -0
  21. data/features/dsl/mirror_rake_tasks/with_defined_task_and_rename_and_cd_and_environment_variables.feature +52 -0
  22. data/features/dsl/mirror_rake_tasks/with_defined_task_and_rename_and_environment_variables.feature +50 -0
  23. data/features/dsl/mirror_rake_tasks/with_defined_task_and_rename_and_valid_options.feature +49 -0
  24. data/features/dsl/mirror_rake_tasks/with_defined_task_and_rename_and_valid_options_and_cd.feature +51 -0
  25. data/features/dsl/mirror_rake_tasks/with_defined_task_and_rename_and_valid_options_and_cd_and_environment_variables.feature +54 -0
  26. data/features/dsl/mirror_rake_tasks/with_defined_task_and_rename_and_valid_options_and_environment_variables.feature +52 -0
  27. data/features/dsl/mirror_rake_tasks/with_defined_task_and_valid_options.feature +53 -6
  28. data/features/dsl/mirror_rake_tasks/{with_defined_task_and_valid_options_arguments_and_environment_variables.feature → with_defined_task_and_valid_options_and_cd.feature} +10 -8
  29. data/features/dsl/mirror_rake_tasks/with_defined_task_and_valid_options_and_cd_and_environment_variables.feature +48 -0
  30. data/features/dsl/mirror_rake_tasks/with_defined_task_and_valid_options_and_environment_variables.feature +98 -0
  31. data/features/dsl/mirror_rake_tasks/with_environment_variables.feature +61 -4
  32. data/features/dsl/mirror_rake_tasks/with_rename.feature +27 -0
  33. data/features/dsl/mirror_rake_tasks/with_rename_and_cd.feature +28 -0
  34. data/features/dsl/mirror_rake_tasks/with_rename_and_cd_and_environment_variables.feature +30 -0
  35. data/features/dsl/mirror_rake_tasks/with_rename_and_environment_variables.feature +29 -0
  36. data/features/dsl/mirror_rake_tasks/with_rename_and_valid_options.feature +28 -0
  37. data/features/dsl/mirror_rake_tasks/with_rename_and_valid_options_and_cd.feature +29 -0
  38. data/features/dsl/mirror_rake_tasks/with_rename_and_valid_options_and_cd_and_environment_variables.feature +31 -0
  39. data/features/dsl/mirror_rake_tasks/with_rename_and_valid_options_and_environment_variables.feature +30 -0
  40. data/features/dsl/mirror_rake_tasks/{with_undefined_argument.feature → with_undefined_task_or_namespace.feature} +3 -4
  41. data/features/dsl/mirror_rake_tasks/with_valid_options.feature +46 -0
  42. data/features/dsl/mirror_rake_tasks/{with_valid_options_argument.feature → with_valid_options_and_cd.feature} +7 -5
  43. data/features/dsl/mirror_rake_tasks/with_valid_options_and_cd_and_environment_variables.feature +28 -0
  44. data/features/dsl/mirror_rake_tasks/with_valid_options_and_environment_variables.feature +51 -0
  45. data/features/dsl/rake_executable.feature +2 -2
  46. data/features/step_definitions.rb +40 -0
  47. data/lib/cape.rb +2 -0
  48. data/lib/cape/capistrano.rb +59 -32
  49. data/lib/cape/capistrano_deprecated.rb +165 -0
  50. data/lib/cape/core_ext.rb +2 -0
  51. data/lib/cape/deprecation.rb +12 -0
  52. data/lib/cape/deprecation/base.rb +59 -0
  53. data/lib/cape/deprecation/capistrano_deprecated_define_rake_wrapper.rb +168 -0
  54. data/lib/cape/deprecation/dsl_deprecated_mirror_rake_tasks.rb +145 -0
  55. data/lib/cape/dsl.rb +49 -60
  56. data/lib/cape/dsl_deprecated.rb +157 -0
  57. data/lib/cape/hash_list.rb +2 -0
  58. data/lib/cape/recipe_definition.rb +103 -0
  59. data/lib/cape/recipe_definition_deprecated.rb +41 -0
  60. data/lib/cape/util.rb +2 -0
  61. data/lib/cape/version.rb +1 -1
  62. data/lib/cape/xterm.rb +326 -0
  63. data/spec/cape/deprecation/base_sharedspec.rb +18 -0
  64. data/spec/cape/deprecation/capistrano_deprecated_define_rake_wrapper_spec.rb +157 -0
  65. data/spec/cape/deprecation/dsl_deprecated_mirror_rake_tasks_spec.rb +153 -0
  66. data/spec/cape/dsl_deprecated_spec.rb +307 -0
  67. data/spec/cape/dsl_spec.rb +10 -43
  68. data/spec/cape/recipe_definition_spec.rb +53 -0
  69. data/spec/cape/xterm_spec.rb +72 -0
  70. metadata +97 -28
  71. data/features/dsl/each_rake_task/with_undefined_argument.feature +0 -53
  72. data/features/dsl/mirror_rake_tasks/with_valid_options_arguments_and_environment_variables.feature +0 -26
@@ -0,0 +1,157 @@
1
+ require 'cape/capistrano_deprecated'
2
+ require 'cape/deprecation/capistrano_deprecated_define_rake_wrapper'
3
+ require 'cape/deprecation/dsl_deprecated_mirror_rake_tasks'
4
+
5
+ module Cape
6
+
7
+ # Implements deprecated methods of {DSL}.
8
+ #
9
+ # @api private
10
+ module DSLDeprecated
11
+
12
+ # The stream to which deprecation messages are printed.
13
+ #
14
+ # @return [Deprecation::Base]
15
+ def deprecation
16
+ @deprecation ||= Deprecation::DSLDeprecatedMirrorRakeTasks.new
17
+ end
18
+
19
+ # Defines Rake tasks as Capistrano recipes.
20
+ #
21
+ # @overload mirror_rake_tasks(task_expression=nil)
22
+ # Defines Rake tasks as Capistrano recipes.
23
+ #
24
+ # @param [String, Symbol] task_expression the full name of a Rake task or
25
+ # namespace to filter
26
+ #
27
+ # @yield [env] a block that defines environment variables for the Rake
28
+ # task; optional
29
+ # @yieldparam [RecipeDefinitionDeprecated] env the environment variables to
30
+ # set before executing the
31
+ # Rake task
32
+ #
33
+ # @return [DSLDeprecated] the object
34
+ #
35
+ # @overload mirror_rake_tasks(capistrano_task_options={})
36
+ # Defines all Rake tasks as Capistrano recipes with options.
37
+ #
38
+ # @deprecated Use {RecipeDefinition#options} instead.
39
+ #
40
+ # @param [Hash] capistrano_task_options options to pass to the Capistrano
41
+ # +task+ method
42
+ #
43
+ # @yield [env] a block that defines environment variables for the Rake
44
+ # task; optional
45
+ # @yieldparam [RecipeDefinitionDeprecated] env the environment variables to
46
+ # set before executing the
47
+ # Rake task
48
+ #
49
+ # @return [DSLDeprecated] the object
50
+ #
51
+ # @overload mirror_rake_tasks(task_expression, capistrano_task_options={})
52
+ # Defines specific Rake tasks as Capistrano recipes with options.
53
+ #
54
+ # @deprecated Use {RecipeDefinition#options} instead.
55
+ #
56
+ # @param [String, Symbol] task_expression the full name of a Rake
57
+ # task or namespace to
58
+ # filter
59
+ # @param [Hash] capistrano_task_options options to pass to the
60
+ # Capistrano +task+ method
61
+ #
62
+ # @yield [env] a block that defines environment variables for the Rake
63
+ # task; optional
64
+ # @yieldparam [RecipeDefinitionDeprecated] env the environment variables to
65
+ # set before executing the
66
+ # Rake task
67
+ #
68
+ # @return [DSLDeprecated] the object
69
+ #
70
+ # @note Any parameters that the Rake tasks have are integrated via environment variables, since Capistrano does not support recipe parameters per se.
71
+ #
72
+ # @see http://github.com/capistrano/capistrano/blob/master/lib/capistrano/configuration/actions/invocation.rb#L99-L144 Valid Capistrano 'task' method options
73
+ #
74
+ # @example Mirroring all Rake tasks
75
+ # # config/deploy.rb
76
+ #
77
+ # require 'cape'
78
+ #
79
+ # Cape do
80
+ # mirror_rake_tasks
81
+ # end
82
+ #
83
+ # @example Mirroring some Rake tasks, but not others
84
+ # # config/deploy.rb
85
+ #
86
+ # require 'cape'
87
+ #
88
+ # Cape do
89
+ # # Create Capistrano recipes for the Rake task 'foo' and/or for the
90
+ # # tasks in the 'foo' namespace.
91
+ # mirror_rake_tasks :foo
92
+ # end
93
+ #
94
+ # @example Mirroring Rake tasks that require Capistrano recipe options and/or environment variables
95
+ # # config/deploy.rb
96
+ #
97
+ # require 'cape'
98
+ #
99
+ # Cape do
100
+ # # Display defined Rails routes on application server remote machines
101
+ # # only.
102
+ # mirror_rake_tasks :routes, :roles => :app
103
+ #
104
+ # # Execute database migration on application server remote machines
105
+ # # only, and set the 'RAILS_ENV' environment variable to the value of
106
+ # # the Capistrano variable 'rails_env'.
107
+ # mirror_rake_tasks 'db:migrate', :roles => :app do |env|
108
+ # env['RAILS_ENV'] = rails_env
109
+ # end
110
+ # end
111
+ #
112
+ # @example Mirroring Rake tasks into a Capistrano namespace
113
+ # # config/deploy.rb
114
+ #
115
+ # require 'cape'
116
+ #
117
+ # namespace :rake_tasks do
118
+ # Cape do |cape|
119
+ # cape.mirror_rake_tasks
120
+ # end
121
+ # end
122
+ #
123
+ # @api public
124
+ def mirror_rake_tasks(*arguments, &block)
125
+ arguments_count = arguments.length
126
+ options = arguments.last.is_a?(::Hash) ? arguments.pop.dup : {}
127
+ deprecation.task_expression = arguments.first
128
+ deprecation.options = options.dup
129
+ unless arguments.length <= 1
130
+ raise ::ArgumentError,
131
+ ("wrong number of arguments (#{arguments_count} for 0 or 1, " +
132
+ 'plus an options hash)')
133
+ end
134
+
135
+ task_expression = arguments.first
136
+ options[:binding] = binding
137
+ rake.each_task task_expression do |t|
138
+ deployment_library.define_rake_wrapper(t, options, &block)
139
+ end
140
+ deployment_library.deprecation.env.each do |name, value|
141
+ deprecation.env[name] = value
142
+ end
143
+ unless deprecation.options.empty? && deprecation.env.empty?
144
+ deprecation.write_formatted_message_to_stream
145
+ end
146
+ self
147
+ end
148
+
149
+ private
150
+
151
+ def new_capistrano(*arguments)
152
+ CapistranoDeprecated.new(*arguments)
153
+ end
154
+
155
+ end
156
+
157
+ end
@@ -7,6 +7,8 @@ module Cape
7
7
  #
8
8
  # This class exists because in Ruby v1.8.7 and earlier, Hash did not preserve
9
9
  # the insertion order of keys.
10
+ #
11
+ # @api private
10
12
  class HashList < ::Array
11
13
 
12
14
  # Constructs a new HashList using the specified _arguments_.
@@ -0,0 +1,103 @@
1
+ require 'cape/hash_list'
2
+
3
+ module Cape
4
+
5
+ # Determines how a Capistrano recipe will be defined.
6
+ class RecipeDefinition
7
+
8
+ # The remote directory from which Rake tasks will be executed.
9
+ #
10
+ # @overload cd
11
+ # The remote directory from which Rake tasks will be executed.
12
+ #
13
+ # @return [String, Proc] the value or a block that returns the value
14
+ #
15
+ # @overload cd(path)
16
+ # Sets the remote directory from which Rake tasks will be executed.
17
+ #
18
+ # @param [String, Proc] path the value or a callable object that returns
19
+ # the value
20
+ #
21
+ # @return [String, Proc] _path_
22
+ #
23
+ # @raise [ArgumentError] _path_ is a callable object that has parameters
24
+ #
25
+ # @overload cd(&block)
26
+ # Sets the remote directory from which Rake tasks will be executed.
27
+ #
28
+ # @yieldreturn [String] the value
29
+ #
30
+ # @return [String, Proc] _block_
31
+ #
32
+ # @raise [ArgumentError] _block_ has parameters
33
+ def cd(path=nil, &block)
34
+ if (cd = (path || block))
35
+ if cd.respond_to?(:arity)
36
+ case cd.arity
37
+ when -1
38
+ # Lambda: good
39
+ when 0
40
+ # Good
41
+ else
42
+ raise ::ArgumentError, "Must have 0 parameters but has #{cd.arity}"
43
+ end
44
+ end
45
+
46
+ @cd = cd
47
+ else
48
+ @cd
49
+ end
50
+ end
51
+
52
+ # A hash of remote environment variables.
53
+ #
54
+ # @return [HashList] the desired environment of the remote computer
55
+ def env
56
+ @env ||= HashList.new
57
+ end
58
+
59
+ # A hash of Capistrano recipe options to pass to the Capistrano +task+
60
+ # method.
61
+ #
62
+ # @return [HashList] the desired Capistrano recipe options
63
+ #
64
+ # @see http://github.com/capistrano/capistrano/blob/master/lib/capistrano/configuration/actions/invocation.rb#L99-L144 Valid Capistrano 'task' method options
65
+ def options
66
+ @options ||= HashList.new
67
+ end
68
+
69
+ # How Rake tasks will be named when they are mirrored as Capistrano recipes.
70
+ #
71
+ # @overload rename
72
+ # How Rake tasks will be named when they are mirrored as Capistrano
73
+ # recipes.
74
+ #
75
+ # @return [Proc] the block that generates a Capistrano recipe name from a
76
+ # Rake task name
77
+ #
78
+ # @overload rename(&block)
79
+ # Determines how Rake tasks will be named when they are mirrored as
80
+ # Capistrano recipes.
81
+ #
82
+ # @yield [task_name]
83
+ # @yieldparam task_name [String] the name of a Rake task
84
+ # @yieldreturn [String] a name for the Capistrano recipe
85
+ #
86
+ # @return [Proc] _block_
87
+ #
88
+ # @raise [ArgumentError] _block_ does not have exactly one parameter
89
+ def rename(&block)
90
+ if block
91
+ unless (block.arity == 1)
92
+ raise ::ArgumentError, "Must have 1 parameter but has #{block.arity}"
93
+ end
94
+
95
+ @rename = block
96
+ else
97
+ @rename
98
+ end
99
+ end
100
+
101
+ end
102
+
103
+ end
@@ -0,0 +1,41 @@
1
+ require 'cape/recipe_definition'
2
+
3
+ module Cape
4
+
5
+ # Implements {RecipeDefinition} with deprecated methods.
6
+ #
7
+ # @api private
8
+ class RecipeDefinitionDeprecated < RecipeDefinition
9
+
10
+ # The object in which deprecated API usage is recorded.
11
+ #
12
+ # @return [Deprecation::Base]
13
+ attr_reader :deprecation
14
+
15
+ # Constructs a new RecipeDefinitionDeprecated with the specified
16
+ # _deprecation_.
17
+ #
18
+ # @param [Deprecation::Base] deprecation the value of {#deprecation}
19
+ def initialize(deprecation)
20
+ @deprecation = deprecation
21
+ end
22
+
23
+ # Sets a remote environment variable.
24
+ #
25
+ # @param [String] name the name of a remote environment variable
26
+ # @param [String] value a value for the remote environment variable
27
+ #
28
+ # @return [RecipeDefinitionDeprecated] the object
29
+ #
30
+ # @deprecated Use {RecipeDefinition#env} instead.
31
+ #
32
+ # @api public
33
+ def []=(name, value)
34
+ env[name] = value
35
+ deprecation.env[name] = value
36
+ self
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -1,6 +1,8 @@
1
1
  module Cape
2
2
 
3
3
  # Provides utility functions.
4
+ #
5
+ # @api private
4
6
  module Util
5
7
 
6
8
  # Conditionally transforms the specified _noun_ into its plural form.
@@ -1,6 +1,6 @@
1
1
  module Cape
2
2
 
3
3
  # The version of Cape.
4
- VERSION = '1.6.2'
4
+ VERSION = '1.7.0'
5
5
 
6
6
  end
@@ -0,0 +1,326 @@
1
+ module Cape
2
+
3
+ # Formats output for an xterm console.
4
+ #
5
+ # Convenience class methods are made available dynamically that permit multiple
6
+ # formats to be applied.
7
+ #
8
+ # @example Apply bold and red-foreground formatting to a string
9
+ # Cape::XTerm.bold_and_red_foreground 'foo'
10
+ #
11
+ # @api private
12
+ module XTerm
13
+
14
+ # The xterm formatting codes.
15
+ FORMATS = {:bold => '1',
16
+ :underlined => '4',
17
+ :blinking => '5',
18
+ :inverse => '7',
19
+ :foreground_black => '30',
20
+ :foreground_red => '31',
21
+ :foreground_green => '32',
22
+ :foreground_yellow => '33',
23
+ :foreground_blue => '34',
24
+ :foreground_magenta => '35',
25
+ :foreground_cyan => '36',
26
+ :foreground_gray => '37',
27
+ :foreground_default => '39',
28
+ :background_black => '40',
29
+ :background_red => '41',
30
+ :background_green => '42',
31
+ :background_yellow => '43',
32
+ :background_blue => '44',
33
+ :background_magenta => '45',
34
+ :background_cyan => '46',
35
+ :background_gray => '47',
36
+ :background_default => '49'}
37
+
38
+ # @!method bold(string)
39
+ # Formats the specified _string_ in bold type.
40
+ #
41
+ # @param [String] string the string to format in bold type
42
+ #
43
+ # @return [String] the formatted _string_
44
+ #
45
+ # @!scope class
46
+ #
47
+ # @api private
48
+
49
+ # @!method underlined(string)
50
+ # Formats the specified _string_ in underlined type.
51
+ #
52
+ # @param [String] string the string to underline
53
+ #
54
+ # @return [String] the formatted _string_
55
+ #
56
+ # @!scope class
57
+ #
58
+ # @api private
59
+
60
+ # @!method blinking(string)
61
+ # Formats the specified _string_ in blinking type.
62
+ #
63
+ # @param [String] string the string to blink
64
+ #
65
+ # @return [String] the formatted _string_
66
+ #
67
+ # @!scope class
68
+ #
69
+ # @api private
70
+
71
+ # @!method inverse(string)
72
+ # Formats the specified _string_ in inverse colors.
73
+ #
74
+ # @param [String] string the string whose colors are to be inverted
75
+ #
76
+ # @return [String] the formatted _string_
77
+ #
78
+ # @!scope class
79
+ #
80
+ # @api private
81
+
82
+ # @!method foreground_black(string)
83
+ # Formats the specified _string_ in black.
84
+ #
85
+ # @param [String] string the string to color
86
+ #
87
+ # @return [String] the formatted _string_
88
+ #
89
+ # @!scope class
90
+ #
91
+ # @api private
92
+
93
+ # @!method foreground_red(string)
94
+ # Formats the specified _string_ in red.
95
+ #
96
+ # @param [String] string the string to color
97
+ #
98
+ # @return [String] the formatted _string_
99
+ #
100
+ # @!scope class
101
+ #
102
+ # @api private
103
+
104
+ # @!method foreground_green(string)
105
+ # Formats the specified _string_ in green.
106
+ #
107
+ # @param [String] string the string to color
108
+ #
109
+ # @return [String] the formatted _string_
110
+ #
111
+ # @!scope class
112
+ #
113
+ # @api private
114
+
115
+ # @!method foreground_yellow(string)
116
+ # Formats the specified _string_ in yellow.
117
+ #
118
+ # @param [String] string the string to color
119
+ #
120
+ # @return [String] the formatted _string_
121
+ #
122
+ # @!scope class
123
+ #
124
+ # @api private
125
+
126
+ # @!method foreground_blue(string)
127
+ # Formats the specified _string_ in blue.
128
+ #
129
+ # @param [String] string the string to color
130
+ #
131
+ # @return [String] the formatted _string_
132
+ #
133
+ # @!scope class
134
+ #
135
+ # @api private
136
+
137
+ # @!method foreground_magenta(string)
138
+ # Formats the specified _string_ in magenta.
139
+ #
140
+ # @param [String] string the string to color
141
+ #
142
+ # @return [String] the formatted _string_
143
+ #
144
+ # @!scope class
145
+ #
146
+ # @api private
147
+
148
+ # @!method foreground_cyan(string)
149
+ # Formats the specified _string_ in cyan.
150
+ #
151
+ # @param [String] string the string to color
152
+ #
153
+ # @return [String] the formatted _string_
154
+ #
155
+ # @!scope class
156
+ #
157
+ # @api private
158
+
159
+ # @!method foreground_gray(string)
160
+ # Formats the specified _string_ in gray.
161
+ #
162
+ # @param [String] string the string to color
163
+ #
164
+ # @return [String] the formatted _string_
165
+ #
166
+ # @!scope class
167
+ #
168
+ # @api private
169
+
170
+ # @!method foreground_default(string)
171
+ # Formats the specified _string_ in the default foreground color.
172
+ #
173
+ # @param [String] string the string to color
174
+ #
175
+ # @return [String] the formatted _string_
176
+ #
177
+ # @!scope class
178
+ #
179
+ # @api private
180
+
181
+ # @!method background_black(string)
182
+ # Formats the specified _string_ with a black background.
183
+ #
184
+ # @param [String] string the string to color
185
+ #
186
+ # @return [String] the formatted _string_
187
+ #
188
+ # @!scope class
189
+ #
190
+ # @api private
191
+
192
+ # @!method background_red(string)
193
+ # Formats the specified _string_ with a red background.
194
+ #
195
+ # @param [String] string the string to color
196
+ #
197
+ # @return [String] the formatted _string_
198
+ #
199
+ # @!scope class
200
+ #
201
+ # @api private
202
+
203
+ # @!method background_green(string)
204
+ # Formats the specified _string_ with a green background.
205
+ #
206
+ # @param [String] string the string to color
207
+ #
208
+ # @return [String] the formatted _string_
209
+ #
210
+ # @!scope class
211
+ #
212
+ # @api private
213
+
214
+ # @!method background_yellow(string)
215
+ # Formats the specified _string_ with a yellow background.
216
+ #
217
+ # @param [String] string the string to color
218
+ #
219
+ # @return [String] the formatted _string_
220
+ #
221
+ # @!scope class
222
+ #
223
+ # @api private
224
+
225
+ # @!method background_blue(string)
226
+ # Formats the specified _string_ with a blue background.
227
+ #
228
+ # @param [String] string the string to color
229
+ #
230
+ # @return [String] the formatted _string_
231
+ #
232
+ # @!scope class
233
+ #
234
+ # @api private
235
+
236
+ # @!method background_magenta(string)
237
+ # Formats the specified _string_ with a magenta background.
238
+ #
239
+ # @param [String] string the string to color
240
+ #
241
+ # @return [String] the formatted _string_
242
+ #
243
+ # @!scope class
244
+ #
245
+ # @api private
246
+
247
+ # @!method background_cyan(string)
248
+ # Formats the specified _string_ with a cyan background.
249
+ #
250
+ # @param [String] string the string to color
251
+ #
252
+ # @return [String] the formatted _string_
253
+ #
254
+ # @!scope class
255
+ #
256
+ # @api private
257
+
258
+ # @!method background_gray(string)
259
+ # Formats the specified _string_ with a gray background.
260
+ #
261
+ # @param [String] string the string to color
262
+ #
263
+ # @return [String] the formatted _string_
264
+ #
265
+ # @!scope class
266
+ #
267
+ # @api private
268
+
269
+ # @!method background_default(string)
270
+ # Formats the specified _string_ in the default background color.
271
+ #
272
+ # @param [String] string the string to color
273
+ #
274
+ # @return [String] the formatted _string_
275
+ #
276
+ # @!scope class
277
+ #
278
+ # @api private
279
+
280
+ # Applies the specified _formats_ to _string_.
281
+ #
282
+ # @param [String] string the string to format
283
+ # @param [Array of Symbol] formats the formats to apply
284
+ #
285
+ # @return [String] the formatted _string_
286
+ def self.format(string, *formats)
287
+ formatting_codes = formats.collect do |f|
288
+ unless FORMATS.key?(f)
289
+ raise ::ArgumentError, "Unrecognized format #{f.inspect}"
290
+ end
291
+
292
+ FORMATS[f]
293
+ end
294
+
295
+ return string if formatting_codes.empty? || string.nil?
296
+
297
+ "\e[#{formatting_codes.join ';'}m#{string}\e[0m"
298
+ end
299
+
300
+ private
301
+
302
+ def self.method_missing(method, *arguments, &block)
303
+ unless respond_to_missing?(method, false)
304
+ return super(method, *arguments, &block)
305
+ end
306
+
307
+ formats = method.to_s.split('_and_').collect(&:to_sym)
308
+ format(*(arguments + formats))
309
+ end
310
+
311
+ def self.respond_to_missing?(method, include_private)
312
+ formats = method.to_s.split('_and_').collect(&:to_sym)
313
+ formats.all? do |f|
314
+ FORMATS.key? f
315
+ end
316
+ end
317
+
318
+ if RUBY_VERSION <= '1.8.7'
319
+ def self.respond_to?(method, include_private=false)
320
+ respond_to_missing? method, include_private
321
+ end
322
+ end
323
+
324
+ end
325
+
326
+ end