Class: Pokan::Cluster::PreparedString
- Inherits:
-
Object
- Object
- Pokan::Cluster::PreparedString
- Defined in:
- lib/pokan-cluster/prepared_string.rb
Overview
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.
Instance Method Summary collapse
-
#initialize(string, token = '%p') ⇒ PreparedString
constructor
Creates a new instance of your prepared string as the first argument.
-
#string ⇒ Object
Retrieves the string content for the prepared string.
-
#with(*args) ⇒ Object
Allows you to specify the strings to be placed in the tokens.
Constructor Details
#initialize(string, token = '%p') ⇒ PreparedString
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`
21 22 23 24 25 |
# File 'lib/pokan-cluster/prepared_string.rb', line 21 def initialize(string, token='%p') @original, @token = string, token @current = copy @original @changed = false end |
Instance Method Details
#string ⇒ Object
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.
30 31 32 33 34 35 36 |
# 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) ⇒ Object
Allows you to specify the strings to be placed in the tokens
39 40 41 42 43 44 45 46 47 |
# 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 |