How can I tidy up code that sets and requests deep hash values in ruby?
For example, say I have a hash like this:
hash = {
user_settings: {
notifications: {
overdue_tasks: { enabled: true, duration: 30 },
created_overdue_tasks: { enabled: true, duration: 30 }
}
}
}
How can I avoid writing brittle access code like this:
hash[:user_settings][:notifications][:overdue_tasks][:duration] = 5
Also, is there a recursive symbolize_keys that will symbolize all keys and not just the top level?
I don't know such way to reduce to code to fetch the desired key/value, but I have a suggestion to make your hash plain by naming.
How about
hash = {
user_settings: {
overdue_task_notification_enabled: true,
overdue_task_notification_duration: 30,
created_overdue_tasks_enabled: true,
created_overdue_tasks_duration: 30
}
}
Then fetch it like
hash[:user_settings][:created_overdue_tasks_duration]
I think this arrangement looks easier to understand for peers and users.
Taking the help from the blog :- Ruby Nested Hash - Deep Fetch - Returning a (Default) Value for a Key That Does Not Exist in a Nested Hash :
class Hash
def deep_fetch(key, default = nil)
default = yield if block_given?
(deep_find(key) or default) or raise KeyError.new("key not found: #{key}")
end
def deep_find(key)
key?(key) ? self[key] : self.values.inject(nil) {|memo, v| memo ||= v.deep_find(key) if v.respond_to?(:deep_find) }
end
end
hash = {
user_settings: {
notifications: {
overdue_tasks: { enabled: true, duration: 30 },
created_overdue_tasks: { enabled: true, duration: 30 }
}
}
}
p hash.deep_fetch(:duration)
# >> 30
Related
I've got three hashes which I want to merge to base_options under new key - checks. Basically what I want to achieve is:
{
base_options,
checks: {
document_check,
identity_check,
dummy_check,
}
},
Below sample hash data:
dummy_check = {
dummy: {
enabled: true,
preferences: {
state: 0,
replay: true,
},
}
}
identity_check = {
identity: {
enabled: true,
preferences: {},
},
}
document_check = {
document: {
enabled: true,
preferences: {
face: false,
liveness: false,
docs_all: true,
},
},
}
base_options = {
send_email: true,
send_reminder: false,
reset_client_status: true,
}
So if I do base_options.merge!(checks: document_check.merge!(identity_check, dummy_check)) I will receive expected hash which is:
{
send_email: true,
send_reminder: false,
reset_client_status: true,
checks: {
document: {
...
},
identity: {
...
},
dummy: {
...
}
},
}
But this is not super flexible and I don't know if using .merge! two times in one line is not a crap. Are there any other alternatives?
I'm using Ruby 2.7 and Rails 6
Using merge! is fine and well understood. However, as you are setting a single key on your base_options hash, you can also simple use the hash accessor, i.e.
base_options[:checks] = document_check.merge!(identity_check, dummy_check)
Note that this will also change the document_hash object as merge! modified the receiver. If this is not desired, you can also use merge and return a new Hash. Thus could look like:
base_options[:checks] = document_check.merge(identity_check, dummy_check)
or equivalently
base_options[:checks] = {}.merge!(document_check, identity_check, dummy_check)
The latter option is slightly slower but might better show your intended behavior and is thus easier to understand to readers of your code.
If I understand correctly, you can try the Double Splat **. you can use like this:
base_options.merge(
checks: **document_check, **identity_check, **dummy_check
)
The answer is in your question, below can be a simple way of achieving the end result.
Below is after initializing values of base_options, document_check, identity_check,dummy check.
base_options = {
base_options: base_options,
checks: {
document_check: document_check,
identity_check: identity_check,
dummy_check: dummy_check,
}
}
=> {:base_options=>{:send_email=>true, :send_reminder=>false, :reset_client_status=>true}, :checks=>{:document_check=>{:document=>{:enabled=>true, :preferences=>{:face=>false, :liveness=>false, :docs_all=>true}}}, :identity_check=>{:identity=>{:enabled=>true, :preferences=>{}}}, :dummy_check=>{:dummy=>{:enabled=>true, :preferences=>{:state=>0, :replay=>true}}}}}
I have a ruby code block, as follows:
require "elasticsearch"
require "json"
search_term = "big data"
city = "Hong Kong"
client = Elasticsearch::Client.new log: true
r = client.search index: 'candidates', body:
{
query: {
bool: {
must: [
{
match: {
tags: search_term
}
},
{
match: {
city: city
}
}
]
}
}
}
It produces multiple returns like this one:
{"_index":"candidates","_type":"data",
"_id":"AU3DyAmvtewNSFHuYn88",
"_score":3.889237,
"_source":{"first":"Kota","last":"Okayama","city":"Tokyo","designation":"Systems Engineer","email":"user#hotmail.co.jp","phone":"phone","country":"Japan","industry":"Technology","tags":["remarks","virtualization big data"]}}
I want to iterate through it and extract various elements. I have tried
data = JSON.parse(r)
data.each do |row|
puts row["_source"]["first"]
end
and the error is:
no implicit conversion of Hash into String (TypeError)
What's the best way forward on this chaps?
I have the solution, I hope it helps somebody else. It took me hours of fiddling and experimentation. Here it is:
require "elasticsearch"
require "json"
search_term = "big data"
city = "Tokyo"
client = Elasticsearch::Client.new log: true
h = client.search index: 'swiss_candidates', body:
{
query: {
bool: {
must: [
{
match: {
tags: search_term
}
},
{
match: {
city: city
}
}
]
}
}
}
data = JSON.parse(h.to_json)
data["hits"]["hits"].each do |r|
puts r["_id"]
puts r["_source"]["first"]
puts r["_source"]["tags"][1]
puts r["_source"]["screened"][0]
end
The important thing seems to be to convert the elasticsearch result into something ruby friendly.
JSON.parse expects a String containing a JSON document, but you are passing it the Hash which was returned from client.search.
I'm not entirely sure what you are trying to achieve with that, why you want to parse something which is already a Ruby Hash into a Ruby Hash.
I am doing an mongodb aggregation using mongoid, using ModleName.collection.aggregate(pipeline) . The value returned is an array and not a Mongoid::Criteria, so if a do a first on the array, I get the first element which is of the type BSON::Document instead of ModelName. As a result, I am unable to use it as a model.
Is there a method to return a criteria instead of an array from the aggregation, or convert a bson document to a model instance?
Using mongoid (4.0.0)
I've been struggling with this on my own too. I'm afraid you have to build your "models" on your own. Let's take an example from my code:
class Searcher
# ...
def results(page: 1, per_page: 50)
pipeline = []
pipeline <<
"$match" => {
title: /#{#params['query']}/i
}
}
geoNear = {
"near" => coordinates,
"distanceField" => "distance",
"distanceMultiplier" => 3959,
"num" => 500,
"spherical" => true,
}
pipeline << {
"$geoNear" => geoNear
}
count = aggregate(pipeline).count
pipeline << { "$skip" => ((page.to_i - 1) * per_page) }
pipeline << { "$limit" => per_page }
places_hash = aggregate(pipeline)
places = places_hash.map { |attrs| Offer.new(attrs) { |o| o.new_record = false } }
# ...
places
end
def aggregate(pipeline)
Offer.collection.aggregate(pipeline)
end
end
I've omitted a lot of code from original project, just to present the way what I've been doing.
The most important thing here was the line:
places_hash.map { |attrs| Offer.new(attrs) { |o| o.new_record = false } }
Where both I'm creating an array of Offers, but additionally, manually I'm setting their new_record attribute to false, so they behave like any other documents get by simple Offer.where(...).
It's not beautiful, but it worked for me, and I could take the best of whole Aggregation Framework!
Hope that helps!
I have this hash:
response = '{"librairies":[{"id":1,"books":[{"id":1,"qty":1},{"id":2,"qty":3}]},{"id":2,"books":[{"id":1,"qty":0},{"id":2,"qty":3}]}]}'
in which I'd like to delete every librairies where, at least, one of the book quantity is null.
For instance, with this given response, I'd expect this return:
'{"librairies":[{"id":1,"books":[{"id":1,"qty":1},{"id":2,"qty":3}]}]}'
I've tried this:
parsed = JSON.parse(response)
parsed["librairies"].each do |library|
library["books"].each do |book|
parsed.delete(library) if book["qty"] == 0
end
end
but this returns the exact same response hash, without having deleted the second library (the one with id => 2).
You can use Array#delete_if and Enumerable#any? for this
# Move through each array element with delete_if
parsed["librairies"].delete_if do |library|
# evaluates to true if any book hash in the library
# has a "qty" value of 0
library["books"].any? { |book| book["qty"] == 0 }
end
Hope this helps
To avoid changing the hash parsed, you could do the following.
Firstly, let's format parsed so we can see what we're dealing with:
parsed = { "libraries"=>[ { "id"=>1,
"books"=>[ { "id"=>1, "qty"=>1 },
{ "id"=>2, "qty"=>3 } ]
},
{ "id"=>2,
"books"=>[ { "id"=>1, "qty"=>0 },
{ "id"=>2, "qty"=>3 } ]
}
]
}
Later I want to show that parsed has not been changed when we create the new hash. An easy way of doing that is to compute a hash code on parsed before and after, and see if it changes. (While it's not 100% certain that different hashes won't have the same hash code, here it's not something to lose sleep over.)
parsed.hash
#=> 852445412783960729
We first need to make a "deep copy" of parsed so that changes to the copy will not affect parsed. One way of doing that is to use the Marshal module:
new_parsed = Marshal.load(Marshal.dump(parsed))
We can now modify the copy as required:
new_parsed["libraries"].reject! { |h| h["books"].any? { |g| g["qty"].zero? } }
#=> [ { "id"=>1,
# "books"=>[ { "id"=>1, "qty"=>1 },
# { "id"=>2, "qty"=>3 }
# ]
# }
# ]
new_parsed # => { "libraries"=>[ { "id"=>1,
"books"=>[ { "id"=>1, "qty"=>1},
{ "id"=>2, "qty"=>3}
]
}
]
}
And we confirm the original hash was not changed:
parsed.hash
#=> 852445412783960729
I have some problems with a hash:
"commissions"=>
{"commission"=>
{"commissionID"=>"38767647",
"date"=>"2014-09-22",
"publisherID"=>"46272",
"domainID"=>"1173659",
"merchantID"=>"35216",
"commissionValue"=>110,
"orderValue"=>2095,
"currency"=>"USD",
"url"=>"http://www.asos.com"},
"commission5"=>
{
other params
}
How can I get the value of 'commissionValue'?
Use [] to get value of the hash by key.
h = {"commissions"=>
{"commission"=>
{"commissionID"=>"38767647",
"date"=>"2014-09-22",
"publisherID"=>"46272",
"domainID"=>"1173659",
"merchantID"=>"35216",
"commissionValue"=>110,
"orderValue"=>2095,
"currency"=>"USD",
"url"=>"http://www.asos.com"},
"commission5"=> { }
}
}
h["commissions"]["commission"]["commissionValue"]
# => 110