Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Purpose/add store #14

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open

Purpose/add store #14

wants to merge 14 commits into from

Conversation

radiospiel
Copy link
Owner

No description provided.

@radiospiel radiospiel force-pushed the purpose/add-store branch 3 times, most recently from b1d247b to 09d8423 Compare November 27, 2018 21:49
@radiospiel radiospiel force-pushed the purpose/add-store branch 2 times, most recently from e6337c8 to 21a76ed Compare November 27, 2018 23:04
If an object is passed in that implements a "from_complete_row" method, and the query
results are complete - i.e. all columns in the underlying table (via fq_table_name)
are in the resulting records - this mode uses the +into+ object's +from_complete_row+
method to convert the records. This mode is used by Simple::Store.

If the records are not complete, and contain more than a single value this mode behaves
like <tt>:immutable</tt>, i.e. they are read only.

If the records are not complete and contain only a single value this mode returns that
value for each record.
Simple::Store allows to easily define object “types”. A typical use case could look like this:

```
# This file is loaded when running bin/console.

Simple::SQL.exec <<~SQL
  CREATE SCHEMA IF NOT EXISTS store;

  CREATE TABLE IF NOT EXISTS store.organizations (
    id SERIAL PRIMARY KEY,
    name VARCHAR
  );

  CREATE TABLE IF NOT EXISTS store.users (
    id SERIAL PRIMARY KEY,
    organization_id INTEGER REFERENCES store.organizations (id),
    role_id INTEGER,
    first_name VARCHAR,
    last_name VARCHAR,
    metadata JSONB,
    created_at timestamp,
    updated_at timestamp
  );
SQL

Simple::Store.ask "DELETE FROM store.users"
Simple::Store.ask "DELETE FROM store.organizations"

Simple::Store::Metamodel.register "User", table_name: "store.users" do
  attribute :full_name, kind: :virtual
end

Simple::Store::Metamodel.register_virtual_attributes "User" do
  def full_name(user)
    "#{user.first_name} #{user.last_name}"
  end
end

Simple::Store::Metamodel.register "Organization", table_name: "store.organizations" do
  attribute :city, writable: false
end

user = Simple::Store.create! "User", first_name: "foo"
p user
# -> <User#33: created_at: 2018-11-28 09:52:37 +0100, first_name: "foo", full_name: "foo ", last_name: nil, organization_id: nil, role_id: nil, updated_at: 2018-11-28 09:52:37 +0100>
```
This is according to the “simple/sql Properly deal w/incomplete models, w/custom type converters” commit in Simple::SQL
Virtual attributes are resolved via method_missing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant