Middleman use custom helper function inside config.rb file - ruby

I have defined a helper function inside the config.rb file. When I use it elsewhere, it works fine. But I have to use that function inside the config.rb as well simultaniously. Unfortunately there is an error:
`method_missing': undefined method `slug' for #<Middleman::ConfigContext:0xa137b44> (NoMethodError)
Is it nevertheless possible to access a function inside the helpers in config.rb file? Or do I have to declare the function outside globally and then access it from both inside the helper function and somewhere else in the file?

I was able to get this working by requiring my helper and including the module like this in config.rb:
require 'helpers/slugify_helper'
include SlugifyHelper
Then I was able to call my helper method from config.rb like: slugify(url).
FYI: I'm running Middleman ~> 4.2.1.

Related

Cucumber: Unable to see methods of a module included in the World?

I'm working with cucumber/ruby and I wanted to create a new module with some methods to use them in my step definitions.
I was reading how to do this here, https://github.com/cucumber/cucumber/wiki/A-Whole-New-World. But when I've tried the following I get an error:
create the new module under /root_location/lib/new_module.rb
create the module as:
.
module Newmodule
def here
puts "here"
end
end
World(Newmodule)
However, when I then try to use the 'here' method from my steps definition, I just get:
undefined local variable or method `here' for # (NameError)
Any idea what I am doing wrong?
The module needs to be located in features, otherwise it won't be added into World. Cucumber does not look outside of features for anything unless you specifically tell it to.
Put this code either in features/support or features/step_definitions

Set capybara default_wait_time to a function

I made a function called wait_for_page load, and I am trying to set the default_wait_time to this function.
I get an undefined variable error:
undefined local variable or method `page' for main:Object (NameError)
I also included the file into the main environment file:
require File.expand_path('../../support/file_name.rb', FILE)
default_wait_time is an accessor in Capybara module. So you'll need to call it on the Capybara object itself, like:
Capybara.default_wait_time = some_value
And Capybara object should be available wherever you have defined this method.
In some newer versions accessor is default_max_wait_time, you can notice this because of a DEPRECATION warning
So you need to do this:
Capybara.default_max_wait_time = 5
The default is 2 seconds

Ruby including files

I have a Ruby app that runs on a server with no web interface. It is run using the command line(ruby path/to/file.rb).
I have classes in different files that I want to be accessible. The files are located in the "app/classes" directory.
I put this in the application.rb file:
config.autoload_paths += Dir["#{config.root}/classes"]
and I get an uninitialized constant error.
I can put in "require_relitive 'somefile'" but I would rather not have to do this for every class that is used. How do I create an autoload path and where should it be located at?
Use require_all
See https://github.com/jarmo/require_all
It basically allows you to write this:
require 'require_all'
require_all 'app/classes'
And all ruby files in app/classes will be loaded.

Undefined method error associated with a particular method

I've defined a method in a file called utility.rb. I've tried to call the method inside another file called main.rb. In main.rb, I did require that file by saying require utility, but when I ran main.rb, it gave me the undefined method error associated with that particular method. utility.rb and main.rb are in the same directory. Any idea?
I guess you have a file utility.rb in any of your installed ruby libraries. This file is loaded instead of your one. Choose a more specific name for your file or add . to the beginning of your lib search path:
$:.unshift('.')

How do I require a file from inside a directory with Ruby?

I think I'm missing something here. I have a directory like this:
myapp
|-lib
|-package1
|-dostuff.rb
|-package2
|-dostuff.rb
From an irb console I'm trying to test the library before I add it to my real project (a Rails app). However, typing this:
require 'lib/package1/dostuff'
returns an error saying it can't find the file to load. I added the lib directory to the load path but I'm not able to load the file.
What am I forgetting? The two filenames don't have to be the same but that's how they are to begin with (they are auto-generated from some web services I need to call using soap4r; each package represents a different web service API group)
If the directory "lib" is in the load path, the argument to require must be relative to lib. So require 'package1/dostuff' without the lib, otherwise it will look for lib/lib/package1/dostuff.rb.
In Ruby 1.9 there's the new require_relative method, which would let you do require_relative "../package2/dostuff" from within package1/dostuff.rb.

Resources