June 28, 2008 by Dean
It is a pretty well known fact that using javascript to add decimals 0.1 with 0.2 does not result in 0.3. [1] Try it yourself with the FireBug console. For the uninitiated, the problem stems from javascript’s internal representation of numbers. They are actually binary numbers that are usually exact, but sometimes for example, are 0.00000000000000004 off. This is particularly aggravating when writing calculators that rely on js to give accurate results.
In my text inputs I was using toFixed() and some magic HTML attributes to keep decimals nice and clean. However, this method breaks down when a user enters a number with more significant figures than initially set up or you try to operate on two numbers with different sig figs. It was probably inevitable that I use a little arithmetic library extending Number to make decimals play nice.
Since javascript is 13 years old I thought it would be a simple thing to find such a library. I was wrong. After four days learning, looking and lamenting I had no library. After putting this one together in about a day and a half I am not surprised that nobody published theirs. Most of the eleven functions are one-liners, yet it bothers me that there are probably thirty-odd implementations of the this out there and not one found through Google.
-
// decimal_arithmetic.js
-
String.prototype.digitsAfterDecimal = function()
-
{ var parts = this.split(".", 2); // FIXME: Not international!
-
if( ! parts[1] )
-
{ parts[1] = ""; }
-
return parts[1].length;
-
};
-
-
Number.prototype.biggerScalar = function(n)
-
{ return n.scale() > this.scale() ? n.scale() : this.scale(); };
-
-
Number.prototype.digitsAfterDecimal = function()
-
{ return this.toString().digitsAfterDecimal(); };
-
-
Number.prototype.divided = function(n)
-
{ return this.dividedBy(n); };
-
-
Number.prototype.dividedBy = function(n)
-
{ return this.multiply( n.reciprocal() ); };
-
-
Number.prototype.minus = function(n)
-
{ return this.plus( n.negative() ); };
-
-
Number.prototype.multiply = function(n)
-
{ var s = this.biggerScalar(n);
-
return (Math.round(s*this,0) * Math.round(s*n,0)) / (s*s);
-
};
-
-
Number.prototype.negative = function()
-
{ return -1 * this; };
-
-
Number.prototype.plus = function(n)
-
{ var s = this.biggerScalar(n);
-
return (Math.round(s*this,0) + Math.round(s*n,0)) / s;
-
};
-
-
Number.prototype.reciprocal = function()
-
{ return 1 / this; };
-
-
Number.prototype.scale = function()
-
{ return Math.pow(10, this.digitsAfterDecimal() ); };
-
Now you can do magical things like:
yielding the correct results.
I am looking forward to javascript 2.0 when I can override the + operator. Maybe I won’t go that far since binary arithmetic is still faster.
[1] http://groups.google.com/group/comp.lang.javascript/…
April 12, 2008 by Dean
acts_as_commentable is a nice little ruby on rails plugin. It extends your ActiveRecord classes giving them comments. We are going to use comments on all kinds of things, starting with recipes, of course. However, AAC lacks a critical feature: the ability for users to approve comments before they are displayed. In this post I am going to run through extending AAC using acts_as_state_machine.
The first thing I did (and do to all the plugins we use) was pistonize the plugin so I could hack on it without fear of getting my changes destroyed.
I start off simply here by adding two states to the Comment model: :pending and :approved.
-
class Comment < ActiveRecord::Base
-
# The first element of this array is the initial state
-
VALID_STATES = [ :pending, :approved ]
-
acts_as_state_machine :initial => VALID_STATES[0]
-
-
event :approve do
-
transitions :from => :pending, :to => :approved
-
end
-
-
VALID_STATES.each do |_state|
-
# Define _state as a state
-
state _state
-
end
-
-
# More code snipped
-
end
Now we are going to write some real code, so here comes a little RSpec. aac provides three class methods:
-
class Comment < ActiveRecord::Base
-
class << self
-
# Helper class method to lookup all comments assigned
-
# to all commentable types for a given user.
-
def find_comments_by_user(user)
-
-
# Helper class method to look up all comments for
-
# commentable class name and commentable id.
-
def find_comments_for_commentable(commentable_str, commentable_id)
-
-
# Helper class method to look up a commentable object
-
# given the commentable class name and id
-
def find_commentable(commentable_str, commentable_id)
-
end
Since it didn’t come with Test::Unit or RSpec tests I wrote up some test for these methods.
-
describe Comment, "class methods" do
-
fixtures :comments, :recipes, :users
-
it "should find comments by user" do
-
Comment.find_comments_by_user( comments(:comment_one).user ).should all_belong_to( comments(:comment_one).user )
-
end
-
-
# This could be more specific
-
it "should find comments for a particular class" do
-
Comment.find_comments_for_commentable( Comments(:comment_one).commentable_type, comments(:comment_one).commentable_id ).should be_an_instance_of(Array)
-
end
-
-
it "should find all comments for a particular class" do
-
# I happen to know that comment_one is a recipe comment
-
Comment.find_commentable( "Recipe", comments(:comment_one).commentable_id ).should be_an_instance_of(Recipe)
-
end
-
-
end
If you are confused by should all_belong_to then you should check out my previous post. With these specs out of the way we can go on to adding more new code.
-
it "should find approved comments by user" do
-
Comment.find_approved_comments_by_user( comments(:comment_one).user ).should all_be_in_state("approved")
-
end
-
-
it "should find pending comments by user" do
-
Comment.find_pending_comments_by_user( comments(:comment_one).user ).should all_be_in_state("pending")
-
end
-
end
Now, normally you would write one spec at a time, but I think I would bore my readers, so I combined these two. Also take note that I am using another custom RSpec matcher all_be_in_state(). It looks a lot like all_belong_to(), so I leave its implementation as an exercise to the reader (unless I can get another blog post out of it). To get these tests to pass I add a few lines of code:
-
VALID_STATES.each do |_state|
-
# Define _state as a state
-
state _state
-
-
# Add Comment.find__comments methods
-
( class << self; self; end ).instance_eval do
-
define_method "find_#{_state}_comments_by_user" do |_user|
-
find_in_state( :all, _state, :conditions => ["user_id = ?", _user.id], :order => "created_at DESC" )
-
end
-
end
-
end
I am not a method_missing kind of guy, and prefer the dynamic-method metaprogramming style. This lot of code defines class methods at runtime that find Comments in specific states. I am actually using whytheluckystiff’s metaid to hide some of the meta-junk, but I thought I should spell it out here for clarity.
Well, now we have a Comment class with two states and code to limit finds to cmments in a specific state. Right now, that is all I have. Here is the full code for the Comment class and the RSpec. You will see another custom RSpec matcher here, require_a().
-
class Comment < ActiveRecord::Base
-
-
# The first element of this array is the initial state
-
VALID_STATES = [ :pending, :approved ]
-
-
acts_as_state_machine :initial => VALID_STATES[0]
-
-
belongs_to :commentable, :polymorphic => true
-
belongs_to :user
-
-
event :approve do
-
transitions :from => :pending, :to => :approved
-
end
-
-
validates_associated :user
-
validates_presence_of :comment, :commentable_id, :commentable_type, :state, :user_id
-
-
VALID_STATES.each do |_state|
-
# Define _state as a state
-
state _state
-
-
# Add Comment.find_<state>_comments methods
-
meta_def "find_#{_state}_comments_by_user" do |_user|
-
find_in_state( :all, _state, :conditions => ["user_id = ?", _user.id],
-
:order => "created_at DESC" )
-
end
-
end
-
-
class < < self
-
-
# Helper class method to look up a commentable object
-
# given the commentable class name and id
-
def find_commentable(commentable_str, commentable_id)
-
commentable_str.constantize.find(commentable_id)
-
end
-
-
# This could be refactored into find_<state>_comments_by_user (somehow)
-
def find_comments_by_user(_user)
-
find( :all, :conditions => ["user_id = ?", _user.id],
-
:order => "created_at DESC" )
-
end
-
-
# Helper class method to look up all comments for
-
# commentable class name and commentable id.
-
def find_comments_for_commentable(commentable_str, commentable_id)
-
find( :all,
-
:conditions => [ "commentable_type = ? and commentable_id = ?",
-
commentable_str, commentable_id ],
-
:order => "created_at DESC" )
-
end
-
-
end
-
-
end</state>
-
require File.dirname(__FILE__) + ‘/../../../../spec/spec_helper’
-
-
module CommentSpecHelper
-
-
end
-
-
describe Comment do
-
-
fixtures :comments
-
-
include CommentSpecHelper
-
-
before(:each) do
-
@comment = Comment.new
-
end
-
-
it "should start out in pending state" do
-
@comment.state.should == "pending"
-
end
-
-
it "sould transition to approved" do
-
@comment = comments(:pending_comment)
-
@comment.approve!
-
@comment.state.should == "approved"
-
end
-
-
it "should require a comment" do
-
@comment.should require_a(:comment)
-
end
-
-
it "should require a commentable_id" do
-
@comment.should require_a(:commentable_id)
-
end
-
-
it "should require a commentable_type" do
-
@comment.should require_a(:commentable_type)
-
end
-
-
it "should require a state" do
-
@comment.should require_a(:state)
-
end
-
-
it "should require a user_id" do
-
@comment.should require_a(:user_id)
-
end
-
-
end
-
-
describe Comment, "class methods" do
-
-
fixtures :comments, :recipes, :users
-
-
it "should find all comments for a particular class" do
-
# I happen to know that comment_one is a recipe comment
-
Comment.find_commentable( "Recipe", comments(:comment_one).commentable_id ).should be_an_instance_of(Recipe)
-
end
-
-
it "should find comments by user" do
-
Comment.find_comments_by_user( comments(:comment_one).user ).should all_belong_to( comments(:comment_one).user )
-
end
-
-
it "should find approved comments by user" do
-
Comment.find_approved_comments_by_user( comments(:comment_one).user ).should all_be_in_state("approved")
-
end
-
-
it "should find pending comments by user" do
-
Comment.find_pending_comments_by_user( comments(:comment_one).user ).should all_be_in_state("pending")
-
end
-
-
# This could be more specific
-
it "should find comments for a particular class" do
-
Comment.find_comments_for_commentable( comments(:comment_one).commentable_type, comments(:comment_one).commentable_id ).should be_an_instance_of(Array)
-
end
-
-
end
–Dean
April 9, 2008 by Dean
I was extending acts_as_commentable and needed a good RSpec test to check the returned objects from its finder methods belonged to the correct user. For example, Comment.find_comments_by_user( :some_user ) should all belong_to :some_user. I’ll be darned if that doesn’t look like a RSpec description. Since there is no all_belong_to matcher, I wrote one.
-
module ActiveRecordValidations
-
class BelongTo
-
def initialize(expected)
-
@expected = expected
-
end
-
-
def matches?(args)
-
args.all? do |target|
-
@target = target
-
@target.send(@expected.class.to_s.downcase) == @expected
-
end
-
end
-
-
def failure_message
-
"expected #{@target.inspect} to all belong to #{@expected}"
-
end
-
-
def negative_failure_message
-
"expected #{@target.inspect} not to all belong to #{@expected}"
-
end
-
end
-
-
def belong_to(expected)
-
BelongTo.new( [expected] )
-
end
-
-
def all_belong_to(expected)
-
BelongTo.new( expected )
-
end
-
end
The matches? method takes an array of objects and goes through them with all? checking that they have a belongs_to the expected thing. Using the example above, each comment object returned by Comment.find_comments_by_user would get tested if comment.user== @user.
–Dean
March 25, 2008 by gregr
Last month the Beer Judges Certification Program (BJCP) released a minor update to their 2004 Style Guidelines. The new “2008 update to the 2004 style guidelines” (as it is officially called) includes some updated style parameters, changes to style descriptions, revised commericial examples for most beer styles and some significant rework to many Belgian ale styles.
In BrewSession we’re using the XML format of the BJCP style guidelines. So as soon as I saw that the 2008 updates had been released, I wanted to make sure we got those into BrewSession as soon as possible. I contacted the BJCP and found that no one was working on updating the BJCP XML. I attempted to contact the original author of the BJCP XML without success (Chris, if you’re out there, give us a hollar, I have some questions). So, I volunteered to update the BJCP XML to the 2008 version.
The BJCP describes it as a minor update, and while there aren’t any new beer styles in the 2008 update, or high level category changes, the number of revisions made to the “fine print” are fairly numerous. While tedious, comparing the 2008 styles with the 2004 XML and making the needed changes is quite interesting, as I’m getting to know the styles like I never did before. In fact, I started going through the styles on my own, buying 2-3 representatives of each style, taking some sips, and reflecting on the style guidelines. My own BJCP course! Dean’s already a certified BJCP judge, but I’m not. However, I think at the end of this I’ll be prepared to take the exam.
In addition to updating the text of the styles, I’m recommending that some elements of the XML be changed, enhanced and updated to make the BJCP Styles XML document more useful to a wider range of people and applications. I’ll be posting my proposals here shortly…
February 4, 2008 by Dean
Login, Signup
We are presented with these quick forms all the time. While it is easy to create standard login and signup pages, Amazon.com has a good one:

What makes it good?
First of all, the prompts are written in plain English. Amazon sells to a wide slice of the population, meaning that about 15% of their customers are probably not very technology-saavy. (-2?) Anything they can do to ease the operation helps their customers buy.
Secondly, when a user visits Amazon.com, there is only one link: “Your account” instead of separate login and signup links. Simple is generally better.
Also note the standard “forgot your password” link plus an additional “has your email address changed?” question. Both are useful to have close at hand.
Implementation
Rails 2.0 strongly encourages you to design RESTful applications. Login forms are associated with Session objects, while signup forms go with User objects (rather, Brewer objects in our case). A simple redirect in the SessionsController#create method takes care of pointing a user in the right direction.
-
-
class SessionsController < ApplicationController
-
def create
-
if params[:signin_action] == ‘new_user’
-
redirect_to new_brewer_path( :brewer => {:email => params[:email]} )
-
else
-
# Do sigin stuff
-
end
-
end
-
end
Note that we pass params[:email] to the new_brewer_path so that field is automatically populated on the next page. If you are using the generated scaffold, you will have to change your BrewersController#new method to instantiate a new @brewer object:
-
-
class BrewersController < ApplicationController
-
def new
-
@brewer = Brewer.new(params[:brewer])
-
end
-
end
Lastly, here is the extra test:
-
-
class SessionsControllerTest < Test::Unit::TestCase
-
def test_should_redirect_to_new_brewer_if_asked
-
an_email = "dean@brewsession.com"
-
post :create, :email => an_email, :signin_action => ‘new_user’
-
assert_redirected_to new_brewer_path(:brewer => {:email => an_email} )
-
assert_nil session[:brewer_id]
-
end
-
end
In a later post I will talk about how to implement the change password action in a RESTful way.
–Dean
June 13, 2007 by Dean
Hi Reader,
I am going to tell you the truth. I was not looking forward to writing the javascript necessary to get BrewSession going. For one thing I am a stickler for strict separation of behavior and content. Cascading Style Sheets are great because they keep the page content from getting lost in the necessary formatting to make a pretty-looking page - there is no equivalent for keeping javascript from obfuscating the stuff on your page.
I have a working prototype for keeping javascript off the page, but I feel like it is just one more thing I need to finish before I can get to the real programming. MochiKit is nice to program with, but laying the foundation for a cool sparkley web site was dragging on.
Enter the Google Web Toolkit. It “…is an open source Java software development framework that makes writing AJAX applications … easy for developers who don’t speak browser quirks as a second language.” Using swing-like widgets and panels, you can quickly put together a functioning app. I do not know swing or even much java, however that is not the obstacle to which I alluded in this post’s title.
I’ve been steaming out a mini-app for … let’s call them a client. It’s quite easy and I’m pleased with the toolkit. I can already see the pieces working together for BrewSession. Now round-about to the problem. It is not currently well-integrated with Rails.
Allow me to geek out on you. Rails is an MVC architecture with great M and C tools and decent V tools. The community believes in doing it yourself, leaning towards being elitist about it. I already have a great start to developing BrewSession into a full-fledged REST service thanks to Rails. I still love working with ruby and am going to keep rails on the server side. All I need to do is get BrewSession, written in GWT, to consume the REST service.
Searching the google group for the toolkit brings up a whole lotta nothing, so it looks like I have to do it myself. The way things are going I will someday refer to myself as a programmer rather than a sys admin.
–Dean
April 16, 2007 by gregr
As Dean mentioned below, we’re working on some calculators that we’ll release as we complete them, since they’re still useful without the rest of the software.
I haven’t posted here yet … so my quick introduction. I’m Greg. I tend to GUI design elements, the web site, brewing calculations, ingredients databases, and a few other things. Of course there’s overlap in what we both do.
Though we can’t make any promises, we’re hoping to release the following calculators in the coming month or two:
1. Carbonation/Draft System Balancer
2. Calorie/Carb Counter
3. Yeast Pitching Rate
4. Kettle Volume
5. Dilution Calculator
Not necessarily in that order, but… To us, those look like a good cross section of functions to just get out there and start having people play with.
UPDATE (May 20) - The Calorie/Carb Counter, Dilution and Kettle volume calculators are nearing completion. Keep on the look out!
January 30, 2007 by Dean
Ruby on Rails allows for plugins to extend its functionality. My first attempt at one is nothing special but I am going to blog about it anyway
I needed a way to check if a number is within a range before saving a record to the database. Rails has a number of good validations built in, but not one that does that. In two or three hours, I was able to piece together a simple plugin that adds “validates_range_of” to my AR models. The code is a little ugly until I can get Greg to install a syntax highlighting plugin for this blog.
-
module ActiveRecord
-
module Validations
-
module ClassMethods
-
def validates_range_of(*attrs)
-
options = { :on => :save }
-
options.update(attrs.pop) if attrs.last.is_a?(Hash)
-
attrs.flatten!
-
unless options[:message]
-
if options[:maximum] && options[:minimum]
-
options[:message] = ‘must be betweeen ‘ + options[:minimum].to_s + ‘ and ‘ + options[:maximum].to_s
-
elsif options[:maximum]
-
options[:message] = ‘must be less than or equal to ‘ + options[:maximum].to_s
-
elsif options[:minimum]
-
options[:message] = ‘must be greater than or equal to ‘ + options[:minimum].to_s
-
else
-
raise ArgumentError, ‘Range unspecified. Specify the :maximum and/or :minimum.’
-
end
-
end
-
-
validates_numericality_of( attrs, options )
-
-
validates_each(attrs, options) do |record, attr_name, value|
-
if ((!options[:maximum].nil?) && (!options[:minimum].nil?)) &&
-
(value > options[:maximum] || value < options[:minimum])
-
record.errors.add( attr_name, options[:message] )
-
elsif (!options[:maximum].nil?) && value > options[:maximum]
-
record.errors.add( attr_name, options[:message] )
-
elsif (!options[:minimum].nil?) && value < options[:minimum]
-
record.errors.add( attr_name, options[:message] )
-
end
-
end
-
end
-
end
-
end
-
end
As you can see this plugin extends AR::Validations. So my models can use validates_range_of to check minimum and maximum values. Let’s look at the BJCP Scoresheet I needed it for:
-
class BjcpScoresheet < ActiveRecord::Base
-
belongs_to :brew_session
-
validates_associated :brew_session
-
validates_range_of :brew_session_id, { :minimum => 1, :message => ‘does not belong to a brew session’ }
-
validates_range_of :aroma_score, { :minimum => 1, :maximum => 12, :only_integer => true }
-
validates_range_of :appearance_score, { :minimum => 1, :maximum => 3, :only_integer => true }
-
validates_range_of :flavor_score, { :minimum => 1, :maximum => 20, :only_integer => true }
-
validates_range_of :mouthfeel_score, { :minimum => 1, :maximum => 10, :only_integer => true }
-
validates_range_of :impression_score, { :minimum => 1, :maximum => 10, :only_integer => true }
How nice - reusable user input validation.
–Dean
January 23, 2007 by Dean
Before the death of my drives I had a flexible Measurement class written up. As most of you know brewing involves all kinds of measurement - hop weights, boil volume, bitterness, etc. The class served as a base that more specific classes would inherit and provided the framework for converting between different measurement systems. For example, the SpecificGravity class would inherit Measurement and provide the conversion code to switch between SG and Plato.
The database would save the scalar and units and instantiate an aggregation of these two pieces. When the units changed, back into the database it went with the new values. It was very easy to work with, but took me quite a bit of time to write.
“Why not use one of the libraries already available?” you might ask. The short answer is that they do not fit my needs. Firstly, none of them produced objects that I could stuff in the database as a aggregation - most are intended as great extensions to various number classes. Secondly, and most importantly, none of them dealt with Bitterness, Color or Specific Gravity, which is really why I need a measurement class. Lastly, most of them only handled linear transformations:
m2 = a * m1 + b
To convert from SG to Plato you need to use a cubic polynomial, ugh.
So I wrote my own, then re-wrote it, and it was beautiful. Now I am re-engineering the whole thing. In addition, I’ll have to figure out where I got the Plato to SG reverse conversion. I remember trying to solve the cubic equation, then finding something that actually worked.
It is a little faster-going because I wrote it all before, but I had some tr1cky 31337 code in there that I will have to figure out again.
Always make sure your backups are working and current.
–Dean
December 25, 2006 by Dean