Skip to content

Instantly share code, notes, and snippets.

@lowang
Created July 30, 2015 13:02
Show Gist options
  • Save lowang/10f13b2b69dca218c705 to your computer and use it in GitHub Desktop.
Save lowang/10f13b2b69dca218c705 to your computer and use it in GitHub Desktop.
class Credentials
@@credentials = {}
class << self
def set_current(credentials)
@@credentials[Thread.current.object_id] = credentials
end
def current
@@credentials[Thread.current.object_id]
end
end
end
class Session
def initialize(credentials)
@credentails = credentials
end
end
class SpecificType
@@starting_from = {}
@@session = {}
class << self
delegate :each, :first, to: :to_a
def from(nr)
@@starting_from[Thread.current.object_id] = nr
self
end
def to_a
session
result = 3.times.to_a.map { |e| e+= @@starting_from[Thread.current.object_id].to_i }
result
end
def all
clear_current_scope
self
end
def clear_current_scope
@@starting_from.delete Thread.current.object_id
end
def session
@@session[Thread.current.object_id] ||= Session.new(Credentials.current)
end
end
end
describe SpecificType do
before do
Credentials.set_current(api_key: 'a', login: 'l', password: 'p')
end
it 'handles chaining and clears state before different invocations' do
SpecificType.all.from(2).each do |val|
p val
end
puts "-"*20
SpecificType.all.each do |val|
p val
end
puts "-"*20
p SpecificType.all.first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment