How to read values from _config.yml file in Jekyll hook - ruby

I am trying to read specific part of _config.yml in my Jekyll hook method something like this:
Jekyll::Hooks.register :site, :after_init do
lm = Jekyll.config("latex-macros")
end
in _config.yml is:
latex-macros:
- ["\\RR", "\\mathbb{R}"]
so in lm variable should be:
[["\\RR", "\\mathbb{R}"]]
I already tried to to use Jekyll.configuration({})["latex-macros"] and it kinda worked but it ignores --config terminal option and reads file everytime it is called. This makes it unusable for me.
I also tried
Jekyll::Hooks.register :site, :after_init do
lm = context.registers[:site].config["latex-macros"]
end
but it throws run time error:
katex.rb:8:in '<top (required)>': undefined local variable or method 'context' for main:Object (NameError)
My question is, how to read _config.yml values in jekyll hook properly? How do I fix second method?
Thank you for your help

I am writing this from top of my head since it's been a long time I used Jekyll. You need to pass site variable into hook.
Jekyll::Hooks.register :site, :after_init do |site|
# Access using site.config[key]
puts site.config['latex-macros']
end

Related

Not able to call method in a gem

This might be an easy question but I was unfortunately not able to find the answer on Google.
Context:
I am working on a project of my own, and I am externalizing some code in a gem (FrenchTaxSystem). It is the first I create a gem and I have difficulties using it properly.
Problem:
When calling a method (like testit) defined in the main file (french_tax_system.rb) of my gem I get a "NoMethodError: undefined method `testit' for FrenchTaxSystem:Module", though I can call constants from this same file (like FISCAL_NB_PARTS_FOR_MARRIED_COUPLE) and it puzzles me.
E.g in IRB I get that when calling a method:
[
And it is the same in my Rspecs tests inside my gem
However when calling a constant I have no error:
Main file in my gem:
french_tax_system.rb
module FrenchTaxSystem
class Error < StandardError; end
# Constants
...
FISCAL_NB_PARTS_FOR_MARRIED_COUPLE = 2
...
# Methods
## Main method
def testit
"test me"
end
end
Gem file structure:
Thank you in advance for your help,
Mth0158
This should work:
module FrenchTaxSystem
def self.testit
"test me"
end
end

How to parse and dump Ruby config files?

In this blog post he gives this example of a Ruby config file.
config do
allow ['server.com', `hostname`.strip]
vhost 'api.server.com' do
path ‘/usr/local/api’
end
vhost 'www.server.com' do
path '/usr/local/web'
end
%w{wiki blog support}.each do |host|
vhost "#{host}.server.com" do
path "/usr/local/#{host}"
end
end
end
I think of a hash after a config file have been loaded, but maybe that is not how this type of configs are intended for...
Update
If I execute it, I get
$ ruby config.rb
config.rb:2:in `<main>': undefined method `config' for main:Object (NoMethodError)
Question
What Ruby code is needed to parse and dump the content of this config file?
That config example is not directly loadable and, if I understand the blog post author correctly, it's not meant to be either so there's no easy way of loading/parsing that example.
The key part is in the blog post where he states "build simple DSLs to design semantically robust config files without the underlying ruby being conspicuous" (my emphasis). The 'underlying ruby' I take to mean the code that enables the DSL elements you're seeing such as 'config' and 'vhost'.
Your original question was, however, what code is required to load that config - below is a sample of something would work, full implementation is up to you and tbh I'm pretty sure there are cleaner, "better" ways of doing the same.
class AppConfig
attr_accessor :hosts
def allow(hosts)
#hosts = hosts
end
def vhost(hostname)
end
def process_config(&block)
instance_eval(&block)
end
end
def config(&block)
config = AppConfig.new
config.process_config &block
puts "Hosts are: #{config.hosts}"
end
load 'config.rb'

Rails uninitialized constant in initializers

I've taken over a working Rails 3 app from an offshore supplier and rails console is failing at this line:
Settings.defaults[:processing_fee] = '0.99'
in the file config/initializers/settings.rb
I've compared this file with what is in git blame and it matches. I've removed the contents of this file and it runs so it doesn't like this line. Reading on SO I've made the filename and constant Singular. Following another SO post I created another file in config/application_settings.rb. The constant could not be found in console. Moving it to /initializers yielded on rails console
/Users/sam/apps/tickat/config/initializers/application_settings.rb:1:in `<top (required)>': uninitialized constant SETTINGS (NameError)
from this content:
SETTINGS[:processing_fee] = '0.99'
It appears that something about my environment is not accepting my constants here. I first noticed this pushing to Heroku and can replicate this error in development in console. I've asked around and am stuck. I'm sure it's something I goofed on, sam
It might be that someone forgot to commit a file in the repo of your application.
If this is the case, and you can't get the file from the author, you need to reverse engineer Settings. I would make it an empty module:
module Settings
def self.defaults
#defaults ||= {}
end
end
And see how far you can get, before you get more errors...
EDIT: You can see in the console how this Hash is initalized:
irb(main):008:0> Settings.defaults
=> {}
irb(main):010:0> Settings.defaults[:a] = 1
=> 1
irb(main):011:0> Settings.defaults
=> {:a=>1}

Ruby-Processing : Undefined method error

I am trying to run a ruby-processing file
vishrut#vishrut-XPS-L501X:~/Twitter-Emotion-Graphs/samples/contributed$ rp5 run sand_traveler.rb
undefined method `current' for Processing::App:Class
./sand_traveler.rb:63:in `initialize'
./sand_traveler.rb:43:in `reset_all'
org/jruby/RubyFixnum.java:256:in `times'
./sand_traveler.rb:40:in `reset_all'
./sand_traveler.rb:22:in `setup'
/var/lib/gems/1.8/gems/ruby-processing-1.0.11/lib/ruby-processing/app.rb:211:in `handleDraw'
Context and code for sand_traveler.rb : http://ashkenas.com/codework/ruby-processing.html
I am pretty sure all necessary modules have been installed (java, ruby, ruby-processing)
I am able to run other other ruby-processing .rb files using rp5. Only this one is showing errors.
PS: Does the file location matter? The file is in my home folder.
Try replacing line 63 of sand_traveler.rb:
#app = Processing::App.current
with:
#app = $app
I get the same error as you, and this fixed it for me. ($app is global handle to the running sketch, which is also what Processing::App.current should do -- see CHANGELOG entry for v0.3).
As to why it doesn't work -- it seems this method has been removed from the latest version of ruby-processing. If you look at version 0.8 for example, Processing::App defines a class method on line 40 of ruby-proccessing.rb:
def self.current; #current_app; end
Such a method is missing in the latest version.

How can I get rid of the following warning: Problem while setting context on example startundefined local variable or method `selenium_driver'

Still making my first steps in Ruby (while dealing with some written code). I am getting the following warning each time I run spec (listed as is):
Problem while setting context on example startundefined local variable or method `selenium_driver' for #<Spec::Example::ExampleGroup::Subclass_1::Subclass_1:0x7f2d2cd840e0>
(Edit: Split into two lines, it says)
Problem while setting context on example start
undefined local variable or method `selenium_driver' for #<Spec::Example::ExampleGroup::Subclass_1::Subclass_1:0x7f2d2cd840e0>
While grep-ing through Ruby code - could find the following:
/home/user/.rvm/gems/ruby-1.8.7-p334#frontend/gems/selenium-client-1.2.18/lib/selenium/rspec/spec_helper.rb: STDERR.puts "Problem while setting context on example start" + e
So here is the excerpt from the source code of spec_helper.rb:
config.append_before(:each) do
begin
if selenium_driver && selenium_driver.session_started?
selenium_driver.set_context "Starting example '#{self.description}'"
end
rescue Exception => e
STDERR.puts "Problem while setting context on example start" + e
end
end
Kindly advise how can I solve the (potential) problem.
Update: This grep might be helpful as well:
user#vm-ubuntu:~/dev/branch/tests$
grep selenium_driver *
my_module.rb: #selenium_driver = driver
my_module.rb: ['TERM', 'INT'].each {|s| Signal.trap(s) { #selenium_driver.stop && Process.exit(1) } }
my_module.rb: return #selenium_driver
Update N2:
My Gemfile:
source "http://rubygems.org" # Default source
gem "hpricot", "~>0.8.4"
gem "json", "~>1.5.1"
gem "rspec", "~>1.3.2"
gem "selenium-client", "~>1.2.18"
My selenium_helper.rb file:
require 'selenium/client'
require "selenium/rspec/spec_helper"
...
The problem is that selenium-client gem expects that you name your driver object 'selenium_driver' and make it visible from the spec.
For example if you initialize selenium like this:
before(:all) do
#driver = create_driver($hub_url, $hub_port, $browser)
#driver.start_new_browser_session
end
You need to change it to look like this:
attr_reader :selenium_driver
before(:all) do
#selenium_driver = create_driver($hub_url, $hub_port, $browser)
#selenium_driver.start_new_browser_session
end
Basically it's same code just different variable name. The selenium-client uses that convention to apply context information to the tests.
It's saying it can't find the variable selenium_driver.
Problem while setting context on example startundefined local variable or method `selenium_driver' for #<Spec::Example::ExampleGroup::Subclass_1::Subclass_1:0x7f2d2cd840e0>
is made up of the string "Problem while setting context on example start" plus the exception error message (what's produced by e) of
"undefined local variable or method `selenium_driver' for #<Spec::Example::ExampleGroup::Subclass_1::Subclass_1:0x7f2d2cd840e0>"`.
Add
gem "selenium-client"
to your Gemfile (don't forget to run $ bundle install)
And add following to spec/spec_helper.rb
require "selenium/client"
require "selenium/rspec/spec_helper"

Resources