Created
March 22, 2013 00:18
-
-
Save jch/5218000 to your computer and use it in GitHub Desktop.
ruby prototyping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Manage user email subscriptions and opt-outs. | |
module OptOut | |
def config | |
@config ||= Configuration.new | |
end | |
def configure(&blk) | |
blk.call(config) if blk | |
config | |
end | |
module_function :config, :configure | |
class Configuration < Struct.new(:user, :persistence) | |
end | |
module Persistence | |
class AbstractAdapter | |
end | |
class MemoryAdapter < AbstractAdapter | |
end | |
end | |
class Unsubscription | |
end | |
class List | |
# Public: lookup a list by name | |
# | |
# Returns a List instance | |
def self.[](name) | |
new(name) | |
end | |
attr_accessor :name | |
def initialize(name) | |
@name = name | |
end | |
def subscribe(user) | |
end | |
def unsubscribe(user) | |
end | |
def subscribers | |
end | |
def subscribed?(user) | |
end | |
end | |
# Defaults | |
configure do |c| | |
c.persistence = OptOut::Persistence::MemoryAdapter | |
c.user = { | |
:primary_key => Proc.new {|u| u.id} | |
} | |
end | |
class ListTest < Test::Unit::TestCase | |
def setup | |
@list = List['security'] | |
end | |
def test_lookup | |
assert @list.is_a?(List) | |
assert_equal 'security', @list.name | |
end | |
def test_subscribe | |
@list.subscribe() | |
end | |
end | |
end |
Although now that I think about it, if you instance_eval
the block will have access to config
and you wont have to bother with a block argument.
Psst, @krainboltgreene, there's no blk
variable now. I think you want block_given?
yield if block_given?
personally I prefer explicit blocks
and definitely prefer a basic block call to instance_eval !!
but yes ruby is super nice for composing ideas simply and quickly
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can turn
into
I believe.