How do you references database dirs in a gem?

The code I have works by making DATA_DIR from a relative path. When I put that in a gem it fails to find the database. This is my first gem, anyone have a good tutorial on how to make portable code find the database, when the code goes from ${HOME}/<project> to /usr/lib/ruby/gems/etc/etc/etc...

Thanks!

Leam

It depends on what kind of database you use. In general I'd like to let the user configure where to look for
store data. you can offer some default path, but for a database the user should have control over the place to
put it IMO

best wishes
ralf

···

On 02/03/2017 10:45 AM, Leam Hall wrote:

The code I have works by making DATA_DIR from a relative path. When I put that in a gem it fails to find the
database. This is my first gem, anyone have a good tutorial on how to make portable code find the database,
when the code goes from ${HOME}/<project> to /usr/lib/ruby/gems/etc/etc/etc...

This is not directly related to gemifying your code, only to the fact
that you run the code from a different directory than the directory
where the source file is located. The same would happen if for example
you would place a Ruby program in a `bin' directory (that is included
in your PATH) and run it from a different directory.

The reason is that relative paths are taken to be relative to the
current working directory. You need a path relative to the source file.

One way to do it (assuming this code is in the project's root dir):

PROJECT_DIR = File.expand_path(File.dirname(__FILE__))
DATA_DIR = File.join(PROJECT_DIR, "db")

This takes the absolute path to the project's source directory
and joins it with a relative path in order to get an absolute path
to the database directory. This works regardless of where the
program or gem is installed and regardless from where it is run.

Regards,
Marcus

···

Am 03.02.2017 um 10:45 schrieb Leam Hall:

The code I have works by making DATA_DIR from a relative path. When I
put that in a gem it fails to find the database. This is my first gem,
anyone have a good tutorial on how to make portable code find the
database, when the code goes from ${HOME}/<project> to
/usr/lib/ruby/gems/etc/etc/etc...

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

The code I have works by making DATA_DIR from a relative path. When I put

that in a gem it fails to find the

database. This is my first gem, anyone have a good tutorial on how to

make portable code find the database,

when the code goes from ${HOME}/<project> to /usr/lib/ruby/gems/etc/etc/

etc...

It depends on what kind of database you use. In general I'd like to let the
user configure where to look for
store data. you can offer some default path, but for a database the user
should have control over the place to

Sorry I was not clear, my coffee was still kicking in. The Sqlite database
is packaged with the gem and I am trying to make the install and run work
for non computer people.

···

On Feb 3, 2017 5:13 AM, "Ralf Mueller" <ralf.mueller@mpimet.mpg.de> wrote:
On 02/03/2017 10:45 AM, Leam Hall wrote:

I think you have access to the gem's installation path via some gem
specific properties. That should be usable. Worst case, you evaluate
__FILE__ inside the gem and use that as basis.

Kind regards

robert

···

On Fri, Feb 3, 2017 at 11:54 AM, leam hall <leamhall@gmail.com> wrote:

Sorry I was not clear, my coffee was still kicking in. The Sqlite database
is packaged with the gem and I am trying to make the install and run work
for non computer people.

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Marcus, your solution worked perfectly! Thanks! I'm going to use it to fix another issue as well.

Leam

···

On 02/03/17 09:59, sto.mar@web.de wrote:

One way to do it (assuming this code is in the project's root dir):

PROJECT_DIR = File.expand_path(File.dirname(__FILE__))
DATA_DIR = File.join(PROJECT_DIR, "db")

This takes the absolute path to the project's source directory
and joins it with a relative path in order to get an absolute path
to the database directory. This works regardless of where the
program or gem is installed and regardless from where it is run.

Regards,
Marcus

Hi Robert,

sounds like you consider this approach a bad practice.
Could you explain why?

Regards,
Marcus

···

Am 03.02.2017 um 15:30 schrieb Robert Klemme:

Worst case, you evaluate __FILE__ inside the gem and use that as basis.

--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A

First of all I have to say I am not familiar with the internal
workings of gems. This approach felt kind of hackish because if gem
system has different deployment locations for code and resources and
one of them is changed, then just relying on a fixed relative path
from the source to resource would break.

Kind regards

robert

···

On Fri, Feb 3, 2017 at 4:50 PM, <sto.mar@web.de> wrote:

Am 03.02.2017 um 15:30 schrieb Robert Klemme:

Worst case, you evaluate __FILE__ inside the gem and use that as basis.

sounds like you consider this approach a bad practice.
Could you explain why?

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/