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:

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



62
63
64
# File 'lib/psychgus/stylables.rb', line 62

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



63
64
65
# File 'lib/psychgus/stylables.rb', line 63

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



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.

Parameters:

  • word (nil, String)

    the word to capitalize

Returns:

  • (String)

    the capitalized word



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

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



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