class Pokan::Cluster::PreparedString
PreparedString
is a class representing strings with marks, to be substituted later with specific values.
Example
p = PreparedString.new('Hi %p, my name is %p') p.with('John', 'James').string #=> "Hi John, my name is James" p.string #=> "Hi %p, my name is %p" p.with('John').string #=> "Hi John, my name is %p"
Useful for log messages.
Public Class Methods
new(string, token='%p')
click to toggle source
Creates a new instance of your prepared string as the first argument. You can specify the token to be substituted when using PreparedString#with
as the second argument. Default is ‘%p`
# File lib/pokan-cluster/prepared_string.rb, line 21 def initialize(string, token='%p') @original, @token = string, token @current = copy @original @changed = false end
Public Instance Methods
string()
click to toggle source
Retrieves the string content for the prepared string. If a substitution had already been done (using PreparedString#with
), then the substitued string will be returned. Otherwise, the original string is returned.
# File lib/pokan-cluster/prepared_string.rb, line 30 def string r_string = @changed ? @current : @original @changed = false @current = copy @original r_string end
with(*args)
click to toggle source
Allows you to specify the strings to be placed in the tokens
# File lib/pokan-cluster/prepared_string.rb, line 39 def with(*args) @changed = true args.each { |token| @current.sub!(@token, token.to_s) } self end
Private Instance Methods
copy(obj)
click to toggle source
# File lib/pokan-cluster/prepared_string.rb, line 51 def copy(obj) obj.dup end