September 2009
Bit of weirdness with ActiveRecord
Submitted by mmorsi on Wed, 2009-09-30 20:26Figure I'd pass on a bit of weirdness concerning Ruby's ActiveRecord. Apparently when two models class are associated via rails the association takes place through a custom ActiveRecord association class, eg if a host model has many instances of the nic model, host.nics will be an instance of ActiveRecord::Associations::AssociationProxy and _not_ a mere array of nics. AssociationProxy acts very similarily to an array, providing the expected functionality to get/set associated classes.
All of this is as expected, the weirdness arising from the fact a call to host.nics.class would return the 'Array' class and not 'AssociationProxy'. This is because AssociationProxy hijacks many methods and uses them for its own purposes, including the 'class' method.
To access an array from an instance of AssociationProxy, merely call the 'all' method, eg
host.nics.all.find { |nic| some_test }
Trying to call the 'find' method on nics directly will result in an ActiveRecord error: "Couldn't find Nic without an ID" as the AssociationProxy find method is called instead of the one you want.
- mmorsi's blog
- Login to post comments
- Read more
A tip every web developer knew but me
Submitted by mmorsi on Mon, 2009-09-28 18:16Funny how the simplest things will slip by you (or at least you'll forget and have to remind yourself from time to time).
Lets say you have two divs, one contained within another, where the child has some floating style and the parent does not. Depending on the amount of content in the parent and child divs, you may see it spill out of the parent like so:
goes in the child div
to illustrate the problem
Notice how the bottom border goes through the text. The markup to generate this is:
<div style="border-bottom: 1px solid black" id="css_example_parent">
<div style="float: left;" id="css_example_child">
Alot of content<br/>
goes in the child div<br/>
to illustrate the problem<br/>
</div>
</div>To fix this simply add a "<div style="clear:both;"></div>" as the very last element of the container div like so:
<div style="border-bottom: 1px solid black" id="css_example_parent">
<div style="float: left;" id="css_example_child">
Alot of content<br/>
goes in the child div<br/>
to illustrate the problem<br/>
</div>
<div style="clear:both;"></div>
</div>Resulting in:
goes in the child div
to illustrate the problem
- mmorsi's blog
- Login to post comments
- Read more
Getting Started w/ Apache QMF
Submitted by mmorsi on Mon, 2009-09-21 17:25The Qpid Management Framework is a powerful open source remoting framework which developers can use to query and invoke methods on managed objects residing on a remote host. As with most other things its fairly straightforward to use when you know it, but it is currently still relatively early in development, and thus there isn't a whole lot of great documentation out there.
To start of, you should read this document thoroughly to familiarize yourself with all the necessary terms. In a gist, a developer will write an 'agent' who is responsible for dispatching requests to managed objects locally, returning results as neccessary. The objects provided and associated properties/methods are detailed via a 'schema'. The agent will register the object classes that it is managing with a 'broker' who is responsible for establishing and maintaining the communication channels, locating the correct agent when a client, known as a 'console', makes a request for a certain object class. With Apache QMF, the Apache QPID daemon also provides QMF broker functionality.
I've written and attached a sample QMF agent/console I've written for anyone who'se looking to start off. It is based of the more complete/robust ovirt-agent (though simplified greatly since its a new developer tutorial), and confusingly enough the agent was written using the Ruby QMF module while the console was written using Ruby QPID::QMF (see my findings on the difference here, I went this way since the example I'm basing this off of, ovirt-agent, does as well).
######################################## Agent #!/usr/bin/ruby # # Simple QMF Agent Example require 'qmf' # class which will do the work # we need for our application class Widget public attr_accessor :socket def initialize @socket = "foo" end def do_something(data) puts "doing something with " + data.to_s end end # setup the Widget Qmf schema # -or- use something like # http://git.et.redhat.com/?p=ovirt-server.git;a=blob;f=src/ovirt-agent/lib/ovirt/schema_parser.rb $widget_schema = Qmf::SchemaObjectClass.new("org.morsi.test", "Widget") $widget_schema.add_property(Qmf::SchemaProperty.new("socket", Qmf::TYPE_SSTR)) $do_something_schema = Qmf::SchemaMethod.new("do_something") $do_something_schema.add_argument(Qmf::SchemaArgument.new("data", Qmf::TYPE_SSTR, {:dir => Qmf::DIR_IN})) $widget_schema.add_method($do_something_schema) # will handle incoming requests for objects and method invocations class WidgetAgent < Qmf::AgentHandler # implementation of Qmf::AgentHandler.get_query callback # called when a client requests an object def get_query(context, query, user_id) puts "Query: context=#{context} class=#{query.class_name} object_id=#{query.object_id} user_id=#{user_id}" # !!! you should actually handle get_query here, by using the specified # class_name and query attributes to lookup and return matching objects widget = Widget.new obj = Qmf::AgentObject.new($widget_schema) obj[:socket] = widget.socket obj.set_object_id(@agent.alloc_object_id) # get_query must perform these steps to return the response @agent.query_response(context, obj) @agent.query_complete(context) end # implementation of Qmf::AgentHandler.method_call callback # called when a client invokes a method on an object def method_call(context, name, object_id, args, user_id) puts "Method: context=#{context} method=#{name} object_id=#{object_id}, args=#{args} user_id=#{user_id}" # !!! you should actually handle method_call here, by using the speicifed # class_name, object_id, and method name / args to invoke the correct method # on the correct object Widget.new.do_something(args["data"]) @agent.method_response(context, 0, "OK", args) end def initialize # connect to specified broker & register self as agent handler @settings = Qmf::ConnectionSettings.new @settings.host = "localhost" #@settings.port = port @connection = Qmf::Connection.new(@settings) @agent = Qmf::Agent.new(self) # register classes that we provide @agent.register_class($widget_schema) end def mainloop Thread.abort_on_exception = true @agent.set_connection(@connection) sleep end end widget_agent = WidgetAgent.new widget_agent.mainloop ######################################## Console #!/usr/bin/ruby # # Simple QMF Console Example require 'qpid' @session = Qpid::Qmf::Session.new @session.add_broker @session.objects(:class => "queue", :package => "org.apache.qpid.broker").each { |q| puts "Queue " + q.to_s } widgets = @session.objects(:class => "Widget", :package => "org.morsi.test") widgets.each { |w| puts "Widget " + w.to_s + " " + w.socket.to_s for (key, val) in w.properties puts " property: #{key}, #{val}" end result = w.do_something('4.20') } ########################################
Check this blog again (or subscribe to the feed) for more about qmf in the future. Enjoy!
- mmorsi's blog
- Login to post comments
- Read more
Warning: Ruby's Qpid::Qmf Module != Qmf Module
Submitted by mmorsi on Sat, 2009-09-19 02:17Something to look out for for anyone interested in using Apache QMF.
The Ruby Qpid::Qmf module (as provided by the ruby-qpid package in fedora) is a qmf implementation written purely in ruby (history/source can be found here).
The Ruby Qmf module (as provided by the ruby-qmf package in fedora) is the ruby binding to the C++ QMF library. It is autogenerated along w/ the ruby bindings to the qpid library (qmf / qpid are seperate libraries now)
It is my understanding based on how these things work, that the Ruby QMF module is more complete and robust, containing the full implementation of the C++ QMF library, adapted to ruby. The QPID::QMF module is a more convenient utility, with nicer methods / classes to use, but a bit more limited in functionality. Which module the developer uses depends on their needs.
As I explore QMF so more I'll make sure to report back with my finding on the differences. I'm already hosting the QPID::QMF API docs, and have just updloaded the QMF API docs for anyone thats interested (at least until they are officially hosted on qpid.apache.org).
Enjoy!
Disabled anon comments
Submitted by mmorsi on Tue, 2009-09-15 02:21So what I had feared had transpired, as I had just realized every post on this site was littered w/ spam comments from anonymous sources. Supposedly there is a drupal module which will filter the spam for you, but honestly I haven't much need for anonymous comments right now anyways, so I disabled it all together. If you notice anything wrong w/ any of my postings, just email me, and if I incorporate your feedback I promise to cite you as the source.
BTW to save anyone in the same situation the hassle; in order to delete all comments from your drupal database you need to not only trucate the 'comments' table but also the 'node_comments_statistics' table, else the comments will be gone but you'll still see 'xyz comments' under each post on your front page.
- mmorsi's blog
- Login to post comments
- Read more
A few more Haikus
Submitted by mmorsi on Wed, 2009-09-09 02:42Came up with these about random topics over some time and figure I'd share. I'm still no expert and am learning (there seem to be a few sites on the net where people can share them).
Too much caution
can likely be a danger
act responsibly
Freedom is a whole
Cannot be picked and chosen
Remove one, take all
We need too leave Earth,
our ultimate ascension,
all eggs one basket
Among other things, Haikus are neat mental exercises you can do pertaining to any topic you may have at mind. Traditionally they are about nature but as with everything there are many variations of the art.
- mmorsi's blog
- Login to post comments
- Read more
Announcing Motel Version 0.1
Submitted by mmorsi on Wed, 2009-09-09 15:30Movable Object Tracking Encompassing Locations is a library/utility written in ruby used to track the locations / coordinates of objects relative to each other in a 3D environment, moving them according to configurable movement strategies.
I actually was able to quickly bang this out, having started it only three weeks ago. Granted, I'd originally written in this in C++ over the last year and a half, my work on the romic and manic projects ultimately culminating in the C++ version of this project. So most of the design work was done, I was just dissapointed at the pace I was going towards even larger goals, and thus decided to switch languages to Ruby (using activerecord for my db stuff) and use Apache QPID to speed things up a bit.
So now I present Motel, an AGPL utility written in Ruby (but also provides an AMQP interface which can be used by a client written in any language) whose sole purpose is to provide a robust way which to track moving locations, configuring them and resolving queries when appropriate. Its still a work in progress, there are quite a few TODOs, and the documentation could use much love, and a user / developer manual written. In lieu of this in the meantime, I'm going to be looking at extracting certain choice snippets from my code, and pasting them to this blog to discuss my design and implementation methodology. Enjoy!
( and btw happy 09-09-09 :-D )
- mmorsi's blog
- Login to post comments
- Read more
Back in the 'cuse
Submitted by mmorsi on Tue, 2009-09-01 02:31So for anyone that didn't know (which is pretty much anyone I didn't personally tell) I was living in the Boston area this past summer, and working out of an office RH has near there. Adding it to the list, this brings the cities that I've lived in or at least seen a substantial part of when visiting to Syracuse, Cairo Egypt, Raleigh NC, NYC, San Franciso CA, Boston MA.
I've also been to Washington DC, Montreal, Toronto, San Jose, the Amsterdam Airport :-) and a slew of other places, but I didn't stay / visit for long and don't have much of a recollection.
They say home is where the heart is. Say what you want about Syracuse (and believe me there are a million crappy things about living here........ though the same number if not more good things) it's still my favorite place to live :-)
- mmorsi's blog
- Login to post comments
- Read more





