Class: Stringtrain::Cab

Inherits:
Object
  • Object
show all
Defined in:
lib/stringtrain/cab.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ Cab

Returns a new instance of Cab.

Parameters:

  • opts (Hash) (defaults to: {})

    Global options for the processor.

Options Hash (opts):

  • :part_separator (String) — default: ' '

    A string that will be used as the delimiter between parts.



9
10
11
12
13
14
15
16
# File 'lib/stringtrain/cab.rb', line 9

def initialize(opts={}, &block)
  opts = { part_separator: ' ' }.merge(opts)
  self.parts = []
  self.part_separator = opts[:part_separator]
  if block_given?
    block.arity == 1 ? yield(self) : instance_eval(&block)
  end
end

Instance Attribute Details

#part_separatorObject

Returns the value of attribute part_separator.



5
6
7
# File 'lib/stringtrain/cab.rb', line 5

def part_separator
  @part_separator
end

#partsObject

Returns the value of attribute parts.



5
6
7
# File 'lib/stringtrain/cab.rb', line 5

def parts
  @parts
end

Instance Method Details

#part(string, opts = {}) ⇒ Object

Examples:

Adding two parts together with suffices

Stringtrain::Cab.new do
  part 'A monkey loves to eat', suffix: '...'
  part 'bananas', suffix: '!'
end
#=> "A monkey loves to eat... bananas!"

A more complex usage example, making use of :condition, :prefix, :suffix, and :surround_with.

Stringtrain::Cab.new {|a|
  a.part cd.current.tracknumber.to_s, surround_with: %w{# :}
  a.part cd.current.artist, suffix: '-'
  a.part cd.current.title
  if cd.current.date
    a.part '%s (%s)' % [cd.current.album, cd.current.date], surround_with: %w{[ ]}, condition: cd.current.album?
  else
    a.part cd.current.album, surround_with: %w{[ ]}, condition: cd.current.album?
  end
}
#=> "#08: Loreena McKennitt - Dante's Prayer [The Book Of Secrets (1997)]"

Parameters:

  • string (String)

    A part of the resulting string.

  • opts (Hash) (defaults to: {})

    Processing options for the part.

Options Hash (opts):

  • :condition (Boolean) — default: true

    An expression that returns true or false (or just an object). If the expression returns false, then the part does not get added.

  • :prefix (String) — default: nil

    A string that appears before the part.

  • :suffix (String) — default: nil

    A string that appears after the part.

  • :surround_with (String, Array) — default: nil

    A string or array that will be used around string. If it is an array, the first and last elements will be used for the left and right side of string.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/stringtrain/cab.rb', line 42

def part(string, opts={})
  opts = { condition: true, prefix: nil, suffix: nil, surround_with: nil }.merge(opts)
  pieces = [opts[:prefix], string, opts[:suffix]]
  part = if opts[:surround_with].respond_to?(:fetch)
    pieces.insert(-1,opts[:surround_with].last)
    pieces.insert(1,opts[:surround_with].first)
    pieces.compact.join
  else
    pieces.compact.join(opts[:surround_with])
  end
  if !string.blank?
    parts << part if opts[:condition]
  end
end

#to_sString

Returns The resultant string.

Returns:

  • (String)

    The resultant string.



58
59
60
# File 'lib/stringtrain/cab.rb', line 58

def to_s
  parts.join(self.part_separator)
end