Class: String

Inherits:
Object show all
Defined in:
lib/doing/colors.rb,
lib/doing/good.rb,
lib/doing/normalize.rb

Overview

String to symbol conversion

Direct Known Subclasses

Doing::TemplateString

Instance Method Summary collapse

Instance Method Details

#good?Boolean

Tests if object is nil or empty

Returns:

  • (Boolean)

    true if object is defined and has content



24
25
26
# File 'lib/doing/good.rb', line 24

def good?
  !strip.empty?
end

#normalize_age(default = :newest) ⇒ Symbol

Convert an age string to a qualified type

Returns:

  • (Symbol)

    :oldest or :newest



34
35
36
37
38
39
40
41
42
43
# File 'lib/doing/normalize.rb', line 34

def normalize_age(default = :newest)
  case self
  when /^o/i
    :oldest
  when /^n/i
    :newest
  else
    default
  end
end

#normalize_age!(default = :newest) ⇒ Object

See Also:



46
47
48
# File 'lib/doing/normalize.rb', line 46

def normalize_age!(default = :newest)
  replace normalize_age(default)
end

#normalize_bool(default = :and) ⇒ Object

Convert a boolean string to a symbol

Returns:

  • Symbol :and, :or, or :not



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/doing/normalize.rb', line 98

def normalize_bool(default = :and)
  case self
  when /(and|all)/i
    :and
  when /(any|or)/i
    :or
  when /(not|none)/i
    :not
  when /^p/i
    :pattern
  else
    default.is_a?(Symbol) ? default : default.normalize_bool
  end
end

#normalize_bool!(default = :and) ⇒ Object

See Also:



114
115
116
# File 'lib/doing/normalize.rb', line 114

def normalize_bool!(default = :and)
  replace normalize_bool(default)
end

#normalize_case(default = :smart) ⇒ Object

Convert a case sensitivity string to a symbol

Returns:

  • Symbol :smart, :sensitive, :ignore



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/doing/normalize.rb', line 75

def normalize_case(default = :smart)
  case self
  when /^(c|sens)/i
    :sensitive
  when /^i/i
    :ignore
  when /^s/i
    :smart
  else
    default.is_a?(Symbol) ? default : default.normalize_case
  end
end

#normalize_case!(default = :smart) ⇒ Object

See Also:



89
90
91
# File 'lib/doing/normalize.rb', line 89

def normalize_case!(default = :smart)
  replace normalize_case(default)
end

#normalize_colorString

Normalize a color name, removing underscores, replacing "bright" with "bold", and converting bgbold to boldbg

Returns:

  • (String)

    Normalized color name



116
117
118
# File 'lib/doing/colors.rb', line 116

def normalize_color
  gsub(/_/, '').sub(/bright/i, 'bold').sub(/bgbold/, 'boldbg')
end

#normalize_matching(default = :pattern) ⇒ Object

Convert a matching configuration string to a symbol

Parameters:

  • default (Symbol) (defaults to: :pattern)

    the default matching type to return if the string doesn't match a known symbol

Returns:

  • Symbol :fuzzy, :pattern, :exact



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/doing/normalize.rb', line 126

def normalize_matching(default = :pattern)
  case self
  when /^f/i
    :fuzzy
  when /^p/i
    :pattern
  when /^e/i
    :exact
  else
    default.is_a?(Symbol) ? default : default.normalize_matching
  end
end

#normalize_matching!(default = :pattern) ⇒ Object



140
141
142
# File 'lib/doing/normalize.rb', line 140

def normalize_matching!(default = :pattern)
  replace normalize_bool(default)
end

#normalize_order(default = :asc) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/doing/normalize.rb', line 59

def normalize_order(default = :asc)
  case self
  when /^a/i
    :asc
  when /^d/i
    :desc
  else
    default
  end
end

#normalize_order!(default = :asc) ⇒ Symbol

Convert a sort order string to a qualified type

Returns:



55
56
57
# File 'lib/doing/normalize.rb', line 55

def normalize_order!(default = :asc)
  replace normalize_order(default)
end

#normalize_tag_sort(default = :name) ⇒ Symbol

Convert tag sort string to a qualified type

Returns:



13
14
15
16
17
18
19
20
21
22
# File 'lib/doing/normalize.rb', line 13

def normalize_tag_sort(default = :name)
  case self
  when /^n/i
    :name
  when /^t/i
    :time
  else
    default
  end
end

#normalize_tag_sort!(default = :name) ⇒ Object



25
26
27
# File 'lib/doing/normalize.rb', line 25

def normalize_tag_sort!(default = :name)
  replace normalize_tag_sort(default)
end

#normalize_triggerString

Adds ?: to any parentheticals in a regular expression to avoid match groups

Returns:

  • (String)

    modified regular expression



150
151
152
# File 'lib/doing/normalize.rb', line 150

def normalize_trigger
  gsub(/\((?!\?:)/, '(?:').downcase
end

#normalize_trigger!Object

See Also:



155
156
157
# File 'lib/doing/normalize.rb', line 155

def normalize_trigger!
  replace normalize_trigger
end

#validate_colorString

Extract the longest valid %color name from a string.

Allows %colors to bleed into other text and still be recognized, e.g. %greensomething still finds %green.

Returns:

  • (String)

    a valid color name



98
99
100
101
102
103
104
105
106
107
# File 'lib/doing/colors.rb', line 98

def validate_color
  valid_color = nil
  compiled = ''
  normalize_color.split('').each do |char|
    compiled += char
    valid_color = compiled if Color.attributes.include?(compiled.to_sym)
  end

  valid_color
end