4891..0.0.1398096616821|534890.0e84f0f3-f5d2-4280-8de2-73fe05fe82d7.0.0.1397601083761|534889.h7njbddqoyypn.0.0.1397601081190|535495.1a26d5c6-8059-11e3-b5d4-00259036d06e.0.0.1397601081190|557581..0.0.1398096616821|534300..0.0.1398096616821|534301.d979f9d7-d158-45f5-8953-b75c225304d7.0.0.1397601083203|554115..0.0.1398096616821|554114..0.0.1398096616821|556010.3980b902-0b1d-4185-87b2-cd79ac14ac89.0.0.1397601084636|534297..0.0.1398096616821|535854..0.0.1398096616821|536088..0.0.1398096616821|534299..0.0.1398096616821|557588..0.0.1398096616821|538303..0.0.1398096616821|557587..0.0.1398096616821|535039.2e937c4c-4be1-48de-8479-3311ac68aeed.0.0.1397601082067|551764.534db437ed76453d85927fa5.0.0.1397601084468|537203..0.0.1398096616821|551767..0.0.1398096616821|550787..0.0.1398096616821|550788..0.0.1398096616821|537583.8b967604-3b09-4103-8a6d-e084191f902e.0.0.1397601086319|557477..0.0.1398096616821|550789..0.0.1398096616821|537204..0.0.1398096616821|535461.3401017810222005929.0.0.1398257031599|531399..0.0.1398096616821|530912.AQELvpSW8PCsaQIBAQErAQEBAQE.0.0.1397601082705|552562..0.0.1398096616821|537983..0.0.1398096616821|532623..0.0.1398096616821|539152.9832d6b4-a56f-11e3-a775-0026b937c890.0.0.1397601084046|547193..0.0.1398096616821|538569..0.0.1398096616821|551586..0.0.1398096616821|536993..0.0.1398096616821|551587..0.0.1398096616821|545979.7683914999798775275.0.0.1397601081880|531292.NC-00000008399250446.0.0.1397601082592|543604.0385ab8a-1eca-46e9-a18c-dafe95b90c96.0.0.1397601082883|557504..0.0.1398096616821|543793.35ca7280-312e-4c50-aabb-15308cf7ba96.0.0.1397601084288|543073..0.0.1398096616821|540281..0.0.1398096616821|541254..0.0.1398096616821|533406..0.0.1398096616821|551446..0.0.1398096616821|538064..0.0.1398096616821|543921.e97a5d9fbf7a161c-064b42a9a7b0f822.0.0.1397601082807|557413..0.0.1398096616821|538467..0.0.1398096616821|539573..0.0.1398096616821|557660..0.0.1398096616821|554196.XU01v04-8c3cfeb1-83bf-4334-a380-0f3614f1058b.0.0.1397601082319; Domain=.contextweb.com; Expires=Thu, 23-Apr-2015 12:43:51 GMT; Path=/ CW-Server: lga-app603 Cache-Control: private, max-age=0, no-cache, no-store Expires: -1 Content-Type: image/gif;charset=ISO-8859-1 Content-Language: pt-BR Transfer-Encoding: chunked Date: Wed, 23 Apr 2014 12:43:50 GMT 31 GIF89a!,T; 0 itterFastly-Debug-Path: (F cache-jfk1028-JFK 1398257031) GitHub Coderwall LinkedIn RSS

Writing My First RubyGem - CanBe

I started working on a personal project in Ruby on Rails, that probably will not see the light of day. I had an Active Record model that could have different attributes depending on the type of record that it was. In Rails, there are different ways to create your models to handle data in this manner. For example, you could have a different model for each type of record. However, this creates some challenges when querying the data. It would be difficult to query data to return a result set that would include different types of models.

You could also use ActiveRecord Single table inheritence to implement the functionality that met my requirements. That’s when my data modeling and DBA experience kicked in. Using STI would result in database rows with many null values in them. This didn’t feel right to me.

That’s why I wrote the CanBe gem. With the CanBe gem, you can declaratively define the possible types for any given instance of your model. Initially, it just provided many helper methods to ensure consistency. Then I enhanced it to allow for different attributes depending on type of model instance that you are accessing. To accomplish this, I used polymorphic associations to connect up different models that represent the specific attributes required for the instance type.

As of version 0.2.1 of the CanBe gem, I have implemented most of the functionality that I wanted to include. There are still a few more tweaks that I would like to make. I am hoping that other developers will find the functionality useful. If you want to see functionality, that is not currently included, feel free to contact me or submit a pull request. Contributions are always welcome and appreciated.

Writing my first gem has been a great experience! It furthered my knowledge of the Ruby language and how to integrate with Ruby on Rails, specifically ActiveRecord. I was able to apply TDD techniques to ensure the gem didn’t break as I added new functionality. I worked with Travis to ensure that my code was integrated properly when changes were made.

I hope that others find the CanBe gem useful for their projects!

Comments

Comments