Thursday, February 28, 2013

Rake task for rspec:features

I'm using Capybara, rspec and poltergeist to test features/requests in Ruby on Rails

I wanted a simple raketask to check the features without typing:
$ rake spec SPEC=spec/features/**/*_spec.rb

I wanted to type:

$ rake spec:features

Creating the new raketask is quite simple:

desc "Run the code examples in spec/features"
task "spec:features" do
  Rails.env = ENV['RAILS_ENV'] = 'test'
  ENV['SPEC'] = "spec/features/**/*_spec.rb"
  Rake::Task['spec'].invoke
end
Note that the spec is set as an ENV variable and not passed as an argument to the invoke command.

Thursday, November 29, 2012

Watching/monitoring CSS attributes with jQuery

I needed to monitor dimension and size CSS attributes for a AAC switch-access library I'm developing. (A separate blog post on that when it's ready. It's already on github, but there are some annoying bugs, and only support for single switch elements, and no groups...)

After testing jquery-resize (github.com/cowboy/jquery-resize), jquery-watch (darcyclarke.me/dev/watch/) and a few others, I found that they were almost what I was looking for, but not exactly what I needed.

My requirements where:

  • Monitor one or multiple CSS attribute
  • Use custom functions like 'outerWidth(true)' to get the full width including border, margin and padding. 'css('width')' would only return the element width.
  • Use requestAnimationFrame instead of setTimeout if available.
  • Either jQuery events or direct callbacks
Since I didn't find any existing jQuery plugins, jquery-csswatch was born.

Sourcesgithub.com/leifcr/jquery-csswatch.
Exampleleifcr.github.com/jquery-csswatch/example/

The plugin is written in CoffeeScript.

Thursday, October 25, 2012

Rails 3.2 and virtual attributes

I was fighting with virtual attributes for a few hours, and I'll share what I did wrong:

Here's a model with one virtual attribute as well as a text field

class User < ActiveRecord::Base
  attr_accessible :sanity_level
  attr_accessor   :sanity_level

  validates_presence_of :sanity_level, :on => :update
end

I would expect the following to work:

user = User.create(:name => "Leif")
user.save(:sanity_level => 5)
user.valid?
# => false # What?
user.errors.inspect
# => Errors show: Sanity Level cannot be blank... but it is 5?
user.update_attributes(:sanity_level => 5)
user.valid?
# => true

I expected both to work, but it doesn't by design. After looking at activerecord and mysql2 source, it is easly spotted that save calls create_or_update without forwarding the hash, so of course it will fail!
To summarize and remember for the future:
  1. Use update_attributes when updating with a hash for the attributes to update
  2. Use save after setting attributes manually
  3. Use create when creating new records
  4. Use build when building new records with a hash and saving later
And a note to myself again: Don't use save when updating attributes.

Friday, June 29, 2012

Why I might move away from Qt

To begin with I loved Qt, and I still do for some projects. However, things have changed a lot the last 5 years in the tech industry, and Qt has fallen a bit off the grid after Nokia took charge.

Key points why I am reluctant in using Qt on future projects:

  • Android
  • iOS
  • Windows RT
  • Windows Phone
  • Still struggling to support meego, and symbian. Big waste of resources!

I think that should be quite self-explanatory, but let me dig a bit deeper.

At one point Qt was on the cutting edge of new platforms, then symbian and meego wanted to join in. I knew already than that it meant trouble, because both were already dying platforms. 

I've been using Qt successfully on quite a number of projects over the last 7-8 years, and it has been a pleasure.

Now I don't know if my next few projects will be Qt based at all, even though they will be both mobile and desktop applications.

Qt can support XP/Win7/Win8 Desktop and OS X, but not metro style WinRT (MS ARM based tablets). Qt cannot help me with mobile so I need a different framework here.

So I started looking this way. How many frameworks do I need to write the same software for iOS, Android, OS X, Win XP, Win 7, Win 8 and Win RT ?

I hoped to keep the number down to three, and with Qt, it still might be possible. But there are other frameworks lurking around as well now. 

Example: Mono/.NET:
I can use C# and write common code for all platforms that have Mono or .NET.
Well. All platforms both mobile and desktop has either .NET or Mono. I have to write the GUI for Desktop (OS X, Win XP/7/8) as one, mobile needs one for iOS and one for Android. I would actually have 1 "base" code and 3 derative guis on top. That is managable.

Example: Qt:
I can use C++ to write common objects for both mobile and desktop using STL. The GUI for OS X, Win XP/7/8 can be written in Qt, while the desktop for WinRT have to be written in C++/XAML. Android and iOS need a separate framework.

By choosing Qt, I end up having a C++ base and at least 4 gui implementations in various languages? It's almost doomed to fail. By choosing .NET/Mono I can have 3 gui implementations that actually use parts of each others code, so I save 1 framework/gui implementation. If that is 1 month of work, it's worth using a different framework.

I hope that Qt and the Trolls will open their eyes and make all non-gui parts of Qt compatible with more platforms. It's impossible to have the same GUI code for all the platforms we have today. Even thought it's possible, it will feel "Qt" on top of a native GUI, which isn't really user friendly...

Mobile framework comparisons will be continued

I've been on paternity leave, which is great, and therefore I've tried to keep the blog level down, as the mobile framework is closely related to my work. Paternity leave means stay with your kids, don't press keys on your computer... At least it does for me.

I will do a couple of frameworks next week.

I'm glad I've been a bit delayed, because of the launch of Win8 RT + Microsofts surface tablet changes the gameplan a bit. There aren't any frameworks that support Win8 RT yet, but I expect a few to be able to do so. (At least marmalade and other low level frameworks)

Thursday, March 29, 2012

Choosing a mobile framework: Investigating Moai SDK

This is a follow-up for Choosing mobile dev platform/framework and looks closer at the ninth step -  Investigating each framework. Here we look at Moai SDK. I'll go through each of the steps from the common post for all the frameworks.

Is it easy to get started?

I downloaded the SDK and compared to the previous tested frameworks, it isn't as "tool-centric". Use any editor you like, and use the command line to compile. Even if you don't know anything about the lua language, you should be up and running within 30-60 minutes, depending on your installation/setup skills.

Is it easy to extend the library?

As I originally started with C, assembly and moved onto C++, I would say that it is really easy to extend this library. However, if you don't know C++, you are stuck. There is some documentation on extending here, although it's more a reference than an example. If you are familiar to C++, check the source on github, and you should be able to extend the library without too much hassle. (Just be sure you don't write platform dependant C++ code... use std libs, don't use MS/Apple/Google c++ libs.)


Features I need for my project

Drag and Drop

There is no "native" drag and drop, so it's a matter of writing routines that handle graphics and touch. Moai has good routines for both 2d and 3d. As I need 2d, I can use Box2d in order to handle collisions, which again can be used for drag and drop. Should be ok to implement.

Text to Speech

No support for that, so have to extend the library.

Basic image manipulation

Rotation and scaling, which is the basics. I do need some filters (Gray, inverse, opacity), which might need to be c++ extensions, depending on performance. Lua is usually quite fast, but it depends on the framework, and compiler.

Basic access to camera

When I saw the MOAICamera Class, I thought: cool, native camera access. But that was the camera "viewing" your objects. So this has to be written as an extension.

Drawing/painting

Here is where MOAI is really easy. As long as you know how to program your paiting/drawing, it's just to use the MOAIdraw class.

JSON support

Has built in parser for parsing between json string and a lua table. Read about MoaiJsonParser here.

HTTP(S)-request support

MoaiHttpTaskBase does all the http magic. And if you need a cloud for your data, there is Moai Cloud as well. Looking at the cheap pricing for Moai cloud, I'm starting to consider if that is a better option than running my own service on a VPS.

Custom user interface placement and user interface elements

Nothing as of now, so you have to paint/draw, or use images. It is on the roadmap.

Audio playback (simple)

The audio engine in Moai is Untz open-source library, which seems quite capable from what I found about it. Retronyms is behind it and they do create some great audio applications. Playing a file was as simple as load, setting volume, setting looping, and then play. This is the kind of audio playback I like to see in a framework. Not too fancy, yet really powerful.

Deployment to supported platforms

Both Android and iOS deployment is quite straight forward. You need a mac for iOS, and either mac or PC for Android.


Pricing

CPAL license! That's really impressive. Such a powerful framework is released under CPAL! You can ask them to buy a commercial license if you need to modify the framework and you don't wish to release your changes.


Direct access to APIs

No. You need to write extensions. Wrap them as small as possible, and you will soon have access to what you need.


Programming Language

Lua and/or C++. C++ isn't recommended as the main language, as it does require some changes to the framework. Most likely both C++ and Lua is needed when you hit the wall on what the framework can do.


Documentation

They are working on the docs/wiki, and that is somewhat needed. Although the samples in the SDK should be enough for most of your questions. The API documentation is OK, but it still needs a bit of work.


Supported platforms

  • iOS (3.0 and above)
  • Android (2.1 and above)
  • Chrome (The browser)


Summary

Moai is a mature, and going forward, and has a proper Roadmap (Not all frameworks do). I think Moai has the potential to be one of the leading cross-platform frameworks. It really depends on where they go from now. I hope they consider native Windows/OS X as platforms as well, as having one codebase for both mobile and desktop is ideal. Although it's fully possible to use chrome on both platforms, it's not really the same as having native apps on desktop.
For Mobile, I think Moai is a winner, both in pricing, features and future roadmap. It's quite powerful and feature-rich, and it's likely to become even more powerful and feature-rich.

Wednesday, March 21, 2012

Version numbers on software with git, tags and sed

After I moved from svn to git, I started doing some version numbers manually, as I felt I never had the time to figure out a good way to set version numbers using git. However, it's much easier and simpler than in svn, when done right.

Usually I use version numbers like this: 1.0.290, where 1 is major, 0 is minor and 290 the revision number.

I wanted to keep the same pattern when using git, as a lot of the software I maintain is moved from svn, where the revision was used directly from the svn repository.

Instead of having automatic revision numbers, I'm only going to increase them when I want to. That makes it possible to commit without increasing the revision number, which is really want I wanted to do.

Here's a quick and simple way to do it:
Tag your repository v1.0.290 (or similar)

To get the major, minor and revision number, git hash and number of commits since that tag:

echo "major:"
git describe --tags --long | sed "s/v\([0-9]*\).*/\1/"

echo "minor:"
git describe --tags --long | sed "s/v[0-9]*\.\([0-9]*\).*/\1/"

echo "revision:"
git describe --tags --long | sed "s/v[0-9]*\.[0-9]*\.\([0-9]*\).*/\1/"

echo "commits since tag:"
git describe --tags --long | sed "s/v[0-9]*\.[0-9]*\.[0-9]*-\([0-9]*\).*/\1/"

echo "git_hash:"
git describe --tags --long | sed "s/v[0-9]*\.[0-9]*\.[0-9]*-[0-9]*-g\(.*\)/\1/"

It should give an idea on how to get the data needed to update a project with the corresponding version info.