Archive

Archive for the ‘ruby’ Category

sudo gem install -v=2.3.2 –include-dependencies –no-rdoc –no-ri rails

August 17th, 2010 dwright No comments

maybe, I'll remember this now.
Gem installation takes too long, and I use the online docs

uninstall specific versions of rails
$ sudo gem uninstall -v=2.3.5 rails
Successfully uninstalled rails-2.3.5
$ sudo gem uninstall -v=2.3.8 rails
Successfully uninstalled rails-2.3.8

Categories: ruby, ruby on rails Tags:

Subversion Ruby Bindings Gem Library

August 9th, 2010 dwright No comments

I had a need to access a svn repository programmatically from Ruby and couldn't find a subversion client for ruby in a gem type of reusable library, so I took a shot at writing one. It is just a simplified API (released as a gem) on top of the subversion-ruby bindings. I also started a web app version, which is in progress but has basic functionality at this point.

http://rubygems.org/gems/svn_wc - svn_wc provides programmatic access to basic svn functions, utilizing the Subversion Ruby Bindings, exposing their functionality in a simplified way
source: http://github.com/dvwright/svn_wc

http://rubygems.org/gems/svn_wc_tree - svn_wc_tree provides an Web Application Front End GUI to a working copy of an remote SVN Repository. (rough and in progress)

Maybe this/these will be useful for someone else as well.

Categories: ruby Tags:

Reinstall MySQL on Mac OSX Snow Leopard (keeping past data)

August 6th, 2010 dwright No comments

Reinstall MySQL (Community Server) on Mac OSX Snow Leopard (or How to downgrade to an older version of MySQL)

First, Read (or glance at this, especially if you are also using Rails/Ruby) upgrading-to-snow-leopard recommends using the 64 bit mysql

UPDATE:Do yourself a favor, if on Snow Leopard install the 64 bit (x86_64) MySQL server version! (The *.dmg) (then if you have to build the mysql ruby gem, do so with:
sudo env ARCHFLAGS="-arch x86_64" && sudo gem install -v=2.7 mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include

Overview:
Recently, I upgraded from Leopard to Snow Leopard, for the most part it was quick and easy (about 1 hour, completely unattended)

Afterwards, I did however have to reinstall several apps, notably for me was MySQL.

I have 2 databases which I need for local development, which are fairly large and take a while to populate, so I wanted to maintain this data if possible. Unfortunely, since I did not dump the data before upgrading my OS and now, could not start the mysql server, there was no way to do the 'standard' mysql dump.

In my case, I was previously using a dev snapshot of the db, I wanted to install the same version to minimize issues. If it worked I can always do a proper data dump and upgrade to another version then.

Steps
Here is what I did which worked for me. (you will need to use the Terminal application for this, see 'Utilities')

In my case, I am using, mysql-5.4.3-beta-osx10.5-x86, your version may (probably will) be different, adjust the command to fit your version.

Back up past data
cd /usr/local
sudo tar -zcvf mysql-5.4.3-beta-osx10.5-x86.tar.gz mysql-5.4.3-beta-osx10.5-x86/
sudo mv mysql-5.4.3-beta-osx10.5-x86.tar.gz ~/Documents/

the first command places you in the correct directory
the second makes a tar archive (compressed) copy of the database files.
the third moves that into you home 'Documents' directory (for safe keeping)
(now your data should be safe and sound)

Now Uninstall Mysql: (be careful with these commands, you cant 'undo' them)
(taken from: uninstalling-mysql-on-mac-os-x-leopard pasted here for convenience)
To uninstall MySQL and completely remove it (including all databases) from your Mac do the following:
• sudo rm /usr/local/mysql
• sudo rm -rf /usr/local/mysql*
• sudo rm -rf /Library/StartupItems/MySQLCOM
• sudo rm -rf /Library/PreferencePanes/My*
• edit /etc/hostconfig and remove the line MYSQLCOM=-YES-
• rm -rf ~/Library/PreferencePanes/My*
• sudo rm -rf /Library/Receipts/mysql*
• sudo rm -rf /Library/Receipts/MySQL*

The last two lines are particularly important as otherwise, you can't install an older version of MySQL even though you think that you've completely deleted the newer version
• sudo rm -rf /var/db/receipts/com.mysql.*
(above needed to work for Snow Leopard)

Now Install Mysql:
Now go find a *.dmg of the same version, you were running. (you will probably have to grab it from an archive somewhere, I found mine here: http://archive.sunet.se/pub/databases/relational/mysql/Downloads/MySQL-5.4

Install both the mysql package and startup items package.

they should have installed with no issues.
try starting the server.

to start:
Library/StartupItems/MySQLCOM/MySQLCOM start
to stop:
Library/StartupItems/MySQLCOM/MySQLCOM stop

if that doesn't work (those are text files, you can 'less' them)
sudo /usr/local/mysql/support-files/mysql.server start
sudo /usr/local/mysql/support-files/mysql.server stop

test a login
mysql -uroot (Enter)
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.4.3-beta MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Great, that means it's working, Now, stop the database.

Load past data into New database
Get you old data into this instance.
cd ~/Documents/
sudo tar xzvf mysql-5.4.3-beta-osx10.5-x86.tar.gz

(this will uncompress your past data into this dir)

sudo mv mysql-5.4.3-beta-osx10.5-x86/data/YOURDB /usr/local/mysql-5.4.3-beta-osx10.5-x86/data/
sudo mv mysql-5.4.3-beta-osx10.5-x86/data/YOUR_OTHER_DB /usr/local/mysql-5.4.3-beta-osx10.5-x86/data/

All Done
now start your db, all should be well. (you will have to recreate any other mysql users you had)

Categories: Mac OSX, MySQL, ruby, ruby on rails Tags: ,

Watir, find input elements by (dynamically generated) id

June 24th, 2010 dwright 1 comment

problem space:
web testing, using watir. Need to find the status of a radio button which has a dynamically generated id

html:

<div id="9_status_544_654321">
<!-- On -->
<input type="radio" id="9__status_544_on__654321" value="on"/>
<!-- Off -->
<input type="radio" id="9__status_544_off__654321" value="off"/>
</div>

watir:
(we know the first and last values in the string, but not the middle value,
i.e. 9__status_(?)_off__654321)

   id1, id2 = 9, 654321
    re = Regexp.new("#{id1}__status_\\d+_on__#{id2}")
    p @ie.radio(:id, re).text   # prints 'checked'
    p @ie.radio(:id, /re/).text # also prints 'checked'
Categories: watir Tags:

watir checkbox clear vs checked? vs value

June 18th, 2010 dwright No comments

Geez, what an unpleasant surprise this was.

Problem space:
Use Watir to test if a checkbox is checked or not and to uncheck it (clear).

Confession, I am actually now using Celerity in place of Watir. (So this could behavior may be with Celerity, not watir, I have not confirmed yet)

html:
<input type="checkbox" name="is_moveable" id="is_moveable" checked="yes"/>

celerity:
p @ie.checkbox(:name, 'is_moveable').value # returns 'on'
p @ie.checkbox(:name, 'is_moveable').checked? # returns true
assert @ie.checkbox(:name, 'is_moveable').clear # uncheck it
p @ie.checkbox(:name, 'is_moveable').value # returns 'on'
p @ie.checkbox(:name, 'is_moveable').checked? # returns false

I would think that if you cleared a checkbox with no value, that the value would be 'off', but I 'assume' the default value is 'on', so that is still the value returned.

In this situation use 'checked?' instead of 'value'

rails assert_no_select

April 2nd, 2010 dwright 1 comment

rails 2.2.* something has deprecated assert_tag in favor of the much less verbose assert_select.

In the case you want to verify that an element does not exist, one may expect, 'assert_no_select' which does not exist.

Here is the suggested method for finding the absence of a element:
assert_select ‘div’, :count => 0

Another method which works in many cases, is:
assert_select 'strong', 'Select a Region:' == false
assert_select 'select[id=rid]', true == false

element_by_xpath with two attributesx

March 30th, 2010 dwright No comments

Watir can not access the 'for' attribute of the 'label' element. Here is a work around.

html: <label class="error" generated="true" for="partner_status">This field is required.</label>

xpath: lpe = "//label[@class='error' and @for='partner_status']"
p @ie.element_by_xpath(lpe).text

will print: 'This field is required.'

Categories: ruby, watir Tags: ,

centos 5.4 rpmforge missing subversion-ruby-1.6.6

March 15th, 2010 dwright No comments

we are using subversion.x86_64 1.6.6-0.1.el5.rf and we require the Ruby bindings, subversion-ruby.x86_64.

However only 1.4.2-4.el5_3.1 (from base) is available. (which wont install, as it breaks deps. We can roll back to the base subversion, however there were some notable changes in svn > 1.5.0, (much improved merging, for example) which we do use

rpmforge is missing subversion-ruby.x86_64 1.6.6-0.1.el5.rf.
There is this bug: rpmforge lists

yum list|grep sub
subversion.x86_64 1.6.6-0.1.el5.rf installed
subversion-devel.x86_64 1.6.6-0.1.el5.rf rpmforge
subversion-javahl.x86_64 1.4.2-4.el5_3.1 base
subversion-perl.x86_64 1.6.6-0.1.el5.rf rpmforge
subversion-ruby.x86_64 1.4.2-4.el5_3.1 base

My work around for this:
(I would rather not build this from source)

luckily, I found: http://packages.sw.be/subversion/

wget http://packages.sw.be/subversion/subversion-1.6.6-1.el5.test.x86_64.rpm
http://packages.sw.be/subversion/subversion-ruby-1.6.6-1.el5.test.x86_64.rpm

yum uninstall subversion (luckily no deps)
sudo rpm -ivh subversion-1.6.6-1.el5.test.x86_64.rpm subversion-ruby-1.6.6-1.el5.test.x86_64.rpm

(NOTE: if you are also using something like enterprise ruby in a non standard location, you will have to tell it where to find svn)
something such as:
sudo ln -s /usr/lib64/ruby/site_ruby/1.8/x86_64-linux/svn /opt/ruby-enterprise-curr/lib/ruby/1.8/x86_64-linux/svn

Ruby CGI example

January 29th, 2010 dwright No comments

Searching for examples of ruby/cgi usage didn't turn up much.

I had to rough it and unfortunately, it took longer than it should have to come up with what I wanted to accomplish. (well, more or less what I wanted, well, actually, it's not going to work for what I need but anyway, here's an working ruby/cgi example).

thought I'd share:

require 'cgi'
require 'json'

cgi = CGI.new('html4Tr') # add HTML generation methods

if cgi.params.empty?
  # no request recieved, return html
  cgi.out('charset' => 'UTF-8') do
    cgi.html() do
      cgi.head() do
        "\n" +
        cgi.title{"cgi testing"} +
        CGI.pretty(
          "\n" + %q(<meta http-equiv="cache-control" content="no-cache">
          <meta http-equiv="content-type" content="text/html; charset=UTF-8">
          <meta http-equiv="expires" content="Mon, 22 Jul 2002 11:12:01 GMT">
          <meta http-equiv="pragma" content="no-cache">
          <script type="text/javascript" src="../js/jquery-1.3.2.js">)
        )
      end +
      cgi.body() do
        CGI.pretty(
          "\n" + %q(<input type='hidden' id="ids" value='' />
          <!-- start: show results -->
          <div id="show_hide_container" style="display:none"><a id="hide_n_show">[-]</a></div>
          <div id="action_results"></div>
          <!-- end: show results -->
          <div id="display_results">
            <div class="show_demo" id="unique" style="height:100%"></div>
          </div>)
        ) \
         +
        cgi.form() do
          cgi.textarea("get_text") +
          cgi.br +
          cgi.submit
        end +
        cgi.pre() do
          CGI::escapeHTML(
            "params: " + cgi.params.inspect + "\n" +
            "cookies: " + cgi.cookies.inspect + "\n" +
            ENV.collect() do |key, value|
              key + " --> " + value + "\n"
            end.join("")
          )
        end
      end
    end
  end
else
  # request recieved, return as JSON
  cgi = CGI.new(:encoding => 'UTF-8')
  print cgi.header('type' => 'application/x-javascript', 'charset' => 'UTF-8')
  print cgi.params.to_json
end
Categories: ruby Tags: , ,

Ruby, SQL Query result to iterable data structure

January 22nd, 2010 dwright No comments

This is an example of how to transform a multi-table join result returned by SQL and iterate over it to retrieve the data you are seeking.

Problem Space:

You have a multi-table join result returned by a SQL query which contains the data you need but in a 'flat' format which makes it difficult to extract, maintaing the data relationship.

The Data:

This data structure represents the data returned by a multi-table join.

sql_results = [
 {'t_name' => 'Acct & Benft','h_name' => 'Account',  'type' => 'pu_group'},
 {'t_name' => 'Acct & Benft','h_name' => 'Account',  'type' => 'py_group'},
 {'t_name' => 'Acct & Benft','h_name' => 'Account',  'type' => 'c_group'},
 {'t_name' => 'Acct & Benft','h_name' => 'Account',  'type' => 'pu_line'},
 {'t_name' => 'Acct & Benft','h_name' => 'Account',  'type' => 'py_line'},
 {'t_name' => 'Acct & Benft','h_name' => 'Account',  'type' => 'cl_line'},
 {'t_name' => 'Acct & Benft','h_name' => 'Benft',    'type' => 'b_line'},
 {'t_name' => 'Acct & Benft','h_name' => 'Benft',    'type' => 'b_c_line'},
 {'t_name' => 'Acct & Benft','h_name' => 'Benft',    'type' => 'b_c_add'},
 {'t_name' => 'Acct & Benft','h_name' => 'Benft',    'type' => 'b_group'},
 {'t_name' => 'Catg',        'h_name' => 'Catg Pge', 'type' => 'catg_line'},
 {'t_name' => 'Catg',        'h_name' => 'Catg Pge', 'type' => 'catg_group'},
 {'t_name' => 'List Page',   'h_name' => 'All St',   'type' => 's_line' },
 {'t_name' => 'List Page',   'h_name' => 'All St',   'type' => 's_group'},
 {'t_name' => 'List Page',   'h_name' => 'Cpns & S', 'type' => 'cpn_group'},
 {'t_name' => 'List Page',   'h_name' => 'Cpns & S', 'type' => 'cpn_line_a'},
 {'t_name' => 'List Page',   'h_name' => 'Cpns & S', 'type' => 'cpn_line'},
 {'t_name' => 'TLP',         'h_name' => 'Mrch Rand','type' => 'contact'},
 {'t_name' => 'TLP',         'h_name' => 'Mrch Rand','type' => 'tlp_group'},
 {'t_name' => 'TLP',         'h_name' => 'Mrch Rand','type' => 'tlp_line'}
]

From the data, I need to create a table where:

't_name' is a header with one or more sub-nodes, 'h_name'.
and 'h_name' is a sub-header with one or more sub-nodes, 'type'

For Illustration:

                          'List Page'(t_name)
                               |
               ----------------------------------
               |                                |
           'All St'(h_name)              'Cpns & S'(h_name)
               |                                |
         -------------             --------------------------
         |           |             |            |           |
      's_line', 's_group'     'cpn_group', 'cpn_line_a' 'cpn_line' (type)

Or, simpler

                          'Catg'(t_name)
                             |
                         'Catg Pge'(h_name)
                             |
                        -------------
                        |            |
                   'catg_line', 'catg_group'(type)

It seems clear, to me, that a good data structure for this representation would be
a hash whose key is t_name and whose values are a hash whose key is h_name and
whose value is an array

Example:

{
 "List Page" => {
      "All St"   => ["s_line", "s_group"],
      "Cpns & S" => ["cpn_group", "cpn_line_a", "cpn_line"]
  },
 "Catg"      => {
      "Catg Pge" => ["catg_line", "catg_group"]
  },
 etc,...

}

Here is some code which produces the desired data structure. (I'm sure there are many other approaches)

top_node     = Hash.new
top_sub_node = Hash.new
sql_results.each do |r|

  if top_sub_node[r['h_name']].nil?
    top_sub_node[r['h_name']] = [r['type']]
  else
    top_sub_node[r['h_name']].push r['type']
  end

  if top_node[r['t_name']].nil?
    top_node[ r['t_name'] ] = {r['h_name'] => top_sub_node[r['h_name']]}
  else
    top_node[ r['t_name'] ].merge!({r['h_name'] => top_sub_node[r['h_name']]})
  end
end

require 'pp'
pp top_node
Categories: ruby Tags: , ,