Module: Psychgus::Stylables::CapStylable
- Includes:
- Psychgus::Styler
- Included in:
- Psychgus::Stylers::CapStyler
- Defined in:
- lib/psychgus/stylables.rb
Overview
A Capitalizer for Scalars.
Constant Summary
Constants included from Psychgus::Styler
Instance Attribute Summary collapse
-
#delim ⇒ String, Regexp
readonly
The delimiter to split on.
-
#each_word ⇒ true, false
Whether to capitalize each word separated by #delim.
-
#new_delim ⇒ nil, String
The replacement for each #delim if not nil.
Instance Method Summary collapse
-
#cap_word(word) ⇒ String
Capitalize an individual word (not words).
- #initialize(each_word: true, new_delim: nil, delim: /[\s_\-]/, **kargs) ⇒ Object
-
#style_scalar(sniffer, node) ⇒ Object
Capitalize
node.value
.
Methods included from Psychgus::Styler
#style, #style_alias, #style_document, #style_mapping, #style_sequence, #style_stream
Instance Attribute Details
#delim ⇒ String, Regexp (readonly)
Returns the delimiter to split on.
77 78 79 |
# File 'lib/psychgus/stylables.rb', line 77 def delim @delim end |
#each_word ⇒ true, false
Returns whether to capitalize each word separated by #delim.
78 79 80 |
# File 'lib/psychgus/stylables.rb', line 78 def each_word @each_word end |
#new_delim ⇒ nil, String
Returns the replacement for each #delim if not nil.
79 80 81 |
# File 'lib/psychgus/stylables.rb', line 79 def new_delim @new_delim end |
Instance Method Details
#cap_word(word) ⇒ String
Capitalize an individual word (not words).
This method can safely be overridden with a new implementation.
100 101 102 103 104 105 106 107 |
# File 'lib/psychgus/stylables.rb', line 100 def cap_word(word) return word if word.nil?() || word.empty?() # Already capitalized, good for all-capitalized words, like 'BBQ' return word if word[0] == word[0].upcase() return word.capitalize() end |
#initialize(each_word: true, new_delim: nil, delim: /[\s_\-]/, **kargs) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/psychgus/stylables.rb', line 85 def initialize(each_word: true,new_delim: nil,delim: /[\s_\-]/,**kargs) delim = Regexp.quote(delim.to_s()) unless delim.is_a?(Regexp) @delim = Regexp.new("(#{delim.to_s()})") @each_word = each_word @new_delim = new_delim end |
#style_scalar(sniffer, node) ⇒ Object
Capitalize node.value
.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/psychgus/stylables.rb', line 113 def style_scalar(sniffer,node) if !@each_word || node.value.nil?() || node.value.empty?() node.value = cap_word(node.value) return end is_delim = false node.value = node.value.split(@delim).map() do |v| if is_delim v = @new_delim unless @new_delim.nil?() else v = cap_word(v) end is_delim = !is_delim v end.join() end |