Metaphor
From the Greek: μεταφορά - metaphora, meaning "transfer"
Rationale
As programmers an awful lot of what we do revolves around taking an input, doing something to it, and sending the result somewhere. Quite a bit of time is wasted writing the "glue" that transfers the results from one place to the next. Metaphor aims to provide a standardised and easy to use way of passing results, leaving you to concentrate on the bit that's important - the business logic.
Programming with Metaphor
= Metaphor.new
.processors << Foo.new
.processors << Bar.new
.processors << Baz.new
.processors << Metaphor::Processor::PrintMessage.new
# Process one message
.call(headers, body) # => [ new_headers, new_body ]
# => false (if halted by processor)
# Process messages from this class until the Ruby VM is killed or the
# input returns nil
.call(StdinInput.new)
Classes used for input must respond to #get and return an array of headers and the message body:
class StdinInput
def get
# return an array like this:
[
{ "header" => "value", ... }, # Message headers
"body" # Message body
]
end
end
Classes used as processors must respond to #call(headers, body):
class PrintMessage
def call(headers, body)
puts "Headers: #{headers.inspect}"
puts "Body : #{body.inspect}"
end
end
The return value of #call controls what happens to the message. If the processor returns:
- An array of headers and the message body
- they are passed to the next processor
- Boolean false (or something that is === to it)
- Metaphor stops processing this message and discards it
- Any other value that is not false
- Metaphor passes the same headers and message that were passed into this processor to the next processor in the chain
Contributing
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don't break it in a future version unintentionally.
- Commit, do not mess with the Rakefile or Metaphor::VERSION. If you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull.
- Send me a pull request. Bonus points for topic branches.
Authors
- Sean O'Halpin
- Craig R Webster http://barkingiguana.com/
License
Released under the MIT licence. See the LICENSE file for details.