Skip to content

Instantly share code, notes, and snippets.

@mbj
Last active December 12, 2015 09:59
Show Gist options
  • Save mbj/4755497 to your computer and use it in GitHub Desktop.
Save mbj/4755497 to your computer and use it in GitHub Desktop.
A potential veritas relation registry usable for switching between adapters and in-memory via configuration.
class RelationRegistry
include AbstractType
# Initialize object
#
# @return [undefined]
#
# @api private
#
def initialize
@map = {}
end
# Return in memory relation
#
# @example
# source = RelationSource::InMemory.new
# source.register(:foo, [[:id, String], [:foo, String]])
# source.get(:foo) # => <Veritas::Relation header=Veritas::Header ...>
#
# @param [Symbol] name
# the name of the base relation
#
# @return [Veritas::Relation]
#
# @raise [KeyError]
#
#
# @api private
#
def get(name)
@map.fetch(name)
end
# Register relation
#
# @param [Symbol] name
# the name to track relation under
#
# @param [Veritas::Header] header
# the veritas header, or coercible to veritas header
#
# @example with coercible header
# source = RelationSource::InMemory.new
# source.register(:foo, [[:id, String], [:foo, String]]) # => source
#
# @example with instace of veritas header
# source = RelationSource::InMemory.new
# source.register(:foo, Veritas::Heade.coerce([[:id, String], [:foo, String]])) # => source
#
# @return [self]
#
def register(name, header)
header = Veritas::Header.coerce(header)
relation = @map.fetch(name) do
@map[name]=build(name, header)
end
self
end
# Relation registry for in memory adapters
class InMemory < self
private
# Build veritas in memory relation
#
# @param [Symbol] name
# the relation name
#
# @param [Veritas::Relation::Header] header
#
# @return [Veritas::Relation]
#
# @api private
#
def build(_name, header)
Veritas::Relation.new(header, [])
end
end
# Relation registry backed by base relations in adapters
class Adapter
# Initialize object
#
# @param [Veritas::Adapter] adapter
#
# @return [undefined]
#
# @api private
#
def initialize(adapter)
@adapter = adapter
super()
end
private
# Build veritas gateway relation
#
# @param [Symbol] name
# the relatio name
#
# @param [Veritas::Relation::Header] header
#
# @return [Veritas::Adapter::Gateway]
#
# @api private
#
def build(name, header)
relation = Veritas::Relation::Base.new(name, header)
@adapter.gateway(relation)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment