establishing an sqlite3 db connection via ActiveRecord

ActiveRecord can be used as a stand-alone gem without going through a Rails app. When establishing a connection we must provide a name for the database. Here is an example where I use sqlite3 as a db engine. I want to provide a full path to the database also.

my_db_dir = ‘~/projects/myapp/db’
filename = File.join(my_db_dir,’development.sqlite3′)
ActiveRecord::Base.establish_connection(:adapter => ‘sqlite3’, :database => filename)

On Ubuntu systems it is very likely that we will get an error

No such file or directory

I know the file is there. I know the path is correct. So what is wrong? The problem is the “~” character. ActiveRecord uses Ruby’s File.binread() method to read the file. The binread() method is a wrapper for a function in the c code (io.c) of the Ruby interpreter. When you dig deep into the source code of the Ruby project you will find the file.c file which contains a function for checking file paths for security related issues. This function is called rb_get_path_check().

Basically we should not pass a tilde character to File.binread(). It is something that will confuse you for many hours if you it!!

Here is a demonstration of the problem using interactive Ruby (irb).

$ irb
ruby-1.9.2-rc2 > File.binread("~/myfile.txt")
errno::ENOENT: No such file or directory – ~/myfile.txt
from (irb):1:in `binread’
from (irb):1
from /home/philip/.rvm/rubies/ruby-1.9.2-rc2/bin/irb:17:in `<main>’
ruby-1.9.2-rc2 > File.binread("/home/myuser/myfile.txt")
=> "This is the content of myfile… "

You can see that the “~” character did not work. 🙁

Thor CLI KJV Bible Search Engine – part 3

In part 1 I created a simple CLI (command line interface) Bible Search Engine. I then built a Thor version. It’s cool. It works. But I can’t lookup verses very easily. So, I want to remedy this situation. I just add a new Thor method to my bbl Thor task:

desc "ref [bookname,verseref]", "display a particular verse"
#method_options :sort => :boolean
def ref(bookname="genesis", verseref="001:001")
inside(Bbl.source_root) {
output = search_for_phrase
shell.say pretty(output, verseref, false)

Now I get the following:

$ thor bbl:ref john 003:013

john 003:013 And no man hath ascended up to heaven, but he that came down from heaven, even the Son of man which is in heaven.
john_1 003:013 Marvel not, my brethren, if the world hate you.
Now, it occurred to me that we could play around with some numerals. Before proceeding, I must say I’m not one of those “Bible code” guys. I am, however, a big fan of Don Knuth, author of Things a Computer Scientist Rarely Talks About and 3:16. In 3:16, Don surveys the sixteenth verse of the third chapter of every book in the Bible with a third chapter. Who said the Bible was boring anyway?

So, with our Thor tool it turns out we can pull the long list of 3:16 verses very easily:
[code]$ thor bbl:ref . 003:016[/code]

acts 003:016 And his name through faith in his name hath made this man strong, whom ye see and know: yea, the faith which is by him hath given him this perfect soundness in the presence of you all.
chronicles_1 003:016 And the sons of Jehoiakim: Jeconiah his son, Zedekiah his son.
chronicles_2 003:016 And he made chains, as in the oracle, and put them on the heads of the pillars; and made an hundred pomegranates, and put them on the chains.
colossians 003:016 Let the word of Christ dwell in you richly in all wisdom; teaching and admonishing one another in psalms and hymns and spiritual songs, singing with grace in your hearts to the Lord.
corinthians_1 003:016 Know ye not that ye are the temple of God, and that the Spirit of God dwelleth in you?
corinthians_2 003:016 Nevertheless when it shall turn to the Lord, the vail shall be taken away.
daniel 003:016 Shadrach, Meshach, and Abednego, answered and said to the king, O Nebuchadnezzar, we are not careful to answer thee in this matter.
deuteronomy 003:016 And unto the Reubenites and unto the Gadites I gave from Gilead even unto the river Arnon half the valley, and the border even unto the river Jabbok, which is the border of the children of Ammon;
ecclesiastes 003:016 And moreover I saw under the sun the place of judgment, that wickedness was there; and the place of righteousness, that iniquity was there.
ephesians 003:016 That he would grant you, according to the riches of his glory, to be strengthened with might by his Spirit in the inner man;
exodus 003:016 Go, and gather the elders of Israel together, and say unto them, The LORD God of your fathers, the God of Abraham, of Isaac, and of Jacob, appeared unto me, saying, I have surely visited you, and seen that which is done to you in Egypt:
ezekiel 003:016 And it came to pass at the end of seven days, that the word of the LORD came unto me, saying,
galations 003:016 Now to Abraham and his seed were the promises made. He saith not, And to seeds, as of many; but as of one, And to thy seed, which is Christ.
genesis 003:016 Unto the woman he said, I will greatly multiply thy sorrow and thy conception; in sorrow thou shalt bring forth children; and thy desire shall be to thy husband, and he shall rule over thee.
habakkuk 003:016 When I heard, my belly trembled; my lips quivered at the voice: rottenness entered into my bones, and I trembled in myself, that I might rest in the day of trouble: when he cometh up unto the people, he will invade them with his troops.
hebrews 003:016 For some, when they had heard, did provoke: howbeit not all that came out of Egypt by Moses.

Crazy. In the next installment I’ll figure out a way to filter by groups of books such as Torah, NT, OT, Pauline, etc.

Thor CLI KJV Bible Search Engine – part 2

In part 1 I showed how I created a simple CLI (command line interface) Bible Search Engine. The problem is that one must always reference the path to the Bible text files. So I will create a Thor task which will eliminate this dependence on the user giving a path (or having to change to the text file directory). First, I create a directory called bbl (which means Bible) inside my _thor directory (which is just a collection of task directories) and then copy the source files there. I’ll create a bbl.rb file which holds my Ruby code creating a Thor task.
_thor/bbl$ ls
bbl.rb books

_thor/bbl$ more bbl.rb
class Bbl < Thor

def self.source_root
# the permanent location of the text files on my system

Now we can install the Thor task so the system always knows about it:
_thor/bbl$ thor install bbl.rb
Please specify a name for bbl.rb in the system repository [bbl.rb]: bbl
Storing thor file in your system repository

_thor/bbl$ thor installed
Modules Namespaces
——- ———-
bbl bbl


thor bbl:find [PHRASE] # find a phase given as list of words on param line

Now we can search through the Bible from anywhere on the system. Here is an example from the home directory:
author@his-laptop:~$ cd ~
author@his-laptop:~$ thor bbl:find "thou art a priest"
psalms 110:004 The LORD hath sworn, and will not repent, Thou art a priest for ever after the order of Melchizedek.
hebrews 005:006 As he saith also in another place, Thou art a priest for ever after the order of Melchisedec.
hebrews 007:017 For he testifieth, Thou art a priest for ever after the order of Melchisedec.
hebrews 007:021 (For those priests were made without an oath; but this with an oath by him that said unto him, The Lord sware and will not repent, Thou art a priest for ever after the order of Melchisedec:)

Another advantage of running output through the Thor library is that we can perform post-processing to clean up the output (notice the use of Thor’s shell.set_color method):

We get a cleaner presentation:

_thor/bbl$ thor bbl:find "Thou art a priest"

psalms 110:004 The LORD hath sworn, and will not repent, << Thou art a priest >> for ever after the order of Melchizedek.
hebrews 005:006 As he saith also in another place, << Thou art a priest >> for ever after the order of Melchisedec.
hebrews 007:017 For he testifieth, << Thou art a priest >> for ever after the order of Melchisedec.
hebrews 007:021 (For those priests were made without an oath; but this with an oath by him that said unto him, The Lord sware and will not repent, << Thou art a priest >> for ever after the order of Melchisedec:)

Thor – an intro

Thor is a simple and efficient tool for building self-documenting command line utilities. It removes the pain of parsing command line options, writing “USAGE:” banners, and can also be used as an alternative to the Rake build tool. The syntax is Rake-like, so it should be familiar to most Rake users.

I have built a corporate security model. When I say “security model” I am talking in a generic fashion. So what is it? Well it is ER-model for a relational database which allows me to record people objects, their web user objects, the organization objects that they belong to, the role objects for web app usage rights, and many other objects related to general web application use by humans.

When implementing such a model in an MVC architecture like RubyOnRails there will be many files located in various directories. This is not a big problem at all until you decide that you want to use this security model’s functionality in other RubyOnRails web applications. The obvious solution is to build some kind of plugin. Indeed, frameworks such as RubyOnRails do provide some sort of support for building plugins that can be reused.

In the Ruby development world we have the excellent Rake build and automation tool. However, this article will explore Thor which can be considered a kind of “next generation” Rake tool.

I’ll keep this post short and explore more details at a later time. For now let’s take a look at my Thor “configuration.”

…dev/_thor/testarea$ thor installed
Modules Namespaces
——- ———-
authena authena
fbptools fbp_tools

thor authena:install NAMEs files #
thor authena:removes installed files #

thor fbp_tools:install NAME # installs files
thor fbp_tools:uninstall NAME # removes installed files


We see that there are two Thor packages installed in my environment. I built them both. Authena is the name of the Thor task package which installs and uninstalls the security model files. fbp_tools is the package which installs some utility files which I use in many Rails apps.

Both packages install and remove files into various directories such as the “models”, “views”, and “controllers” directories.

Let me finish with a teaser showing you what happens when we install the Authena package. Next time, I’ll dig into the code a bit deeper.

…testarea$ thor authena:install
create app/models/accessibility.rb
create app/models/address.rb
create app/models/contact.rb
create app/models/organization_binding.rb
create app/models/organization.rb
create app/models/person.rb
create app/models/role.rb
create app/models/software_application.rb
create app/models/user.rb
create app/views/accessibilities/_access.html.haml
create app/views/accessibilities/change_user_role_state.js.rjs
create app/views/accessibilities/invalid_state_change.js.rjs
create app/views/layouts/admin.html.haml
create app/views/layouts/organizations.html.haml
create app/views/layouts/user_sessions.html.haml
create app/views/layouts/users.html.haml
create app/views/organizations/edit.html.haml
create app/views/organizations/_form.html.haml
create app/views/organizations/index.html.haml
create app/views/organizations/new.html.haml
create app/views/organizations/show.html.haml
create app/views/people/cancel_access_edit_area.js.rjs
create app/views/people/edit.html.haml
create app/views/people/new.html.haml
create app/views/people/_roles_by_user.html.haml
create app/views/people/show.html.haml
create app/views/people/destroy.html.haml
create app/views/people/index.html.haml
create app/views/people/_people_list.html.haml
create app/views/people/show_access_edit_area.js.rjs
create app/views/roles/_action_menu.html.haml
create app/views/roles/index.html.haml
create app/views/roles/show.html.haml
create app/views/users/_accessibilities_for_users.html.haml
create app/views/users/change_user_teacher.js.rjs
create app/views/users/_form.html.haml
create app/views/users/show_assign_to_teacher_area.js.rjs
create app/views/users/cancel_assign_student_to_teacher_area.js.rjs
create app/views/users/edit.html.haml
create app/views/users/new.html.haml
create app/views/users/show.html.haml
create app/controllers/accessibilities_controller.rb
create app/controllers/admin_controller.rb
create app/controllers/managers_controller.rb
create app/controllers/organizations_controller.rb
create app/controllers/people_controller.rb
create app/controllers/roles_controller.rb
create app/controllers/users_controller.rb
create app/helpers/accessibilities_helper.rb
create app/helpers/admin_helper.rb
create app/helpers/managers_helper.rb
create app/helpers/organizations_helper.rb
create app/helpers/people_helper.rb
create app/helpers/roles_helper.rb
create app/helpers/user_sessions_helper.rb
create app/helpers/users_helper.rb
create db/schema.rb

As you can image it would be very time consuming to copy these files manually every time you want to add this module to your MVC application!!!

Removing this security model’s files is quite easy as well:

…/testarea$ thor authena:uninstall
remove app/models/accessibility.rb
remove app/models/address.rb
remove app/models/contact.rb
remove app/models/organization_binding.rb
remove app/models/organization.rb
remove app/models/person.rb
remove app/models/role.rb
remove app/models/software_application.rb
remove app/models/user.rb
remove app/views/accessibilities/_access.html.haml
remove app/views/accessibilities/change_user_role_state.js.rjs
remove app/views/accessibilities/invalid_state_change.js.rjs
remove app/views/layouts/admin.html.haml
remove app/views/layouts/organizations.html.haml
remove app/views/layouts/user_sessions.html.haml
remove app/views/layouts/users.html.haml
remove app/views/organizations/edit.html.haml
remove app/views/organizations/_form.html.haml
remove app/views/organizations/index.html.haml
remove app/views/organizations/new.html.haml
remove app/views/organizations/show.html.haml
remove app/views/people/cancel_access_edit_area.js.rjs
remove app/views/people/edit.html.haml
remove app/views/people/new.html.haml
remove app/views/people/_roles_by_user.html.haml
remove app/views/people/show.html.haml
remove app/views/people/destroy.html.haml
remove app/views/people/index.html.haml
remove app/views/people/_people_list.html.haml
remove app/views/people/show_access_edit_area.js.rjs
remove app/views/roles/_action_menu.html.haml
remove app/views/roles/index.html.haml
remove app/views/roles/show.html.haml
remove app/views/users/_accessibilities_for_users.html.haml
remove app/views/users/change_user_teacher.js.rjs
remove app/views/users/_form.html.haml
remove app/views/users/show_assign_to_teacher_area.js.rjs
remove app/views/users/cancel_assign_student_to_teacher_area.js.rjs
remove app/views/users/edit.html.haml
remove app/views/users/new.html.haml
remove app/views/users/show.html.haml
remove app/controllers/accessibilities_controller.rb
remove app/controllers/admin_controller.rb
remove app/controllers/managers_controller.rb
remove app/controllers/organizations_controller.rb
remove app/controllers/people_controller.rb
remove app/controllers/roles_controller.rb
remove app/controllers/users_controller.rb
remove app/helpers/accessibilities_helper.rb
remove app/helpers/admin_helper.rb
remove app/helpers/managers_helper.rb
remove app/helpers/organizations_helper.rb
remove app/helpers/people_helper.rb
remove app/helpers/roles_helper.rb
remove app/helpers/user_sessions_helper.rb
remove app/helpers/users_helper.rb
remove db/schema.rb