Skip to content

Instantly share code, notes, and snippets.

@dkubb
Last active December 19, 2015 22:59
Show Gist options
  • Save dkubb/6031208 to your computer and use it in GitHub Desktop.
Save dkubb/6031208 to your computer and use it in GitHub Desktop.
Examples for axiom-memory-adapter
require 'axiom-memory-adapter'
adapter = Axiom::Adapter::Memory.new
# setup base relations
adapter['customers'] = Axiom::Relation::Empty.new(
[[:id, Integer], [:name, String]]
)
adapter['orders'] = Axiom::Relation::Empty.new(
[[:id, Integer], [:customer_id, Integer]]
)
# insert customer data
customers = adapter['customers']
customers.insert([[1, 'Dan Kubb']])
customers.insert([[2, 'John Doe']])
# insert order data
orders = adapter['orders']
orders.insert([[1, 1]])
orders.insert([[2, 1]])
orders.insert([[3, 1]])
orders.insert([[4, 2]])
# join customers and orders
customer_orders = customers.
rename(id: :customer_id).
join(orders.rename(id: :order_id))
# demonstrate writable view-like behaviour
# insert into the join
customer_orders.insert([[3, 'Jane Doe', 5]])
# inserts are propagated to the base relations
adapter['customers'].count # => 3
adapter['orders'].count # => 5
# delete from a join
customer_orders.delete([[3, 'Jane Doe', 5]])
# deletes are propagated to the base relations
adapter['customers'].count # => 2
adapter['orders'].count # => 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment