Ruby abstract database connector - ruby

I'm writing a Ruby gem that requires to make a very simple query to an unknown database among the next: sqlite, mysql, postgresql.
For now I'm using ActiveRecord to handle this idiosyncrasy but looks like a bit overwhelming for me since I'm not using 99% of the ActiveRecord power.
I need something to play with in the next way:
connection = AbstractConnection.new("adapter", "database" {, "username", "userpass"})
connection.query("select * from table")
Is there any gem that abstracts me of generating the connection to different adapters? Is it using ActiveRecord a good idea here?

If you don't want to use ActiveRecord, you could use:
Sqlite3 gem
MongoDB driver
Pg gem
MySQL driver for Ruby
ActiveRecord provides a much nicer DSL across databases, but if you don't need all the overhead then you might want to invest some time in picking up one of the above.
If, however, you think that you may want to change database type in the future, then I recommend you use ActiveRecord: your code will stay the same and the underlying framework will deal with the changed database type.
Hope this helps!

Related

inflections dont work after update of activerecord

I have updated active record from 3.2.12 to 4.1.4. Accordingly I have update activesupport also from 3.2.12 to 4.1.4. Now when I do data base queries (h2-data base) I cannot use
mrs = MeasurementResultSample.find_by_duration_and_measurement_result_id(duration, id)
I can oly use
mrs = MeasurementResultSample.where(:duration => duration, :measurement_result_id => id).first
The activerecord-jdbch2-adapter I have upgraded from 1.2.9 to 1.3.9
How can I make my 'find_by' methods using one or more fields working again?
Update:
actually it turned out, that find_by is working but I had to check if the table exsist with the method exists?. What wont work is find_last_by... or find_all_by... or find_first_by... .
As I understand it, Rails is moving away from certain 'magic' method names due to the poor performance of 'method_missing,' upon which they're based.
However, find_by works. So you can re-work your query with:
mrs = MeasurementResultSample.find_by(duration: duration, measurement_result_id:, id)
Amusingly, the implementation behind the scenes is:
def find_by(*args)
where(*args).take
end
http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_by
Edit:
There's a gem that includes Rails 4 deprecated finders:
https://github.com/rails/activerecord-deprecated_finders
Confusingly it states that find_by... methods aren't deprecated. However, if you've updated ActiveRecord outside Rails it might help.
If not, you could always look at the Rails 3 ActiveRecord code and port the relevant methods across.
Thanks to A Fader Darkly! I finally started to port the code to activerecord 4.1.4. I opted to change the finder methods to where(...).
Here I found some information too what-s-new-in-active-record-rails-4.
It's the cleanest way. I noticed, that I had to use the exist? method in some cases to make sure the table is not nil. I did not have to do this before (in 3.2.12) with inflections used.

Is there a lightweight, all purpose validation library/DSL for Ruby?

I'm doing a lot of bulk data-validation on various kinds of data sources and I find myself writing such boilerplate code:
if summed_payments != row['Total']
raise "The sum of the payments, #{summed_payments} != #{row['Total']}"
end
I was wondering if there was a way to apply a DSL, like Minitest, but to use it for purposes that didn't involve application testing? For example, finding and logging errors during a bulk data import and validation script...it's a quick-and-dirty script that I don't want to have to write a test suite for, but that I want to do some various kinds of validation upon.
I think standalone ActiveModel should be good for this.
Watch this railscast for more information: http://railscasts.com/episodes/219-active-model
If you like you can check out http://rubygems.org/gems/validates_simple which is a gem for doing simple validation of hashes trying to mimic the interface of the active model validation methods.
I know this is a little old, but our Veto gem is likely what you're looking for: https://github.com/kodio/veto
Standalone validation of plain old ruby object, without dependencies.

Difference between Base, Schema, and Migration in ActiveRecord

I am currently trying to use the activerecord gem in Ruby but I'm quite confused on how to use the gem. What's the difference of ActiveRecord::Base, ActiveRecord::Schema and ActiveRecord::Migration? And when do I use them?
To sum up : AR::Base is the skeleton for a model. Your models usually are classes that derives from this one. AR::Schema is, well, for the schema of your db. You can see what it looks like in the file db/schema.rb. As for AR::Migration, it's in the name too : it's the skeleton for the migration files that you can find in db/migrate. You can check the rails guides & api reference to have more information on them.

Rails 3 generate without persistence layer support

I'm trying to create a Rails app without a persistence layer (ActiveRecord, MongoMapper, etc). I've used --skip-active-record on rails new nice_app. This has changed the config/application.rb, but when I try to create new "model" with Rails generate - rails g model nice_class - it fail like a noob with:
No value provided for required options '--orm'
So, if I skip ActiveRecord I can't use Rails generators ?
Note the tasks that get called when you run rails g model nice_class:
invoke active_record
create db/migrate/20111227183458_create_nice_classes.rb
create app/models/nice_class.rb
invoke rspec
create spec/models/nice_class_spec.rb
The first thing you notice, is that it's using ActiveRecord to generate the model. Besides that, though, it's not doing a lot: creates a file in the migrations folder (which you don't need), another file in the model folder (which you do need), and a file in the test or spec folder (which you 'should' need). You can make these yourself if you want want, they are pretty close to being empty anyway.
For specifics about models not based on ActiveRecord, take a look at Ryan Bates' "Tableless Models" Railscast available here: http://railscasts.com/episodes/193-tableless-model.
If you are going to be making these kinds of non-ActiveRecord models a lot, you can write your own generator that does exactly what you want.
Best of luck.
The models that are generated by the Rails generators are subclasses of ActiveRecord, which only really makes sense in the context of an app with an ORM. You could certainly create models that aren't subclasses of ActiveRecord (and thus not associated with any ORM), but you'd have to do that manually.

How do I make a persistent hash in Ruby?

I would like a persistent hash; an object that act as a hash, but that can persist between program runs.
Ideally, it would only load in memory the value that are accessed.
Since persistent key/value storage is kind of everyones requirement, as it happens there are a large number of solutions.
YAML is probably the easiest way to persist Ruby objects.
JSON works as well but doesn't directly handle symbols.
MySQL and other SQL databases such as sqlite3 also solve this problem, of course. Usually, access is encapsulated within the ActiveRecord ORM library.
The Ruby core has a Marshaling library.
Using sdbm
require 'sdbm'
SDBM.open("/mypath/myfile.dbm") do |myMap|
[...]
myMap[key] = avalue
[...]
myvar = myMap[anotherKey]
[...]
end
create to files : myfile.dbm.dir and myfile.dbm.pag
I would consider using redis-rb, which has a hash datatype. This would not only persist your hash across program runs, but across multiple machines. It's super fast, in memory, and you can have it up and running in < 5 minutes.
in IRB (assuming you've installed and are running redis-server and have installed redis-rb:
require "redis"
redis = Redis.new
The important operations are:
redis.hset(key, field, value)
and
redis.hget(key,field)

Resources