Good project layout for a ruby system project? - ruby

I am writing small Ruby program that is used to backup some folders and send to s3,
e.g.
backup.rb --folder /folder1 --folder /folder1 ...-c config.ini
Before I start writing this program from scratch, I want to know if there is some existing good sample project layout that I can reference?
e.g. how to layout the tests, libraries, classes, configs, doc etc
I am particularly interested in a layout that allow me to share the libraries so later I can easily re-use in other project., even submit to ruby gems
Would be helpful if anyone can send me some good examples from existing OSS.

This is a pretty standard structure. Drawn from RubyGem Tutorial
.
bin/ # Executables here
data/ # Assets here
doc/ # RDOC for the lib directory
lib/ # Code for the project
project_module.rb # Contains project module
project_module/ # Classes for project module
# Either
spec/ # RSpec Tests
test/ # Test::Unit Tests

Related

Ruby - rspec how to include specs in root of project?

rspec always finds files in the spec/ directory tree.
How can I have it also find files that are in the root of the project (the one that contains the spec/ folder iself.
For example if I have a small project with only two tests, any folders may essentially be unneeded overhead given a minimalist approach. Or if you only have one type of test and would only create one folder it may not (or may) be worth creating that one folder or just omitting it altogether. The value it adds of course is immediate description of what type of tests exist, even if only one type exist.
Spec supports a --default-path option, and you can put a default set of command-line options into an .rspec file. That is, you can create a .rspec file in your project root directory containing
--default-path .
My experience has generally been that rspec tests are almost always in a spec directory; I would be a little surprised to see them in a top-level directory. This in particular mirrors the standard gem filesystem layout which puts all library code into a lib directory and tests in a parallel spec directory.
One approach:
# File: spec_helper.rb Add the following at the top:
Dir.glob(File.expand_path("../../*_spec.rb", __FILE__)).each do |file|
require file
end
This will include files at the root to the ones already being included in spec/

Is the site directory in puppet 4.x required

I'm following this site: https://puppet.com/docs/pe/2017.2/r_n_p_full_example.html
In this file path: /etc/puppetlabs/code/environments/production/site/profile/manifests/jenkins/master.pp
Is the directory called 'site' required?
Could the directory called profile above be moved to the modules directory?
I'm using puppetserver version 4.10.12.
Is the directory called 'site' required? Could the directory called profile above be moved to the modules directory?
It depends. In a general sense, sure, you can put your 'role' and 'profile' modules into the environment's modules/ directory. That's where I put mine.
But the writeup you linked explains why it demonstrates using a different directory:
If you deploy your code with Puppet Enterprise’s code manager or r10k,
we recommend putting these two modules in your control repository
instead of declaring them in your Puppetfile. Since code manager and
r10k reserve the modules directory for their own use, you must put
them in a separate directory
If you are not using either of those code deployment tools and don't anticipate doing so any time soon -- like me -- then not only can you put your modules into the modules/ directory, but that's what I would recommend doing. But if you plan on using one of those then do follow the guide in this matter.

Rspec multiple spec folders

Im currently building a project using Rspec in which I sepparated in two different folders with domain code and infrastructure code. Both folders have their own specs in a spec folder. The domain/spec folder is the one containing the spec_helper.rb file, thats required from the tests inside the other folder infrastructure/spec
I'd like to know how to have a spec folder in the root of the project, including the spec_helper file and also tests, and being able to run all the tests with just one command (right now I do it running rspec domain/ infrastructure/)
RSpec is designed to work with all tests in one folder. By default, this folder is called spec/, but you can use a different name with the --default-path option.
So, your options as I see it are:
Edit the source code of rspec-core to let that configuration support multiple directories. Hopefully your PR will be approved and merged.
Or, write a simple wrapper script that runs rspec against both directories. For example, you could alias rspecs='rspec domain/ infrastructure/'.
Or (what I would recommend!), you could just restructure your tests slightly to use spec/domain/ and spec/infrastructure/ folders -- and then everything will just work, by convention, out of the box.

Get Rack to load config from Gem

Is it possible to run Rack and specify that it should look for config.ru from one of the project's Gem dependencies rather than the project's files on the local filesystem?
I'm sure this is a 'wrong' pattern, but we've got project A that has a load of front-end static resources. Project B is a Sinatra app that provides the backend services for that web UI to call upon. We separated them into two projects so A could depend on a particular version of B, and be insulated from breaking changes.
B contains the Sinatra app, and the config.ru which defines what Sinatra endpoints are mapped to which paths, and additionally that the static resources should be served too.
A has the thinnest sliver of Ruby, essentially just depending on B via a Gemfile.
We want someone to be able to clone project A, do a bundle install, and then run rackup and have Rack use the config.ru inside B's Gem to decide how to configure itself.
The "rackup" command can take options for the include path and a specific library as well as the path to the config file you want to run, so you should be able to pull it off…
Usage: rackup [ruby options] [rack options] [rackup config]
Ruby options:
...
-I, --include PATH specify $LOAD_PATH (may be used more than once)
-r, --require LIBRARY require the library, before executing your script
If that doesn't work for you, you may need to provide more details on what you are trying to do

Ruby - Where to put resource files that a module in lib relies on?

I am trying to structure my ruby project following best practices. I currently have something like this:
test_project/
bin/
test_project # My Executable
lib/
test_project/
my_module.rb
test_project.rb # Loads my_module.rb
I setup it up this way based on recommendations I found on the web.
My problem is I have some resource files, "resouce1.txt" and "resouce2.txt". My executable needs to open the file "resource1.txt". my_module.rb needs to be able to open the file "resource2.txt". Where do I put these plan text resource files in this directory structure and how to I open them (File.open) from the corresponding ruby files.
Since /lib has to do with specifically the Ruby that powers your gems, I would put it in a top-level directory in your gem named after their subject matter. If they are files with lists of species I would call it /species.
You could also go the Rails way and put it in an /assets folder if you have a lot of external assets like /assets/species. Either way, I would not be prone to put them in /lib.
I don't think there is a standard place for these, as for the most part it is operating system dependent (/var and /etc vs Program Files vs the Application Bundle). But your best bet (I think) is to either put them in the root of your hierarchy, put them in lib/ or, if they really are static text files, put them in your script.

Resources