Iterating over yaml data in Middleman using Slim - slim-lang

I've got the following index.slim file:
---
title: Welcome to my site
---
ul.nav.nav-tabs
= data.about.team
The output is ["John Doe", "Jane Monroe"], which is great.
Now how the heck do I iterate through this data using slim with Middleman?
I've tried the following but that doesn't work:
ul.nav.nav-tabs
= data.about.team.each do |person|
li = person

Found the answer. Had to use - instead of that first =:
ul.nav.nav-tabs
- data.about.team.each do |person|
li = person
That's it. The - is used for executing Ruby code, and = for printing the result of a Ruby operation.

Related

Show square brackets in yml file

How to generate content in yml file with this format:
required_groups:
- ["member", "cn=serviceboard-users,cn=groups,cn=accounts,dc=int,dc=dostack,dc=io"]
Instead of this format:
required_groups:
- - member
- cn=serviceboard-users,cn=groups,cn=accounts,dc=int,dc=dostack,dc=io
Input:
[["member", "cn=serviceboard-users,cn=groups,cn=accounts,dc=int,dc=dostack,dc=io"]]
Current Code:
File.open("config/ldap/#{Rails.env}.yml", "w") do |file|
data = {}
formatted_groups_array = []
Setting.ldap_users_filter_required_groups.each { |group| formatted_groups_array.push([Setting.ldap_group_membership_attribute.to_s, group.to_s])}
data["required_groups"] = formatted_groups_array
file.write(data.to_yaml)
end
As stated in this answer, the default ruby library Psych does not have many options available, and you cannot customize the output the way you are trying to do.
In the meantime I've found an old possibile custom solution to your problem, but I had to rewrite a small part of it to make it compatible to the most recent ruby version (I am using 2.7.0 for this example). This is my updated script. Basically it will introduce a new way to edit the YAML output using a more concise syntax.
You can use it this way:
File.open("config/ldap/#{Rails.env}.yml", "w") do |file|
data = {}
formatted_groups_array = []
Setting.ldap_users_filter_required_groups.each { |group| formatted_groups_array.push([Setting.ldap_group_membership_attribute.to_s, group.to_s])}
data["required_groups"] = formatted_groups_array
styled_yaml = StyledYAML.inline(data)
file.write(StyledYAML.dump(styled_yaml))
end

Nokogiri - Selecting a link between two &quot s

I'm having trouble selecting a link that's part of an embeded code. It's between two &quots. I've put a link to the code that I'm trying with.
Thank you.
https://gist.githubusercontent.com/anonymous/d2efee7b3967debc531d67de9cc7993a/raw/3f5e0e63ce6fe3aa4cffc2d0afafd2415408308a/gistfile1.txt
The Correct Form is.
(?<=")(.*)(?=")
Input:
"https://gist.githubusercontent.com/anonymous/d2efee7b3967debc531d67de9cc7993a/raw/3f5e0e63ce6fe3aa4cffc2d0afafd2415408308a/gistfile1.txt"
Output:
https://gist.githubusercontent.com/anonymous/d2efee7b3967debc531d67de9cc7993a/raw/3f5e0e63ce6fe3aa4cffc2d0afafd2415408308a/gistfile1.txt
Ruby Code:
re = /(?<=")(.*)(?=")/m
str = '"https://gist.githubusercontent.com/anonymous/d2efee7b3967debc531d67de9cc7993a/raw/3f5e0e63ce6fe3aa4cffc2d0afafd2415408308a/gistfile1.txt"'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
Test Code: http://ideone.com/moljMo
See: https://regex101.com/r/vrBUhc/1

ruby on rails select a variable name using loop

There's a similar answer but it won't apply to me, but maybe will help someone: here
So, I have a loop and need to input a value into each of 20 variables, called product1, product2, ... , product20.
Im using nokogiri to change the values from a page, and manually it works:
li.content = #site.product1
li = #doc.css('li')[1]
But to avoid code repetition and also I have more cases like that one in my app im trying to make a loop, but it won't work since now.
What it need to do:
(1..20).each do |i|
li = #doc.css('li')[i]
li.content = #site.producti
end
Thanks
TRy
(0...20).each do |i|
li = #doc.css('li')[i]
li.content = #site.send("product#{i}")
end

CSV export customization

I have the following piece of code which export a csv file.
- if #product.type_id == 2
= "#{l(:user)} ID , #{l(:name)} , #{l(:company)} , #{l(:item)} , #{l(:total)} , #{l(:order_time_stamp)}"
- orders = #product.orders
- orders.each do |order|
- order_users = get_user_who_order order
- order_users.each do |order_user|
- user = order_user[:user]
- products , op_total = get_products_and_total order_user[:order_products]
- company = user.company.present? ? user.company.name : ''
- products = products.join('. ')
But we have list of items which is currently rendering in a single column however we need it to be in the different columns like item1, item2, item3 and so on.
Seems a bit crazy to me to do this with haml. There is a nice CSV library in Ruby you can use for this instead.
If you still insist on doing it with haml what you are missing is printing the extra , - you need to this yourself when using haml like this. I don't see any = in your loop at all. How is it possible that you get anything printed at all? (Except the header?)

read an array on ruby on rails

I'm trying to read this array on RoR:
> importer_name = [#<RouteImporter id: 1, name: "aa", filename: "aa1", type: "RouteImporter">]
I just want to get the filename character "aa1", I tried with importer_name[2] but I didn't get nothing and I don't want 'filename: "aa1"' I just want "aa1", any idea? Thanks in advance!
you have a Ruby object stored in an array. You can access it like this(If I understand you correctly):
importer_name.first.filename

Resources