Class: Riot::Message
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- Riot::Message
- Defined in:
- lib/riot/message.rb
Overview
A Message is similar in nature (but not implementation) to a string buffer; you put some strings in and calling #to_s will generate a single new string. What’s special abnout Message is how you get strings into it. By convention, any method called on a Message that isn’t defined will have its name turned into a string and any underscores replaced with spaces. This happens for each method call and those small messages are chained together at the end. For instance:
message = Riot::Message.new
message.hello_world.to_s
=> "hello world"
message.whats_the_news.to_s
=> "hello world whats the news"
For every method called it is also acceptable to pass any number of arguments. These arguments will be added to the final message after having ‘inspect` called on them. Another for instance:
message = Riot::Message.new
message.expected([1, 2, 3], "foo").not([3, 2, 1], "bar")
message.to_s
=> 'expected [1, 2, 3], "foo", not [3, 2, 1], "bar"'
This is useful for - and was originally intended for - generating pass/fail messages from assertion macros.
Instance Method Summary collapse
-
#but(*phrases) ⇒ Riot::Message
Adds the string “, but”.
-
#comma(str, *phrases) ⇒ Riot::Message
Adds a comma then the provided phrase to this message.
-
#initialize(*phrases) ⇒ Message
constructor
Creates a new Message instance.
-
#method_missing(meth, *phrases, &block) ⇒ Riot::Message
Converts any method call into a more readable string by replacing underscores with spaces.
-
#not(*phrases) ⇒ Riot::Message
Adds the string “, not”.
-
#to_s ⇒ String
(also: #inspect)
Generates the string value of the built-up message.
Constructor Details
#initialize(*phrases) ⇒ Message
Creates a new Message instance.
36 37 38 39 |
# File 'lib/riot/message.rb', line 36 def initialize(*phrases) @chunks = [] _inspect(phrases) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *phrases, &block) ⇒ Riot::Message
Converts any method call into a more readable string by replacing underscores with spaces. Any arguments to the method are inspected and appended to the final message. Blocks are currently ignored.
53 54 55 56 |
# File 'lib/riot/message.rb', line 53 def method_missing(meth, *phrases, &block) push(meth.to_s.gsub('_', ' ')) _inspect(phrases) end |
Instance Method Details
#but(*phrases) ⇒ Riot::Message
Adds the string “, but”.
Riot::Message.new.any_number.but(52).to_s
=> "any number, but 52"
78 |
# File 'lib/riot/message.rb', line 78 def but(*phrases); comma("but", *phrases); end |
#comma(str, *phrases) ⇒ Riot::Message
Adds a comma then the provided phrase to this message.
Riot::Message.new.hello.comma("world").to_s
=> "hello, world"
66 67 68 69 |
# File 'lib/riot/message.rb', line 66 def comma(str, *phrases) _concat([", ", str]) _inspect(phrases) end |
#not(*phrases) ⇒ Riot::Message
Adds the string “, not”.
Riot::Message.new.expected_freebies.not("$1.50").to_s
=> 'expected freebies, not "$1.50"'
87 |
# File 'lib/riot/message.rb', line 87 def not(*phrases); comma("not", *phrases); end |
#to_s ⇒ String Also known as: inspect
Generates the string value of the built-up message.
44 |
# File 'lib/riot/message.rb', line 44 def to_s; @chunks.join.strip; end |