I'm writing a fluentd plugin and came across desc. From the docs I see that it means describe.
desc 'Postgres username'
config_param :user, :string
desc 'Postgres password'
config_param :password, :string, :secret => true
Take a look at the example above. I'm curious if desc is ever used by Fluentd Engine? Because when I run fluentd from a config file I don't see desc being used anywhere, at-least from the output I see. What is the purpose of this? Why not use comments instead?
Another example of desc being used is shown here.
desc is a parameter typical in ruby DSLs as is commonly found in rake or Rspec for example. You're writing a plugin which I assume is to be used in conjunction with fluentd. A simple search yields results such as .erb files which render string/text data. This is one example of where such parameters could be used.
Looking at this answer may also provide more insight.
Related
Thank y'all all within the community and the moderators for being so cool and willing to help so quickly! Just wanted to lead in with that. So I could really use some help with this basic automation script that I am running. I am trying to select the search bar on Google.com and enter some text. I have gotten some help from friends but they were stuck as well. But it learning this I was hoping to get some help from the experts and just ask these questions that I have because Google ain't got shit!
1) How to select the search field and enter text.
Mine looks something like this I've tried xpath, different values, id's classes.
require 'ruby'
require 'watir-webdriver'
browser = Browser::browser.new :firefox
browser.goto 'http://google.com'
browser.text_field(:value => 'Search').set('google search')
2) When I inspect the element and find unique characteristics to that value (i.e. href, id, title, class, name), which are the ones that I can actually utilize to call either the button, text_field, or link?
3) I understand html and css pretty well. Can someone please explain how to properly utilize xpath?
Y'all rock, I feel like there are tons of people out there who have these same questions as I do, and can't find the damn answers anywhere, so I ask all of you automation experts, would you mind dropping a knowledge bomb and learnin us?
There are a number of errors in this script. Here's a working version:
# require 'ruby' # don't require ruby
require 'watir-webdriver' # corrected typo in gem name
browser = Watir::Browser.new :firefox # corrected Browser::browser.new
browser.goto 'http://google.com'
browser.text_field(:title => 'Search').set('google search') # changed :value to :title
In terms of identifying page elements, it's generally considered good practice to use the id attribute since it's unique to the page. You can use the attributes that you've listed, but they have to exist as attributes for the given HTML element. AFAIK, watir-webdriver supports using the majority of standard HTML tag attributes for location and can also locate elements based on their index, via regular expression, and by combining multiple locators. For example, you could substitute any of these in the script above:
browser.text_field(:title => /Search/).set('google search')
browser.text_field(:class => 'gsfi').set('google search')
browser.text_field(:id => 'lst-ib').set('google search')
browser.text_field(:name=> 'q', :class => 'gsfi').set('google search')
If you haven't already, I'd suggest checking out http://watirwebdriver.com/ and https://github.com/watir/watir/wiki. And if you're curious about using xpath to find tricky elements, check out
https://jkotests.wordpress.com/2012/08/28/locate-element-via-custom-attribute-css-and-xpath/.
We are trying to incorporate CSV into Cucumber to get the benefit of CSV files, but when researching online I see no official documentation on this feature but only "Import scenario outline 'examples' from CSV?".
I am not sure if this comes to any conclusions yet. Is it currently any built-in way to use CSV in Cucumber?
If currently there is no built-in way to import CSV, and I have to write the own parsing method, I think my question will be, in the step definition, how do I hook up the variables with the definition in scenario? For example:
Scenario: do something
Given I eat number as <number> cucumbers
and the cucumber is produced at date as <date>
When the cucumber is expired
Then I should have diarrhea
data.csv
number,date
1,2012-01-01
1,2012-11-03
in the steps.rb, if I do:
CSV.foreach("path/to/data.csv") do |row|
...
How to I map row.number`row.date` to the number\date in the feature file?
In order to use the Examples functionality in cucumber I think you'd have to do some significant metaprogramming that would probably reduce the maintainability and readability of your features. I'd probably accomplish this by wrapping you current steps in another step definition like so:
Scenario: do something
Given I load some data
Then expired cucumbers should give me diarrhea
and the define the step definitions
Then 'I load some data' do
#data = []
CSV.foreach("path/to/data.csv", headers: true) do |row|
#data << row
end
end
Then 'expired cucumbers should give me diarrhea' do
#data.each do |row|
step %Q|I eat number as #{row[:number]} cucumbers|
step %Q|the cucumber is produced at date as #{row[:date]}|
step %Q|the cucumber is expired|
setp %Q|I should have diarrhea|
end
end
The only problem with this is that if one scenario fails, it may take an extra debugging step to figure out which one is failing. Since these steps are run, more or less, under the hood. You could do this pretty easily by printing the row to STDOUT from the step definition:
Then 'expired cucumbers should give me diarrhea' do
#data.each do |row|
puts "row", row.inspect
step %Q|I eat number as #{row[:number]} cucumbers|
step %Q|the cucumber is produced at date as #{row[:date]}|
step %Q|the cucumber is expired|
setp %Q|I should have diarrhea|
end
end
That should give you some indication of which row is giving you trouble.
Erata: I understand your desire to be able to maintain a data sheet separate from the features so that someone like a Project Manager can come up with edge cases and then have those run against the behavior of the code. But I'd almost be more willing to allow them to edit the feature on github and allow CI to run those examples they added to the example table rather than do this approach. In either case its probably something good for regression, but probably really painful to develop code against. This type of testing is sort of the aim of http://fitnesse.org/ and that might be a project of inspiration for you.
Experimenting with pushing my first gem to rubygems.org, and I'm trying to figure out how to generate online documentation for it. For most gems 'show' page, when I click the 'Documentation' link, I am brought to http://rubydoc.info/gems/gemname/version/frames. Is this something that will happen automagically if I generate the docs in the right place? Do I have to specify something in the gemspec? Thanks!
rubydoc.info will automatically generate API docs for your gem. At a minimum, those docs will contain the signature of all classes, modules and methods, as well as the comments you put before each method and class.
If you add yardoc markup to your files, the documentation will get better. Here's a small bit of yardoc markup for a method:
# Validate the value.
# #param long_mailer_id truthy if the mailer ID is long (9 digits).
# #raise ArgumentError if invalid
def validate(long_mailer_id)
yardoc, the documentation engine that rubydoc.info uses, will look for comments in a certain format and format them into spiffy documentation.
You can also add, to the root of your gem, a README.md in Markdown format. This will be formatted and used as the "main page" of your documentation.
Here is the response that I received from someone in the RubyDoc community:
Hi there,
New gems can take up to a day to make it into RubyGems' master gem
list. Not much we can do about this one. From then on, it's about an
hour for new versions of your gem to be populated into the list (we
run a cron job at *:15 to update our copy).
It was a matter of letting enough time elapse.
I'm using HAML to generate some static html pages for a site, and I was wanting to split out common components into partials that I can include in multiple pages, just like in Rails. However I don't want to use the whole Rails stack to do this as it seems like overkill.
I've looked around on the Internet but haven't found anything, better than just doing something like:
Haml::Engine.new(IO.read("header.haml")).render
Is there a nicer way of including so-called partials from within HAML? An existing filter or command that I'm missing?
It's best to combine haml & sass with a tool for building static websites. Here's some links:
StaticMatic
Serve
Haml Jekyll -- a fork of Jekyll that supports haml.
Middleman
I'm using jekyll for my blog, but if you're not building a blog it's probably not appropriate for your needs.
Darn, you're right – there isn't a built-in way. I've used helpers with the haml command line, but always ones whose output was already HTML formatted.
My best suggestion is to write a partial() method and require it. It looks like you have already started down this path. I would suggest anyone writing such a function consider keeping the original binding around in some way. Haml::Helpers#capture_hame looks to be the easiest way to make this happen.
If execution speed is an issue, it might also be a good idea to cache some of the parsed template in the same way that Merb does it.
If someone does get some code working, please put it up on GitHub and make a comment here.
I finally found what I was looking for, so I thought I'd share my find with you. you can simply use the same syntax you use for haml in the first place - http://www.semicomplete.com/blog/geekery/partials-in-haml-in-sinatra.html
/views/file.haml
%h3 Title
%div= haml :content, :layout => false
/views/content.haml
%p your content here
All of the solutions seemed bulky for my purposes, though I'm trying to do basically the same thing. Here is a ruby script I wrote that does precious little - evaluates Haml with the option to stick in = partial "test.haml" anywhere you like. It does a trivial amount of logic to try and find a partial file.
require 'haml'
def find filename
return filename if File.exists? filename
path = File.dirname ARGV[0]
filename = path+"/"+filename
return filename if File.exists? filename
throw "Could not find file."
end
def partial filename
source = File.read(find(filename))
engine = Haml::Engine.new(source)
engine.render(binding)
end
puts partial ARGV[0]
you execute it like so : ruby hamlize.rb input.haml > output.html
I had the same problem. I needed to generate some html snippets to optimize the server response, once this snippets have a lot of calculations(for drawing graphs) and every time a user do a request it was doing all the stuff unnecessarily.
Once i have the views partitioned in several haml partials, i wanted to reuse this to generate the html files.
My first approach was trying with the Haml engine, but once i have partials rendering other partials and some functions of the application helper, it didn't worked.
Finality i found a solution that passed by creating a function in the models that i wanted to create an html file ( to_html ) Here is it:
def to_html
ActionView::Base.send :include, ActionView::Helpers::ApplicationHelper
File.open(File.join(Rails.root, "public", 'test.html'), "w") do |file|
file.print ActionView::Base.new(Rails.configuration.paths["app/views"].first).render(
:partial => 'partial_folder/partial',
:format => :html,
:locals => { :model => self}
)
end
end
This way, i was able to use the already done partials, to print the html files.
This worked for me, maybe it can help you also.
I need to store some simple properties in a file and access them from Ruby.
I absolutely love the .properties file format that is the standard for such things in Java (using the java.util.Properties class)... it is simple, easy to use and easy to read.
So, is there a Ruby class somewhere that will let me load up some key value pairs from a file like that without a lot of effort?
I don't want to use XML, so please don't suggest REXML (my purpose does not warrant the "angle bracket tax").
I have considered rolling my own solution... it would probably be about 5-10 lines of code tops, but I would still rather use an existing library (if it is essentially a hash built from a file)... as that would bring it down to 1 line....
UPDATE: It's actually a straight Ruby app, not rails, but I think YAML will do nicely (it was in the back of my mind, but I had forgotten about it... have seen but never used as of yet), thanks everyone!
Is this for a Rails application or a Ruby one?
Really with either you may be able to stick your properties in a yaml file and then YAML::Load(File.open("file")) it.
NOTE from Mike Stone: It would actually be better to do:
File.open("file") { |yf| YAML::load(yf) }
or
YAML.load_file("file")
as the ruby docs suggest, otherwise the file won't be closed till garbage collection, but good suggestion regardless :-)
Another option is to simply use another Ruby file as your configuration file.
Example, create a file called 'options'
{
:blah => 'blee',
:foo => 'bar',
:items => ['item1', 'item2'],
:stuff => true
}
And then in your Ruby code do something like:
ops = eval(File.open('options') {|f| f.read })
puts ops[:foo]
YAML will do it perfectly as described above. For an example, in one of my Ruby scripts I have a YAML file like:
migration:
customer: Example Customer
test: false
sources:
- name: Use the Source
engine: Foo
- name: Sourcey
engine: Bar
which I then use within Ruby as:
config = YAML.load_file(File.join(File.dirname(__FILE__), ARGV[0]))
puts config['migration']['customer']
config['sources'].each do |source|
puts source['name']
end
inifile - http://rubydoc.info/gems/inifile/2.0.2/frames will support basic .properties files and also .ini files with [SECTIONS] eg.
[SECTION]
key=value
YAML is good when your data has complex structure but can be fiddly with spaces, tabs, end of lines etc - which might cause problems if the files are not maintained by programmers. By contrast .properties and .ini files are more forgiving and may be suitable if you don't need the deep structure available through YAML.
Devender Gollapally wrote a class to do precisely that:
...though i'd recommend better to use a YAML file.
Instead of the .properties style of config file, you might consider using YAML. YAML used in Ruby on Rails for database configuration, and has gained in popularity in other languages (Python, Java, Perl, and others).
An overview of the Ruby YAML module is here: http://www.ruby-doc.org/core/classes/YAML.html
And the home page of YAML is here: http://yaml.org