Skip to content

Instantly share code, notes, and snippets.

@dkubb
Last active December 29, 2015 07:29
Show Gist options
  • Save dkubb/b279738b9d13f61f44f2 to your computer and use it in GitHub Desktop.
Save dkubb/b279738b9d13f61f44f2 to your computer and use it in GitHub Desktop.
Example Axiom::Schema using Axiom objects
require 'axiom'
module Axiom
class Schema < BasicObject
module DSL
def build(*args, &block)
object = new(*args)
object.instance_eval(&block)
object
end
end
extend DSL
def initialize(databases = {})
@databases = databases
end
def database(name, uri, &block)
@databases[name] = Database.build(uri, &block)
self
end
def [](name)
@databases[name]
end
class Database < BasicObject
extend DSL
def initialize(uri, relations = {})
@uri = uri
@relations = relations
end
def base_relation(name, &block)
@relations[name] = BaseRelation.build(name, &block)
self
end
def [](name)
@relations[name]
end
end
class BaseRelation < BasicObject
extend DSL
def initialize(name)
@name = name
@relation = ::Axiom::TABLE_DUM
end
def attribute(name, type)
@relation = @relation.extend { |r| r.add([name, type]) }
self
end
def relation
::Axiom::Relation::Base.new(@name, @relation.header)
end
end
end
end
schema = Axiom::Schema.build do
database(:memory, ':memory://test') do
base_relation(:people) do
attribute :id, Integer
attribute :email, String
attribute :name, String
end
base_relation(:addresses) do
attribute :id, Integer
attribute :person_id, Integer
attribute :city, String
attribute :zip, String
und
end
end
people = schema[:memory][:people].relation
addresses = schema[:memory][:addresses].relation
puts people.inspect
puts addresses.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment