Re: Multiple database management: dynamic models without class name

2018-02-12 Thread Андрей Аладьев
Ok. I find sequel very friendly for multiple database management. Thank 
you. \m/

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: Multiple database management: dynamic models without class name

2018-02-09 Thread Jeremy Evans
On Friday, February 9, 2018 at 7:43:19 AM UTC-8, Андрей Аладьев wrote:
>
> I don't know whether we could use ":class" option. Maybe we can add a new 
> option like ":associated_class" in order to patch "associated_class" 
> method.
>

You are specifically setting the associated class for the association, so 
the :class option is the appropriate one.  
AssociationReflection#associated_class will use the :class option you 
specify, and only fallback to constantizing the :class_name option if the 
:class option is not set.  I don't see how adding an :associated_class 
option would help, since it would do the same thing that the :class option 
currently does.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: Multiple database management: dynamic models without class name

2018-02-09 Thread Андрей Аладьев
I don't know whether we could use ":class" option. Maybe we can add a new 
option like ":associated_class" in order to patch "associated_class" method.


class Sequel::Model::Associations::AssociationReflection
  def associated_class
self[:associated_class] or constantize_class_name
  end

  def constantize_class_name
cached_fetch(:class) do
  begin
constantize(self[:class_name])
  rescue NameError => e
raise NameError, "#{e.message} (this happened when attempting to 
find the associated class for #{inspect})", e.backtrace
  end
end
  end
end


Thank you.

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: Multiple database management: dynamic models without class name

2018-02-09 Thread Jeremy Evans
On Friday, February 9, 2018 at 7:03:49 AM UTC-8, Андрей Аладьев wrote:
>
> it works fine. But it is just a dirty workaround. I think we can implement 
> some option like ":associated_class" that will be used instead of 
> ":class_name" constantization. For example:
>
> author.many_to_many :books, :associated_class => book
> book.many_to_many :authors, :associated_class => author
>
> What do you think about it? Thank you.
>

This appears to just rename the existing :class option, so I don't see the 
point.  Am I missing something?

Thanks,
Jeremy 

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: Multiple database management: dynamic models without class name

2018-02-09 Thread Андрей Аладьев
require "sequel"


class Sequel::Model::Associations::AssociationReflection
  def associated_class
cached_fetch(:class) do
  begin
if self[:class].is_a? Class
  self[:class]
else
  constantize(self[:class_name])
end
  rescue NameError => e
raise NameError, "#{e.message} (this happened when attempting to 
find the associated class for #{inspect})", e.backtrace
  end
end
  end
end

def db_scope(db)
  unless db.table_exists?(:authors)
db.create_table :authors do
  primary_key :id
end
  end

  unless db.table_exists?(:books)
db.create_table :books do
  primary_key :id
end
  end

  unless db.table_exists?(:authors_books)
db.create_table :authors_books do
  primary_key :id
  foreign_key :author_id, :authors
  foreign_key :book_id, :books
end
  end

  author = Class.new Sequel::Model(db[:authors]) do
def self.name; "author"; end
  end

  book = Class.new Sequel::Model(db[:books]) do
def self.name; "book"; end
  end

  author.many_to_many :books, :class => book
  book.many_to_many :authors, :class => author

  puts author.create.books
end

db_scope Sequel.connect("postgres://user@localhost")

it works fine. But it is just a dirty workaround. I think we can implement 
some option like ":associated_class" that will be used instead of 
":class_name" constantization. For example:

author.many_to_many :books, :associated_class => book
book.many_to_many :authors, :associated_class => author

What do you think about it? Thank you.

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Re: Multiple database management: dynamic models without class name

2018-02-09 Thread Андрей Аладьев
I can use the following workaround:

models = Module.new

author = Class.new Sequel::Model(db[:authors]) do
  many_to_many :books, :class_namespace => models, :class => :Book
end
models.const_set :Author, author

book = Class.new Sequel::Model(db[:books]) do
  many_to_many :authors, :class_namespace => models, :class => :Author
end
models.const_set :Book, book

But I've received the following error:
`constantize': "::#::Book" is not a valid 
constant name!

So it won't work.

def associated_class
  cached_fetch(:class) do
begin
  constantize(self[:class_name])
rescue NameError => e
  raise NameError, "#{e.message} (this happened when attempting to find 
the associated class for #{inspect})", e.backtrace
end
  end
end

So maybe I need to override this method in order to use raw class model 
instead of constantizing global class name.


пятница, 9 февраля 2018 г., 12:13:17 UTC+3 пользователь Андрей Аладьев 
написал:
>
> Hello. I see that it is almost possible for sequel to work with multiple 
> databases. But I've found a design issue.
>
> require "sequel"
>
>
> def db_scope(db)
>   unless db.table_exists?(:authors)
> db.create_table :authors do
>   primary_key :id
> end
>   end
>
>
>   unless db.table_exists?(:books)
> db.create_table :books do
>   primary_key :id
> end
>   end
>
>
>   unless db.table_exists?(:authors_books)
> db.create_table :authors_books do
>   primary_key :id
>   foreign_key :author_id, :authors
>   foreign_key :book_id, :books
> end
>   end
>
>
>   authors = Class.new Sequel::Model(db[:authors]) do
> many_to_many :books
>   end
>
>
>   books = Class.new Sequel::Model(db[:books]) do
> many_to_many :authors
>   end
>
>
>   authors.create.books
> end
>
>
> db_scope Sequel.connect("postgres://user@localhost")
>
> We can see the following error:
>
> uninitialized constant Book (this happened when attempting to find the 
> associated class for 
> # #.many_to_many :books>)
>
> Sequel wants "::Book" class. But it is not possible to provide such class 
> in global scope if we want to maintain multiple Books classes for multiple 
> db client instances. Book could be defined in db scope, not in global 
> scope. How to solve such question? Should I create a github issue?
>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.


Multiple database management: dynamic models without class name

2018-02-09 Thread Андрей Аладьев
Hello. I see that it is almost possible for sequel to work with multiple 
databases. But I've found a design issue.

require "sequel"


def db_scope(db)
  unless db.table_exists?(:authors)
db.create_table :authors do
  primary_key :id
end
  end


  unless db.table_exists?(:books)
db.create_table :books do
  primary_key :id
end
  end


  unless db.table_exists?(:authors_books)
db.create_table :authors_books do
  primary_key :id
  foreign_key :author_id, :authors
  foreign_key :book_id, :books
end
  end


  authors = Class.new Sequel::Model(db[:authors]) do
many_to_many :books
  end


  books = Class.new Sequel::Model(db[:books]) do
many_to_many :authors
  end


  authors.create.books
end


db_scope Sequel.connect("postgres://user@localhost")

We can see the following error:

uninitialized constant Book (this happened when attempting to find the 
associated class for 
#.many_to_many :books>)

Sequel wants "::Book" class. But it is not possible to provide such class 
in global scope if we want to maintain multiple Books classes for multiple 
db client instances. Book could be defined in db scope, not in global 
scope. How to solve such question? Should I create a github issue?

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sequel-talk+unsubscr...@googlegroups.com.
To post to this group, send email to sequel-talk@googlegroups.com.
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.