Ruby / MongoDB : Exclude certain results in Find - ruby

I'm typically using:
#coll.find({"lang"=>#language,"description"=>#description,"location"=>#location},{:limit=>#results_needed}).to_a
But there are times when I have an array of "_ids", that I don't want to be included in the results. Is there a native way to do that? I've been doing a hack with .delete_if but I would like to keep the database doing as much work as possible.

What about
#coll.find(:id.ne => array_of_ids)
or
#coll.find(:id => {:$ne => array_of_ids})
From Not equals in mongo mapper.

Related

Laravel 5.3 and Redis (predis) - autoincrement hash and delete hash `row`

I've been flirting with Redis for a while now.
I've watched these series some time ago and they were awesome. I've been through some of the documentation and the mentioning of the Time complexity of the queries blew me away, this is something that's rarely mentioned in web materials but is of huge importance for app building.
Anyhow I'm trying to make my app use the Redis on the consumer end so the users can fetch the data as fast as possible.
So I'm trying to save some objects to hash as:
$redis->hmset("taxi_car", array(
"brand" => "Toyota",
"model" => "Yaris",
"license number" => "RO-01-PHP",
"year of fabrication" => 2010,
"nr_stats" => 0)
as found here and this works nicely.
However I can't find a way to delete the whole entry anywhere.
Did I get this hash thing wrong?
Following this example I would like to delete the entry with given licence number. All I could find is how to delete the licence number from the object:
$redis->hdel("taxi_car", "license number");
and can't figure out how to delete the whole hash row (please do correct with proper word for row here).
Another problem here is that it seems this only allows me to save a single taxi_car in the Redis. How do I set the UUID so I can have multiple Taxi cars?
I'm going to play with this a bit, any help is welcome. Thanks!
To delete a key of any type, Hash included, call the Redis DEL command.
To have multiple keys, give them different names, e.g. taxi_car:1, taxi_car:2 etc.

Unable to adjust Ruby block

I am fairly new to Ruby, JRuby, etc...
I started to work on migration of certain bash scripts into Ruby, now I used a block from a different pipeline, but here its unnecessary to use it in this format as there is no need to iterate through array:
all_cf = %w(
customers
).map do |table_name|
schema("columns.#{table_name}.hbase.families").gsub(/'/,'')
end.uniq.map{|s| "{ NAME => '#{s}', VERSIONS => 1 }" }.join(',')
Is there an easier way to replace that array iteration and just replace #{table_name} with customers?
I tried this:
all_cf = task do
schema("columns.customers.hbase.families").gsub(/'/,'')
end.uniq.map...
But that just throws error and tried couple of more forms of this, but I think I still don't have a right understanding of the Ruby grammar, as I come from the PHP background I'm still struggling with this, anyone any idea how, and maybe an explanation why?
Cheers...
This produces the result that your original script produced:
"{ NAME => '#{schema("columns.customers.hbase.families").gsub(/'/,'')}', VERSION => 1 }"
I'm not sure what task is, so I don't know whether you should expect your code to work. uniq is a method on Array which returns an Array with all duplicates removed:
> [1,2,3,1,2,1].uniq
# => [1,2,3]
Similarly you can look up what map and join do.

How to create unique ID in format xx-123 on rails

is it possible to create some unique ID for articles on rails?
For example, first article will get ID - aa-001,
second - aa-002
...
article #999 - aa-999,
article #1000 - ab-001 and so on?
Thanks in advance for your help!
The following method gives the next id in the sequence, given the one before:
def next_id(id, limit = 3, seperator = '-')
if id[/[0-9]+\z/] == ?9 * limit
"#{id[/\A[a-z]+/i].next}#{seperator}#{?0 * (limit - 1)}1"
else
id.next
end
end
> next_id("aa-009")
=> "aa-010"
> next_id("aa-999")
=> "ab-001"
The limit parameter specifies the number of digits. You can use as many prefix characters as you want.
Which means you could use it like this in your application:
> Post.last.special_id
=> "bc-999"
next_id(Post.last.special_id)
=> "bd-001"
However, I'm not sure I'd advice you to do it like this. Databases have smart methods to avoid race conditions for creating ids when entries are created concurrently. In Postgres, for example, it doesn't guarantee gapless ids.
This approach has no such mechanism, which could potentially lead to race conditions. However, if this is extremely unlikely to happen such in a case where you are the only one writing articles, you could do it anyway. I'm not exactly sure what you want to use this for, but you might want to look into to_param.
You may want to look into the FriendlyId gem. There’s also a Railscast on this topic which covers a manual approach as well as the usage of FriendlyId.

How to process Bootstrap with less.rb outside of Rails

I'm attempting to create a standalone application (independent of the Rails asset pipeline) using less.rb to output CSS files based upon Twitter Bootstrap.
The following results in an empty document
parser = Less::Parser.new :paths => [Rails.root + '/public/bootstraps/twitter-bootstrap-857b8fb/less']
tree = parser.parse("#import 'bootstrap.less'")
tree.to_css
Which results in an empty string being returned. I've tried variations of altering the #import to be the full path etc, with no success. I think I must be missing something simple.
I believe you have an issue with how you are specifying your path. As far as I can tell, Less is looking for an array of String objects, not Path obejcts.
Use the following:
parser = Less::Parser.new paths: [Rails.root.join('public', 'bootstraps', 'twitter-bootstrap-857b8fb', 'less').to_s]
tree = parser.parse("#import 'bootstrap.less'")
tree.to_css
You can simple run make as you have described here: https://github.com/twitter/bootstrap/wiki/Contributing-to-Bootstrap
If performance is not of utmost concern, you can always include less.js, which will compile the less files at runtime. Detailed instructions here.
Have you considered using the filename syntax from the less.rb github page? https://github.com/cowboyd/less.rb/
parser = Less::Parser.new :paths => ['./lib', 'other/lib'], :filename => 'mystyles.less'

Get more than one value from memcache with Ruby

I installed *memcache_client* GEM Ruby from http://seattlerb.rubyforge.org/memcache-client/
It's easy to get a single value:
cache.get('foo', 'bar')
How to get all values, starting with 'foo', for example foo_1, foo_2, foo_3, foo_* ?
Something like "SELECT * FROM foo", but for Memcached.
There will be about 10 000 "foo_n" entries.
Not a perfect solution, but look at the get_multi function:
keys = (1..10_000).map{ |n| "foo_#{n}" }
data = cache.get_multi(*keys)
Unfortunately memcached doesn't support regex key lookups, or even let you get a list of all the keys to process on your own. One alternative would be to use Redis which can get a list of keys using a glob style pattern.
Might want to look at Redis as an alternative to memcache. It supports lists, sets, sorted sets and hashes. http://code.google.com/p/redis/

Resources