Module: NRSER::RSpex::Format
- Defined in:
- lib/nrser/rspex/format.rb
Overview
String formatting utilities.
Class Method Summary collapse
- .bold(string) ⇒ Object
- .code(string) ⇒ Object
- .description(*parts, type: nil) ⇒ String
- .esc_seq_bold(string) ⇒ Object
- .esc_seq_italic(string) ⇒ Object
- .italic(string) ⇒ Object
- .md_code_quote(string) ⇒ Object
- .mean_streak ⇒ Object
- .method_name?(string) ⇒ Boolean
- .pastel ⇒ Object
- .pathname(pn) ⇒ Object
- .prepend_type(type, description) ⇒ return_type
- .rspec_syntax_highlighter ⇒ Object
-
.unicode_bold(string) ⇒ String
Bold a string via “Unicode Math Bold” substitution.
-
.unicode_italic(string) ⇒ String
Italicize a string via “Unicode Math Italic” substitution.
Class Method Details
.bold(string) ⇒ Object
71 72 73 |
# File 'lib/nrser/rspex/format.rb', line 71 def self.bold string public_send "#{ RSpec.configuration.x_style }_#{ __method__ }", string end |
.code(string) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/nrser/rspex/format.rb', line 102 def self.code string if method_name? string pastel.bold.blue string else rspec_syntax_highlighter.highlight string.lines end end |
.description(*parts, type: nil) ⇒ String
TODO:
Document format method.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/nrser/rspex/format.rb', line 170 def self.description *parts, type: nil parts. flat_map { |part| if part.respond_to? :to_desc desc = part.to_desc if desc.empty? '' else md_code_quote desc end else case part when Module mod = part name_desc = if mod.anonymous? "(anonymous #{ part.class })" else md_code_quote mod.name end [name_desc, description( mod.source_location )] when NRSER::Meta::Source::Location if part.valid? "(#{ NRSER::RSpex.dot_rel_path( part.file ) }:#{ part.line })" else '' end when String part when Pathname pathname part when NRSER::Message [part.symbol, part.args]. map( &NRSER::RSpex.method( :short_s ) ).join( ', ' ) else NRSER::RSpex.short_s part end end }. join( ' ' ). squish. thru { |description| prepend_type type, mean_streak.render( description ) } end |
.esc_seq_bold(string) ⇒ Object
56 57 58 |
# File 'lib/nrser/rspex/format.rb', line 56 def self.esc_seq_bold string pastel.bold string end |
.esc_seq_italic(string) ⇒ Object
44 45 46 |
# File 'lib/nrser/rspex/format.rb', line 44 def self.esc_seq_italic string pastel.italic string end |
.italic(string) ⇒ Object
49 50 51 |
# File 'lib/nrser/rspex/format.rb', line 49 def self.italic string public_send "#{ RSpec.configuration.x_style }_#{ __method__ }", string end |
.md_code_quote(string) ⇒ Object
111 112 113 114 |
# File 'lib/nrser/rspex/format.rb', line 111 def self.md_code_quote string quote = '`' * ((string.scan( /`+/ ).map( &:length ).max || 0) + 1) "#{ quote }#{ string }#{ quote }" end |
.mean_streak ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/nrser/rspex/format.rb', line 17 def self.mean_streak @mean_streak ||= NRSER::MeanStreak.new do |ms| ms.render_type :emph do |doc, node| italic doc.render_children( node ) end ms.render_type :strong do |doc, node| bold doc.render_children( node ) end ms.render_type :code do |doc, node| code node.string_content end end end |
.method_name?(string) ⇒ Boolean
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/nrser/rspex/format.rb', line 84 def self.method_name? string # Must start with `#` or `.` return false unless ['#', '.'].any? { |c| string[0] == c } name = string[1..-1] case name when '!', '~', '+', '**', '-', '*', '/', '%', '+', '-', '<<', '>>', '&', '|', '^', '<', '<=', '>=', '>', '==', '===', '!=', '=~', '!~', '<=>', '[]', /\A[a-zA-Z_][a-zA-Z0-9_]*(?:\?|\!|=)?/ true else false end end |
.pastel ⇒ Object
12 13 14 |
# File 'lib/nrser/rspex/format.rb', line 12 def self.pastel @pastel ||= Pastel.new end |
.pathname(pn) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/nrser/rspex/format.rb', line 138 def self.pathname pn if pn.absolute? rel = pn.relative_path_from Pathname.getwd if rel.split( File::SEPARATOR ).first == '..' File.join '.', rel else pn.to_s end else if pn.exist? File.join '.', pn else lib_pn = Pathname.getwd / 'lib' / pn if lib_pn.exist? File.join '.', lib_pn.relative_path_from( Pathname.getwd ) else pn.to_s end end end end |
.prepend_type(type, description) ⇒ return_type
TODO:
Document format_type method.
Returns @todo Document return value.
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/nrser/rspex/format.rb', line 125 def self.prepend_type type, description return description if type.nil? prefixes = RSpec.configuration.x_type_prefixes prefix = pastel.magenta( prefixes[type] || i( type.to_s.upcase.gsub('_', ' ') ) ) "#{ prefix } #{ description }" end |
.rspec_syntax_highlighter ⇒ Object
78 79 80 81 |
# File 'lib/nrser/rspex/format.rb', line 78 def self.rspec_syntax_highlighter @rspec_syntax_highlighter ||= \ RSpec::Core::Formatters::SyntaxHighlighter.new RSpec.configuration end |