Sha256: ca4d2582f988bffa4dc5fbb1aa3f8abaddfde07608b44ab5b04d5089ef1087c2

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

#!/usr/bin/env ruby
# This example is loosely based on a joke posted by MillionthMonkey on
# slashdot: http://ask.slashdot.org/comments.pl?sid=250311&cid=19862863
# Nonetheless, it demonstrates some features of the protocol library pretty
# nicely.

require 'protocol'
require 'singleton'

MessageStrategyProtocol = Protocol do
  def send_message() end
end

MessageBodyProtocol = Protocol do
  attr_reader :payload

  def configure(obj)
    precondition { obj.respond_to? :to_str }
  end
  
  def send(message_strategy)
    MessageStrategyProtocol =~ message_strategy
    precondition { payload.respond_to? :to_str }
    postcondition { result == :done }
  end
end

class MessageBody
  attr_reader :payload

  def configure(obj)
    @payload = obj
  end

  def send(message_strategy)
    message_strategy.send_message
    :done
  end

  conform_to MessageBodyProtocol
end

StrategyFactoryProtocol = Protocol do
  def create_strategy(message_body)
    MessageBodyProtocol =~ message_body
  end
end

class DefaultFactory
  include Singleton

  def create_strategy(message_body)
    Class.new do
      define_method(:send_message) do ||
        puts message_body.payload
      end

      conform_to MessageStrategyProtocol
    end.new
  end

  conform_to StrategyFactoryProtocol
end

class HelloWorld
  def self.main(*args)
    message_body = MessageBody.new
    message_body.configure "Hello World!"
    factory = DefaultFactory.instance
    strategy = factory.create_strategy message_body
    message_body.send strategy
  end
end

if $0 == __FILE__
  HelloWorld.main(*ARGV)
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
protocol-0.9.0 examples/hello_world_patternitis.rb
protocol-0.8.2 examples/hello_world_patternitis.rb
protocol-0.8.0 examples/hello_world_patternitis.rb
protocol-0.8.1 examples/hello_world_patternitis.rb