Skip to content

Instantly share code, notes, and snippets.

@jch
Created March 22, 2013 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jch/5218000 to your computer and use it in GitHub Desktop.
Save jch/5218000 to your computer and use it in GitHub Desktop.
ruby prototyping
# 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
@krainboltgreene
Copy link

You can turn

def configure(&blk)
    blk.call(config) if blk
    config
  end

into

def configure
    yield(config) if blk
    config
end

I believe.

@krainboltgreene
Copy link

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.

@tarcieri
Copy link

Psst, @krainboltgreene, there's no blk variable now. I think you want block_given?

@patmaddox
Copy link

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