vanilla ruby rspec cannot see my files - ruby

So, I am trying to create a gem with a lib folder and a spec folder for rspec tests, like so:
- my_gem
|-- lib
| |-- my_gem
| | |-- foo.rb
| |
| |-- my_gem.rb
|
|-- spec
| |-- lib
| |-- foo_spec.rb
|
|-- my_gem.gemfile
|-- Rakefile
my_gem.rb contains require 'my_gem/foo' which works fine.
But my spec file which tries to require 'lib/my_gem' doesn't work. When I run rspec (or rake spec) without explicitly setting a path with -I ., it fails to load the file:
foo_spec.rb:1 in `require': cannot load such file -- lib/my_gem.rb (LoadError)
I feel like I shouldn't have to require a spec_helper.rb in every spec just to fix this, nor should I have to specify the load path for rspec every time. There has to be a simple convention that I am missing because I am new to gem development (I usually only do rails stuff).
Edit: I should point out that I am well aware that I can require_relative '../../lib/my_gem.rb'. The point is that I want to be able to get the root folder onto my path automatically like I am used to in rails. If this just isn't possible then please let me know.

The spec doesn't know the path to lib/my_gem. Try
require_relative '../../lib/my_gem'

Related

How to specify Dapr component locations with Sidekick?

I'm trying to get started with Sidekick for Dapr, and am having trouble telling Sidekick where the dapr components are.
By default it's going to %USERPROFILE%.dapr\components, but I'd rather it go to a folder local to the solution.
Looking at the code it appears that adding the following to the appsettings.json should work, but it isn't picked up.
"DaprSidekick": {
"RuntimeDirectory": "dapr",
"ComponentsDirectory": "C:\\Dev\\DaprPOC\\components",
}
However the components folder invariably becomes %USERPROFILE%\.dapr\components
Any help on how I specify the component locations with Sidekick?
When you set "RuntimeDirectory": "dapr" Sidekick will automatically look for component files in the dapr/components subdirectory in your solution. Try removing the ComponentsDirectory entry so it returns to defaults, and try a directory structure like this:
|-- MyProject
| |-- MyProject.csproj
| |-- dapr
| | |-- config.yaml
| | |-- components
| | | |-- my_component.yaml
The Dapr Sidecar should then load my_component.yaml.
You can also manually add the components directory in the dependency injection:
services.AddDaprSidekick(configuration, p => p.Sidecar =
new DaprSidecarOptions() { AppId = "daprservice", ComponentsDirectory = "C:\\Dev\\DaprPOC\\components" });

Can I add factorygirl factories to shared gem

I have a set of active_model models I've extracted into a shared gem. I want the gem to also contain their factories for use in rspec tests defined in any of the projects that are using the gem to avoid defining the same factories in each project. Is that possible?
Yeah its possible. You can place the Factories inside your gem. Lets say lib/gem/factories/. Then your other gems can include the factory.
For example when you create the gem 'awesome_gem', you could have the followin structure:
|-- lib
| |-- awesome_gem
| | |-- module1
| | `-- version.rb
| |-- factories
| | |-- factory1.rb
| | `-- factory2.rb
| `-- awesome_gem.rb
|
`-- spec
|-- awesome_gem
`-- spec_helper.rb
Where lib contains your gem code and specs contain your specs.
awesome_gem.rb would define your gem includes such as awesome_gem/version.rb and more includes. However its recommended not to include the factories directory, since an user of your gem could not be using factory_girl.
Once the gem is packed, they can use require awesome_gem/factories/factory1.rb in their tests. Note that you need to specify your factory path in your spec helper to include your shared factories. Note that this solution is not tested, and paths that need to be included may be different. Also you can place the factories directory outside the lib dir, as long it is included by your .gemspec .

Compass Source in Multiple Directories

Have you had success compiling SASS in multiple directories? Can you set up compass to recursively watch a directory?
I have read the documentation on add_import_path, but I would really appreciate some sample code, as I have (I am fairly certain) never written a line of ruby code.
The reason I ask is that I have several projects that share some standard scss. I would like changes to the shared scss to cascade to all projects.
thanks.
Let's say you have the following directroy structure:
project
|-- config.rb
+-- apps
|-- main.scss
|-- app1
+-- appst1.scss
|-- app2
+-- appst2.scss
+-- app3
+-- appst3.scss
Then adjust your config.rb:
sass_dir = "apps"
add_import_path "apps"
...
and in your main.scss include the other scss files:
#import "app1/appst1";
#import "app2/appst2";
#import "app3/appst3";
Here is my solution that supports batch compass compile/watch of multiple independent SASS projects, based on two Ruby scripts.
Folder structure with the Ruby files:
Root
--compile.rb
--watch.rb
--Module1
----config.rb
----css
----sass
--Module2
----config.rb
----css
----sass
--Module3
----config.rb
----css
----sass
Run compile.rb and watch.rb with several arguments representing the paths to your module folders containing the config.rb files.
I.e. : ruby compile.rb Module1/ Module2/ Module3/
compile.rb
require 'rubygems'
require 'compass'
require 'compass/exec'
ARGV.each do |arg|
Compass::Exec::SubCommandUI.new(["compile", arg, "--force"]).run!
end
I.e. : ruby watch.rb Module1/ Module2/ Module3/
watch.rb
require 'rubygems'
require 'compass'
require 'compass/exec'
threads = []
ARGV.each do |arg|
threads << Thread.new {
Compass::Exec::SubCommandUI.new(["watch", arg, "--force"]).run!
}
sleep(1)
end
threads.each { |thr| thr.join }
Notice that we need to create a separate thread for each compass watch (since they are blocking processes). sleep(1) is necessary because Compass::Exec::SubCommandUI is not actually thread-safe and might run several watches on the same module, instead of one on each. In case that happens, try increasing the sleep value.
Create a similar config.rb file in all modules. You might have to use compass init to get the a first config.rb that compass recognizes.
config.rb
http_path = "/"
css_dir = "css"
sass_dir = "sass"

Rails 3.1 asset pipeline vendor/assets folder organization

I'm using the jQuery Tools scrollable library in my Rails 3.1 site with the various assets placed in the vendor/assets folder and it works great.
My question is regarding the best way to organize the various files under vendor/assets. What is the recommended way to organize vendor/assets subfolders? Currently I have this structure:
vendor/assets/
|-- images/
| |-- scrollable/
| <various button/gradient images>
|-- javascripts/
| |-- scrollable/
| jquery.tools.min.js
|-- stylesheets/
| |-- scrollable/
| scrollable-buttons.css
| scrollable-horizontal.css
This is a fairly un-DRY what to do this. I feel that all of the 'scrollable' items should be under one folder.
What is the recommended way to do this without having to manipulate the asset pipeline load paths?
Thanks!
You could organise them this way, which is slightly better in that it keeps stuff related to the plugin in one directory:
vendor/assets/scrollable
|-- images/
| |-- <various button/gradient images>
|-- javascripts/
| |-- jquery.tools.min.js
|-- stylesheets/
| |-- scrollable-buttons.css
| scrollable-horizontal.css
I am pretty sure this will work as rails globs all directories under assets/.

How to include directories while debugging and testing a gem

Can anybody throw me a line?
I'm having problems in tests implementation, concretely, while requiring files.
I am developing a gem called mme_tools with a directory tree like this. The initial scaffold was generated using jeweler.
mme_tools
|-- examples
| |-- demo_enumerable.rb
| `-- demo_print_debug.rb
|-- Gemfile
|-- Gemfile.lock
|-- lib
| |-- mme_tools
| | |-- concurrent.rb
| | |-- debug.rb
| | |-- enumerable.rb
| | |-- version.rb
| | `-- webparse.rb
| `-- mme_tools.rb
|-- LICENSE.txt
|-- Rakefile
|-- README.rdoc
`-- test
`-- test_mme_tools.rb
where, mme_tools.rb is simply a container that requires all the files in mme_tools subdir (i.e. require 'mme_tools/debug.rb').
In the examples dir I can make some untidy tests (demos) as I progress. Simply putting a require 'mme_tools' at the top of each of those demos, and running ruby with option -I../lib works OK. In fact, I use Netbeans to develop so I include that option in every run.
My problem arises while testing. At the top of test_mme_tools.rb I have
require 'test/unit'
require 'mme_tools'
but my tests don't work because I think mme_tools.rb isn't found. The output from the rake task that I run from the shell is:
$ rake test
rake/rdoctask is deprecated. Use rdoc/task instead (in RDoc 2.4.2+)
/usr/bin/ruby1.8 -I"lib:lib:test" -I"/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib" "/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb" "test/**/test_*.rb"
Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader
Started
E
Finished in 0.02176 seconds.
1) Error:
test_compose(TC_MMETools):
NoMethodError: undefined method `compose' for MMETools::Enumerable:Module
/mnt/dropbox/DESENVOLUPAMENT/Gems/mme_tools/test/test_mme_tools.rb:19:in `test_compose'
1 tests, 0 assertions, 0 failures, 1 errors
rake aborted!
Command failed with status (1): [/usr/bin/ruby1.8 -I"lib:lib:test" -I"/usr/...]
Tasks: TOP => test
(See full trace by running task with --trace)
Of course, from Netbeans occurs the same, exceptuating that it shows the full trace
The file test_mme_tools.rb is as follows:
require 'test/unit'
require 'mme_tools'
class TC_MMETools < Test::Unit::TestCase
def setup
#a=(1..12)
#b=("A".."M")
#c=%w{tantmateix adhuc nogensmenys urgell pocassolta carrincló bajanada casundena Massalcoreig}
end
def test_compose
assert_equal [2,"B","adhuc"], MMETools::Enumerable.compose(#a,#b,#c)[1]
end
end
Any idea?
Assuming you continue with Rake, and update to use minitest, you can use a Rakefile that looks like this:
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << 'lib'
t.pattern = 'test/**/*.rb'
end

Resources