Wednesday, July 23, 2014

Simple NullObject For Ruby

Sometimes you don't need something fancy

I love the Null Object pattern. Especially on the things I've been working on where it just needs to work despite errors in data. When you find these errors it's handy to grab a NullObject that will quack like you need. 

Avdi Grimm wrote a great library, naught. It's great when you have a lot of dependency injection. If you're only using it once or twice in your small library, it's probably overkill.

So here's a simple formula for putting in your own simple nullobjects.

class NullObject
  # You can instantiate a new NullObject with arbitrary methods by calling

  # Interactions.NullObject.new({method1: "outcome1"})

  # That will apply the method to the object

  def initialize(methods={})

    methods.each_pair do |k,v|

      define_singleton_method k do

        v

      end

    end

  end

end

Then all you have to do to call it is:

NullObject.new(method1: "Output")

And the key is the method name and the value is whatever you want it to output.
So something like:

def user
  creator || NullObject.new(name: "Jim", accounts: [])
end

Will let your nullobject do user.accounts and return an empty array. Yay you always get an array instead of a NilClass error.

Friday, July 11, 2014

Homebrew MariaDB (MySQL) and strict_all_tables

I recently updated my MariaDB on OSX using homebrew and suddenly my tests were failing with a mysql error for inserting nulls. After some quick research I found that in a recent update MySQL and MariaDB defaulted sql_mode to strict_all_tables. This prevents nulls from entering where they were allowed before.

Ideally that option is what you would want, but in our case our app has been running for years with NULLs everywhere. It would break our application and was in fact doing so in development.

After some reasearch I found that by putting

[mysqld]
sql_mode=""

would disable it globally but was insufficient because running

SELECT @@SESSION.sql_mode;

would return STRICT_ALL_TABLES. Meaning that the database was still not functioning as before.

There's probably a better way, but how I fixed it was to modify the plist.
~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist
and added:

<string>--sql-mode=</string>

to the program parameters.

Edit: I finally figured out why that session was being set in my rails application by a default.

In your database.yml file you'll want to add strict: false

It just so happened that I had upgraded maria and rails at the same time. Leading me to have a false positive. The plist option only appeared to work. After running tests a few more times the tests were failing again.

Thursday, September 27, 2012

Javascript Helps


Keep in mind that most of these articles are opinionated and you may not agree with every one of them, that's fine. Learn what you can, figure out how to improve your code and if you know of some other resources let's add them to the list.
Javascript Basics:
Javascript Advanced Topics:
Javascript Patterns:
Javascript Tips:
Useful Javascript Tools:

This is taken from my gist.

Thursday, January 5, 2012

Rails Virutalization

Rails virtualization for Windows 7 64-bit 


First off, a caveat, this will work for pretty much any system that can install jruby with a little tweaking.
I was attempting to follow engineyard's virtualization tutorial and ran into a bit of a snag.  It seems that vagrant doesn't play well with 64-bit windows systems (for now).  There is an easy solution however.
Steps
  1. Install java 64-bit and jruby 64 bit. (Note, you may need to add jruby to your path depending on what you did during install)
  2. Install vagrant  
    1. jgem install jruby-openssl jruby-win32ole
    2. jgem install vagrant
  3. Download and install Virtualbox.
  4. Download the enginyard rails setup or make your own. Unzip it to desired directory.
  5. Add the vagrant box using: vagrant box add [name] package.box  (make sure you're in the directory of the box you downloaded/made.
  6. Start vagrant : vagrant up
  7. Login to it: vagrant ssh
  8. You're done!

Some of you may wonder why you would ever go through the hassle.  Honestly, it's pretty easy to just set up a dedicated system for this.  The reason why I ended up using this solution was because I needed a way to do this sort of thing at work where I can't install anything (hence using the jar version of jruby) and at home my wife loves windows over linux.  Not sure what's wrong with Fedora, but for some reason she doesn't like it so we ended up in boot wars.  This mostly fixes the issue.  Not to mention I can share the environments very easily.

[Edit]
Depending on your setup you may encounter an issue where the vm won't start, you'll need to disable usb 2.0 on the machine or install the extras from Oracle.

Friday, December 30, 2011

Rebuilding

I used to have a blog on the hosting service that my wife and I share but due to a mishap it was deleted somehow.  It made me sad because I actually enjoyed posting on that site.  I've decided that I'll use blogger's services since it integrates well with google+ and I already have access through my gmail.  Seems like a good plan.  I've also found great ways to post code, which I plan on doing quite a bit in the near future.

Inheritance Review

Inheritance (Inheritance, #4)Inheritance by Christopher Paolini
My rating: 4 of 5 stars

This book is definitely the weakest in the series. I really enjoyed the story, however, I could definitely tell it was written by someone without a lot of life-experience. At times the characters seemed drawn into some very petty arguments. It's odd to see a king pout like a moody teenager. Overall these were forgivable because Christopher Paolini did a great job with the main characters.

View all my reviews