How could I include these requires among multiple scripts - ruby

I have lots of scripts need to include the same header.
What's the better way to include them in every script
# -*- encoding : utf-8 -*-
#!/usr/bin/ruby
require 'yaml'
require "json"
require 'pry'
require "selenium-webdriver"
require 'nokogiri'
require 'active_support/all'
require 'yaml'
require 'date'
require 'awesome_print'
require 'mongo'
require File.expand_path('./form_action.rb', File.dirname(__FILE__))
require File.expand_path('./schedule_manager.rb', File.dirname(__FILE__))
require File.expand_path('../common_helper.rb', File.dirname(__FILE__))
require File.expand_path('../mongodb_helper.rb', File.dirname(__FILE__))
require File.expand_path('../webdriver_helper.rb', File.dirname(__FILE__))
require File.expand_path('../lib/db_manager.rb', File.dirname(__FILE__))
require File.expand_path('../lib/app_util.rb', File.dirname(__FILE__))
require File.expand_path('../lib/schedule_manager_base.rb', File.dirname(__FILE__))
require File.expand_path('../lib/class_template.rb', File.dirname(__FILE__))
include Mongo
include MongodbHelper
include AppUtil
Project folder structure
I will include the common_includes in client1.rb,
But the SIBLINGS not work.
├── common_helper.rb
├── common_includes.rb
├── config
│   ├── database.yml
│   └── schedule.rb
├── client1
│   ├── config.yml
│   ├── client1.rb
│   └── schedule_manager.rb
└── webdriver_helper.rb
├── lib
│   ├── app_util.rb
│   ├── class_template.rb
│   ├── db_manager.rb
│   └── schedule_manager_base.rb

Put smth like following:
REQUIRES = %w(yaml json pry selenium-webdriver nokogiri active_support/all yaml date awesome_print mongo)
SIBLINGS = %w(form_action schedule_manager)
COMMON = %w(common_helper mongodb_helper webdriver_helper db_manager.rb lib/app_util lib/schedule_manager_base lib/class_template.rb)
INCLUDES = %i(Mongo MongodbHelper AppUtil)
REQUIRES.each &method :require
SIBLINGS.each do |f|
File.expand_path("./#{f}.rb", File.dirname(__FILE__))
end
COMMON.each do |f|
File.expand_path("../#{f}.rb", File.dirname(__FILE__))
end
module CommonIncludes
def self.included base
INCLUDES.each { |inc| base.include Kernel.const_get(inc) }
end
end
into a very file common_includes.rb and everywhere you need it, call:
require 'common_includes'
include CommonIncludes
This is more or less common approach to require/include in case you need to specify the file list explicitly one by one.
NB To manage external dependencies, everyone wants to use bundler, as mentioned Mike Slutsky around.
UPD Don’t hesitate to use Dir["./*.rb"] to collect all the files for a given directory.

If I am understanding your question correctly, you are looking for a way that you can include all these headers without having to copy and paste them all over the place.
One way you can achieve this is to put all these headers into one file, and in your scripts just include that file:
For example, in headers.rb:
# -*- encoding : utf-8 -*-
#!/usr/bin/ruby
require 'yaml'
require "json"
require 'pry'
require "selenium-webdriver"
require 'nokogiri'
require 'active_support/all'
require 'yaml'
require 'date'
require 'awesome_print'
require 'mongo'
require File.expand_path('./form_action.rb', File.dirname(__FILE__))
require File.expand_path('./schedule_manager.rb', File.dirname(__FILE__))
require File.expand_path('../common_helper.rb', File.dirname(__FILE__))
require File.expand_path('../mongodb_helper.rb', File.dirname(__FILE__))
require File.expand_path('../webdriver_helper.rb', File.dirname(__FILE__))
require File.expand_path('../lib/db_manager.rb', File.dirname(__FILE__))
require File.expand_path('../lib/app_util.rb', File.dirname(__FILE__))
require File.expand_path('../lib/schedule_manager_base.rb', File.dirname(__FILE__))
require File.expand_path('../lib/class_template.rb', File.dirname(__FILE__))
include Mongo
include MongodbHelper
include AppUtil
Then your scripts:
require 'path/to/headers'

Related

puppet apply custom function

How can I quickly run a custom function on any node with puppet apply ?
Let's say I have this file (test.pp)
file { "/root/files/f":
content => test('test'),
}
And this ruby file (test.rb) which only log and return the first agument.
require 'logger'
module Puppet::Parser::Functions
newfunction(:test, :type => :rvalue
) do |args|
log = Logger.new(STDOUT)
log.level = Logger::INFO
log.info(args[0])
args[0]
end
end
How does one call the ruby function test with a puppet apply test.pp?
My problem is that is take like 5minutes to run my entire puppet agent -t and I need to add a function to puppet so I would like to test it quickly instead of waiting 5minutes each time.
This will certainly work, but it's hard to maintain.
A more maintainable way to do this is to add your function to a module, then install that module on your master.
For example, create a new with the name of your module (eg. test_function):
mkdir -p test_function/lib/puppet/parser/functions
You should have the following tree in your test_function module
├── lib
│   └── puppet
│   ├── parser
│   │   └── functions
│   │   ├── test.rb
Add your test.rb code to the test.rb file
Copy this module to your master's module path (this is probably /etc/puppet/modules depending on your Puppet version, use puppet module list to find out)
After that, the puppet master will read its module list and dynamically add-in all functions it finds.
More documentation about it here:
https://docs.puppetlabs.com/guides/plugins_in_modules.html
https://docs.puppetlabs.com/guides/custom_functions.html
I just found out.
All you need to do is to add the test.rb file directly into /var/lib/puppet/lib/puppet/parser/functions/test.rb and the puppet apply will "see" the function.

Avoiding dependency load order

I am writing a gem that looks as such:
lib/my_gem.rb:
require 'base64'
require 'ostruct'
require 'my_gem/utils.rb'
require 'my_gem/base.rb'
...
This has been fine until recently when the gem has added more functionality and the lib/my_gem directory has grown and grown.
Now, I'm having to be really careful to require my classes and modules in a very specific order because something in utils requires that base.rb be loaded first. However, something in base.rb requires that app.rb be loaded before that.
So it turns into:
# require all standard libraries first
require 'base64'
require 'ostruct'
require 'my_gem/app.rb' # be sure this is loaded before base!
require 'my_gem/base.rb' # be sure this is loaded before utils!
require 'my_gem/utils.rb' # be sure this is loaded before some other class!
I end up having a mess in this file all due to order of dependencies and I feel like there has to be a better way?
Try using Kernel#autoload:
require 'base64'
require 'ostruct'
autoload :SomeModule, 'my_gem/app.rb'
autoload :AnotherModule, 'my_gem/base.rb'
autoload :SomeClass, 'my_gem/utils.rb'
The idea is that the source file is not loaded until the module/class defined in it is used, therefore you don't need to take care of the order of requiring source files.

selenium suite in ruby/rspec - how to run multiple cases and stay logged in?

I have a selenium suite in rspec.
How can I run multiple cases and keep the browser open and logged in between them?
I have a test suite in the selenium IDE that is comprised of about 20 unit cases.
I've exported both the individual test cases and the test suite file itself to ruby/rspec
I can run individual tests and they pass, e.g.
rspec spec/2day/units/set_QA_district_name_spec.rb
However when I tried to run the converted suite with either
rake spec
or
rspec spec/2day/complete_district_suite_spec.rb
it runs each spec one-by-one by bringing up (and then closing) the application for each test.
How can I run the suite and have the browser 'stay' up and logged in from test to test and not be opening and then closing down the browser window for each unit test that runs.
I have to run these tests in the standalone directory that I've created as they have to be kept separate from the rest of the applications test areas, in other words I have to avoid any use of rails itself.
I have set up the following Gemfile / spec_helper / Rake files in the root directory for these files as follows:
Gemfile:
gem 'rspec'
gem 'selenium-webdriver'
spec_runner.rb:
require "selenium-webdriver"
require 'rspec'
Rakefile
require 'rspec'
require 'rspec/core/rake_task'
desc "Run all examples"
RSpec::Core::RakeTask.new(:spec) do |t|
t.ruby_opts = %w[-w]
t.rspec_opts = %w[--color]
end
The contents of complete_district_suite.rb are:
ENV['RSPEC_COLOR'] = 'true'
require File.join(File.dirname(__FILE__), "units/set_QA_district_name_spec.rb")
require File.join(File.dirname(__FILE__), "units/set_file_uploads_source_location_spec.rb")
require File.join(File.dirname(__FILE__), "units/district_spec.rb")
require File.join(File.dirname(__FILE__), "units/select_district_spec.rb")
require File.join(File.dirname(__FILE__), "units/upload_service_types_spec.rb")
require File.join(File.dirname(__FILE__), "units/upload_services_spec.rb")
require File.join(File.dirname(__FILE__), "units/upload_grades_spec.rb")
require File.join(File.dirname(__FILE__), "units/upload_schools_spec.rb")
require File.join(File.dirname(__FILE__), "units/upload_classrooms_spec.rb")
require File.join(File.dirname(__FILE__), "units/upload_students_spec.rb")
require File.join(File.dirname(__FILE__), "units/upload_ieps_spec.rb")
require File.join(File.dirname(__FILE__), "units/upload_travel_spec.rb")
require File.join(File.dirname(__FILE__), "units/manual_add_student_spec.rb")
require File.join(File.dirname(__FILE__), "units/generate_basic_schedule_spec.rb")
require File.join(File.dirname(__FILE__), "units/wait_for_dss_to_finish_spec.rb")
require File.join(File.dirname(__FILE__), "units/view_schedules_spec.rb")
require File.join(File.dirname(__FILE__), "units/visit_first_schedule_spec.rb")
require File.join(File.dirname(__FILE__), "units/monday_1pm_new_appt_spec.rb")
require File.join(File.dirname(__FILE__), "units/tuesday_2pm_new_5_students_spec.rb")
require File.join(File.dirname(__FILE__), "units/wednesday_9am_5pm_new_1_student_spec.rb")
require File.join(File.dirname(__FILE__), "units/thursday_9am_10am_new_1_manual_add_student_spec.rb")
require File.join(File.dirname(__FILE__), "units/friday_10am_11am_new_5_students_spec.rb")
I think you are looking for before(:all)
describe "Search page" do
before(:all) do
#browser = Watir::Browser.new
end
it "should find all contacts" do
...
end
after(:all) do
#browser.kill! rescue nil
end
end
More info here
Suggestion
Also you could include all your spec files in one line by so :-
Dir[File.join(File.dirname(__FILE__), 'units') + "/*_spec.rb"].each { |file|
require file
}

Set / change + add Sinatra views folder

I am learning sinatra, and I am trying to create simple website. This is my web directory tree:
├── app.rb
│
├── admin
│   └── views
│  └── admin.rb
├── models
├── static
│  
└── views
and now I want render views just for admin. In other words: I have 2 views folder in different locations, admin for admin controller and views, and another views is for homepage.
Add config.ru file in root application folder
require './app'
require './admin/admin'
# run MyApp
run Rack::URLMap.new("/" => MyApp.new, "/admin" => AdminApp.new)
In app.rb
require 'sinatra'
require 'haml'
class MyApp < Sinatra::Base
get "/app" do
haml :app
end
end
In admin.rb
# admin.rb
class AdminApp < Sinatra::Base
get "/" do
haml :index
end
end
Finally in console rackup -p PORTNUMBER example
rackup -p 4000
Update
Reference to Gist

issues finding public folder when requiring sinatra/base

I have found that in my Sinatra app, when I require 'sinatra', I can access my public folder as expected, but when I require 'sinatra/base' I can't. Here is my relevant code (which works until I change to /base):
config.ru
root = ::File.dirname(__FILE__)
require ::File.join( root, 'app' )
run MyApp.new
app.rb
require 'sinatra'
require 'sinatra/namespace'
require 'haml'
class MyApp < Sinatra::Application
# ...
end
require_relative 'models/init'
require_relative 'helpers/init'
require_relative 'routes/init'
script.haml
%script(type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js")
%script(type="text/javascript" src="/js/table.js")
%link(rel="stylesheet" type="text/css" href="/css/table.css")
And yes, I have the correct directory structure in place. Like I said, it works using require sinatra. Anyone know why this is occurring and what I can do to fix it?
Requiring Sinatra::Base does not set any of the default configuration settings that requiring Sinatra does. You'll need to set :public_folder ... to a suitable value yourself, e.g:
set :public_folder, 'public'

Resources