Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/mumuki/domain/extensions/string.rb,
lib/mumuki/domain/extensions/string.rb,
app/models/concerns/submittable/solvable.rb

Overview

The nil-safe affable pipeline goes as follow:

i18n > markdownified > sanitized > affable

Where:

* i18n: translates to current locale
* markdownified: interpretes markdown in message and generates HTML
* sanitized: sanitizes results HTML
* affable: changes structure to hide low level details

Other classes may polymorphically implement their own markdownified, sanitized and affable methods with similar semantics to extend this pipeline to non-strings

Instance Method Summary collapse

Instance Method Details

#affableObject

Creates a humman representation - but not necessary UI - representation of this string by interpreting its markdown as a one-liner and sanitizing it



49
50
51
# File 'lib/mumuki/domain/extensions/string.rb', line 49

def affable
  markdownified(one_liner: true).sanitized
end

#ensure_newlineObject

Adds a newline character unless this string is empty or already ends with a newline See unix.stackexchange.com/a/18789



6
7
8
# File 'lib/mumuki/domain/extensions/string.rb', line 6

def ensure_newline
  empty? || ends_with?("\n") ? self : self + "\n"
end

#file_extensionObject



26
27
28
# File 'lib/mumuki/domain/extensions/string.rb', line 26

def file_extension
  File.extname(self).delete '.'
end

#friendlishObject



10
11
12
13
14
15
16
# File 'lib/mumuki/domain/extensions/string.rb', line 10

def friendlish
  I18n.transliterate(self).
    downcase.
    gsub(/[^0-9a-z ]/, '').
    squish.
    gsub(' ', '-')
end

#markdown_paragraphsObject



18
19
20
# File 'lib/mumuki/domain/extensions/string.rb', line 18

def markdown_paragraphs
  split(/\n\s*\n/)
end

#markdownified(**options) ⇒ Object

Interprets the markdown on this string, and converts it into HTML



54
55
56
# File 'lib/mumuki/domain/extensions/string.rb', line 54

def markdownified(**options)
  Mumukit::ContentType::Markdown.to_html self, options
end

#normalize_whitespacesObject



22
23
24
# File 'lib/mumuki/domain/extensions/string.rb', line 22

def normalize_whitespaces
  gsub(/([^[:ascii:]])/) { $1.blank? ? ' ' : $1 }
end

#randomize_with(randomizer, seed) ⇒ Object



63
64
65
# File 'lib/mumuki/domain/extensions/string.rb', line 63

def randomize_with(randomizer, seed)
  randomizer.randomize!(self, seed)
end

#sanitizedObject

Sanitizes this string, escaping unsafe HTML sequences



59
60
61
# File 'lib/mumuki/domain/extensions/string.rb', line 59

def sanitized
  Mumukit::ContentType::Sanitizer.sanitize self
end

#to_mumuki_solution(language) ⇒ Object



26
27
28
# File 'app/models/concerns/submittable/solvable.rb', line 26

def to_mumuki_solution(language)
  Mumuki::Domain::Submission::Solution.new content: normalize_whitespaces
end