Module: Psychgus::Stylables::CapStylable

Includes:
Psychgus::Styler
Included in:
Psychgus::Stylers::CapStyler
Defined in:
lib/psychgus/stylables.rb

Overview

A Capitalizer for Scalars.

Examples:

require 'psychgus'

data = {
  'eggs' => [
    'omelette',
    'BBQ eggs',
    'hard-boiled eggs',
    'soft_boiled eggs',
    'fried@eggs'
]}

seq_flow = Psychgus::SeqFlowStyler.new

puts data.to_yaml(stylers: [Psychgus::CapStyler.new,seq_flow])

# Output:
# ---
# Eggs: [Omelette, BBQ Eggs, Hard-Boiled Eggs, Soft_Boiled Eggs, Fried@eggs]

puts data.to_yaml(stylers: [Psychgus::CapStyler.new(each_word: false),seq_flow])

# Output:
# ---
# Eggs: [Omelette, BBQ eggs, Hard-boiled eggs, Soft_boiled eggs, Fried@eggs]

puts data.to_yaml(stylers: [Psychgus::CapStyler.new(new_delim: '(o)'),seq_flow])

# Output:
# ---
# Eggs: [Omelette, BBQ(o)Eggs, Hard(o)Boiled(o)Eggs, Soft(o)Boiled(o)Eggs, Fried@eggs]

class Cappie
  include Psychgus::CapStylable

  def cap_word(word)
    return 'bbq' if word.casecmp('BBQ') == 0

    super(word)
  end
end

puts data.to_yaml(stylers: [Cappie.new(new_delim: '*',delim: /[\s@]/),seq_flow])

# Output:
# ---
# Eggs: [Omelette, bbq*Eggs, Hard-boiled*Eggs, Soft_boiled*Eggs, Fried*Eggs]

See Also:

Since:

  • 1.2.0

Constant Summary

Constants included from Psychgus::Styler

Psychgus::Styler::EMPTY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Psychgus::Styler

#style, #style_alias, #style_document, #style_mapping, #style_sequence, #style_stream

Instance Attribute Details

#delimString, Regexp (readonly)

Returns the delimiter to split on.

Returns:

  • (String, Regexp)

    the delimiter to split on

Since:

  • 1.2.0



77
78
79
# File 'lib/psychgus/stylables.rb', line 77

def delim
  @delim
end

#each_wordtrue, false

Returns whether to capitalize each word separated by #delim.

Returns:

  • (true, false)

    whether to capitalize each word separated by #delim

Since:

  • 1.2.0



78
79
80
# File 'lib/psychgus/stylables.rb', line 78

def each_word
  @each_word
end

#new_delimnil, String

Returns the replacement for each #delim if not nil.

Returns:

  • (nil, String)

    the replacement for each #delim if not nil

Since:

  • 1.2.0



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.

Parameters:

  • word (nil, String)

    the word to capitalize

Returns:

  • (String)

    the capitalized word

Since:

  • 1.2.0



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

Parameters:

  • each_word (true, false) (defaults to: true)

    whether to capitalize each word separated by delim

  • new_delim (nil, String) (defaults to: nil)

    the replacement for each delim if not nil

  • delim (String, Regexp) (defaults to: /[\s_\-]/)

    the delimiter to split on

  • kargs (Hash)

    capture extra keyword args, so no error for undefined args

Since:

  • 1.2.0



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.

See Also:

Since:

  • 1.2.0



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