Class: Rake::ToolkitProgram::HelpStyling
- Inherits:
-
Object
- Object
- Rake::ToolkitProgram::HelpStyling
- Defined in:
- lib/rake/toolkit_program/help_styling.rb
Overview
An object that captures styling rules for a CLI program
This class defines several methods that either configure a styling transformation or apply the configured transformation, depending on the argument type. These methods are defined with .define_style for consistency.
Each “style” method (e.g. #title) “learns” how to apply its style if passed anything responding to #to_proc and applies its current style transformation if passed a string.
Constant Summary collapse
- IDENTITY =
-> (s) {s}
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ HelpStyling
constructor
A new instance of HelpStyling.
Constructor Details
#initialize ⇒ HelpStyling
Returns a new instance of HelpStyling.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rake/toolkit_program/help_styling.rb', line 38 def initialize super begin require 'colorize' rescue LoadError title ->(s) {"*** #{s} ***"} else title ->(s) {"*** #{s} ***".light_white.bold.on_blue} code ->(s) {s.bold} param ->(s) {s.italic} error_marker ->(s) {s.bold.red.on_black} end end |
Class Method Details
.define_style(*names) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rake/toolkit_program/help_styling.rb', line 52 def self.define_style(*names) names.each do |name| vname = "@#{name}".to_sym define_method(name) do |s| case when String === s then (instance_variable_get(vname) || IDENTITY)[s] when s.respond_to?(:to_proc) then instance_variable_set(vname, s.to_proc) else raise ArgumentError, "\##{name} accepts a String or Proc" end end end end |