Class: String

Inherits:
Object
  • Object
show all
Includes:
Doing::Color
Defined in:
lib/doing/string.rb,
lib/doing/string_chronify.rb

Overview

Chronify methods for strings

Constant Summary

Constants included from Doing::Color

Doing::Color::ATTRIBUTES, Doing::Color::ATTRIBUTE_NAMES, Doing::Color::COLORED_REGEXP

Instance Method Summary collapse

Methods included from Doing::Color

attributes, coloring?, #support?

Instance Method Details

#add_tags(tags, remove: false) ⇒ Object

#add_tags!(tags, remove: false) ⇒ Object

#cap_firstObject

Capitalize on the first character on string

Returns:

  • Capitalized string

#chronify(**options) ⇒ DateTime

Converts input string into a Time object when input takes on the following formats: - interval format e.g. '1d2h30m', '45m' etc. - a semantic phrase e.g. 'yesterday 5:30pm' - a strftime e.g. '2016-03-15 15:32:04 PDT'

Parameters:

  • options

    Additional options

Options Hash (**options):

  • :future (Boolean)

    assume future date (default: false)

  • :guess (Symbol)

    :begin or :end to assume beginning or end of arbitrary time range

Returns:

  • (DateTime)

    result

Raises:

  • (InvalidTimeExpression)

#chronify_qtyInteger

Converts simple strings into seconds that can be added to a Time object

Input string can be HH:MM or XX[dhm][XXhm][XXm]

Returns:

  • (Integer)

    seconds

#clean_unlinked_urlsObject

Clean up unlinked

#compressObject

Compress multiple spaces to single space

#compress!Object

#dedup_tagsObject

#dedup_tags!Object

Remove duplicate tags, leaving only first occurrence

Returns:

  • Deduplicated string

#highlight_tags(color = 'yellow') ⇒ String

Colorize @tags with ANSI escapes

Parameters:

  • color (String) (defaults to: 'yellow')

    color (see #Color)

Returns:

  • (String)

    string with @tags highlighted

#highlight_tags!(color = 'yellow') ⇒ Object

Parameters:

  • color (String) (defaults to: 'yellow')

    color (see #Color)

#ignore?Boolean

Test if line should be ignored

Returns:

  • (Boolean)

    line is empty or comment

#is_rx?Boolean

Determines if receiver is surrounded by slashes or starts with single quote

Returns:

  • (Boolean)

    True if regex, False otherwise.

#last_colorString

Returns the last escape sequence from a string.

Actually returns all escape codes, with the assumption that the result of inserting them will generate the same color as was set at the end of the string. Because you can send modifiers like dark and bold separate from color codes, only using the last code may not render the same style.

Returns:

  • (String)

    All escape codes in string

Turn raw urls into HTML links

Parameters:

  • opt (Hash)

    Additional Options

#normalize_bool(default = :and) ⇒ Object

#normalize_bool!(default = :and) ⇒ Object

Convert a boolean string to a symbol

Returns:

  • Symbol :and, :or, or :not

#normalize_case(default = :smart) ⇒ Object

#normalize_case!Object

Convert a case sensitivity string to a symbol

Returns:

  • Symbol :smart, :sensitive, :ignore

#normalize_order(default = 'asc') ⇒ Object

#normalize_order!(default = 'asc') ⇒ String

Convert a sort order string to a qualified type

Returns:

  • (String)

    'asc' or 'desc'

#normalize_triggerObject

#normalize_trigger!Object

Remove formatting

#replace_qualified_urls(**options) ⇒ Object

Replace qualified urls

#set_type(kind = nil) ⇒ Object

#tag(tag, value: nil, remove: false, rename_to: nil, regex: false, single: false, force: false) ⇒ String

Add, rename, or remove a tag

Parameters:

  • tag

    The tag

  • value (String) (defaults to: nil)

    Value for tag (@tag(value))

  • remove (Boolean) (defaults to: false)

    Remove the tag instead of adding

  • rename_to (String) (defaults to: nil)

    Replace tag with this tag

  • regex (Boolean) (defaults to: false)

    Tag is regular expression

  • single (Boolean) (defaults to: false)

    Operating on a single item (for logging)

  • force (Boolean) (defaults to: false)

    With rename_to, add tag if it doesn't exist

Returns:

  • (String)

    The string with modified tags

#tag!(tag, **options) ⇒ Object

Add, rename, or remove a tag in place

See Also:

#to_rx(distance: 3, case_type: :smart) ⇒ Regexp

Convert string to fuzzy regex. Characters in words can be separated by up to distance characters in haystack, spaces indicate unlimited distance.

Examples:

"this word".to_rx(2) => /t.{0,3}h.{0,3}i.{0,3}s.{0,3}.*?w.{0,3}o.{0,3}r.{0,3}d/

Parameters:

  • distance (Integer) (defaults to: 3)

    Allowed distance between characters

  • case_type (defaults to: :smart)

    The case type

Returns:

  • (Regexp)

    Regex pattern

#to_tagsObject

#truncate(len, ellipsis: '...') ⇒ Object

Truncate to nearest word

Parameters:

  • len

    The length

#truncate!(len, ellipsis: '...') ⇒ Object

#truncmiddle(len, ellipsis: '...') ⇒ Object

Truncate string in the middle

Parameters:

  • len

    The length

  • ellipsis (defaults to: '...')

    The ellipsis

#truncmiddle!(len, ellipsis: '...') ⇒ Object

#truthy?Boolean

Test string for truthiness (0, "f", "false", "n", "no" all return false, case insensitive, otherwise true)

Returns:

  • (Boolean)

    String is truthy

#uncolorObject

Remove color escape codes

Returns:

  • clean string

#uncolor!Object

#wrap(len, pad: 0, indent: ' ', offset: 0, prefix: '', color: '', after: '', reset: '') ⇒ Object

Wrap string at word breaks, respecting tags

Parameters:

  • len (Integer)

    The length

  • offset (Integer) (defaults to: 0)

    (Optional) The width to pad each subsequent line

  • prefix (String) (defaults to: '')

    (Optional) A prefix to add to each line