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.
62 63 64 |
# File 'lib/psychgus/stylables.rb', line 62 def delim @delim end |
#each_word ⇒ true, false
Returns whether to capitalize each word separated by #delim.
63 64 65 |
# File 'lib/psychgus/stylables.rb', line 63 def each_word @each_word end |
#new_delim ⇒ nil, String
Returns the replacement for each #delim if not nil.
64 65 66 |
# File 'lib/psychgus/stylables.rb', line 64 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.
85 86 87 88 89 90 91 92 |
# File 'lib/psychgus/stylables.rb', line 85 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
70 71 72 73 74 75 76 |
# File 'lib/psychgus/stylables.rb', line 70 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})") @each_word = each_word @new_delim = new_delim end |
#style_scalar(sniffer, node) ⇒ Object
Capitalize node.value
.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/psychgus/stylables.rb', line 98 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 |