Archive for January, 2008

true or false javascript objects

>>> var o = new Object();
>>> !!o
true
>>> !o
false
>>> !null
true
>>> !!null
false

// example code
// ...
hasAuthToken: function() {
  return !!this.authToken; // this.authToken != null
}
// ...

validatable_hash

  # -----------------------------------------------------------------------
  # Don't like nasty conditionals when doing option checking ?
  # Good news! They have already been done for u ... by snusnu
  # -----------------------------------------------------------------------
  #
  # How does it work?
  #
  # 1) instance method acts_as_option_hash! gets defined on Hash
  # 2) You call acts_as_option_hash! on any old Hash instance
  # 3) acts_as_option_hash! yields an instance of OptionSpec
  # 4) This instance IS ONLY ADDED TO YOUR Hash INSTANCE
  #    (not to class Hash itself => it becomes a singleton method)
  # 5) You spec your options using methods on OptionSpec
  #    (you will be for
  # 6) Once acts_as_option_hash! returns it evaluates the passed options
  # 7) a) Everything is fine you go use your options
  #       without any further checks
  # 7) b) An InvalidOptions exception is thrown because the passed options
  #       don't match your spec.
  #
  # ----------------------------------------------------------------------
  #
  # DESIRED USAGE
  #
  # ----------------------------------------------------------------------
  #
  # class Foo
  #
  #   options_for :foo do |spec|
  #     spec.only_one_of do |group|
  #       group.option :only do |o|
  #         o.allow [ :sleep, 1, String, Float ]
  #         o.reject [ :bar, 2, 3.14 ]
  #       end
  #       group.option :only do |o|
  #         o.allow [ :sleep, 1, String, Float ]
  #         o.reject [ :bar, 2, 3.14 ]
  #       end
  #     end
  #
  #     spec.option :only do |o|
  #       o.allow [ :sleep, 1, String, Float ], :unless => :except
  #       o.reject [ :bar, 2, 3.14 ]
  #
  #     spec.option :foo do |o|
  #       if spec.present [ :bam ] do ||
  #         o.allow => [ :sleep, 1, String, Float ]
  #         o.reject => [ :bar, 2, 3.14 ]
  #       end
  #       o.if_present [ :bam ]
  #       o.unless_present [ [ "test", :spec ], "fun" ]
  #     end
  #   end
  #
  #   OPTION_SPEC = lambda do |spec|
  #     spec.option :foo do |o|
  #       if spec.present [ :bam ] do ||
  #         o.allow => [ :sleep, 1, String, Float ]
  #         o.reject => [ :bar, 2, 3.14 ]
  #       end
  #       o.if_present [ :bam ]
  #       o.unless_present [ [ "test", :spec ], "fun" ]
  #     end
  #   end
  #
  # end

Read more »

capistrano housekeeping

# RAILS_ROOT
cap deploy:cleanup

# config/deploy.rb
after "deploy", "deploy:cleanup"

sex spam

“Your little soldier will grow up to a big love general!” — Buddy Oneal

that = this;

var Foo = Behavior.create({
var that = this; // work around bug with this in inner functions
this.foo = "bar";
new Ajax.Request(url, {
//...
onSuccess: function(transport) {
alert(that.foo); // this.foo would not work
}
})
});

fix to_json for methods ending with ‘?’

ruby methods are allowed to end with a ‘?’
while javascript Object properties are NOT.

This leads to problems when serializing a ruby
object to_json along with the :methods option
pointing to a method that ends with a ‘?’

# Example:
o.to_json(:methods => :valid?)
# returns something like
# { ..., "valid?": true, ... }

Workaround:

1)
Always access such a property with the [] operator.
Which actually is kind of weird because it’s not
really clear to me, wether this means that ‘?’
IS actually a valid porperty name in javascript
albeit one that just cannot be accessed in all
possible ways javascript syntax would allow it?

# >>> o = { "valid?": true }
# Object valid?=true
# >>> o["valid?"]
# true
# >>> o.valid?
# SyntaxError: syntax error

2)
try to workaround in rails’ to_json which leads
to a few problems (possible names clashing, only ruby=>json and not json=>ruby, …)

module ActiveRecord #:nodoc:
  module Serialization
    class Serializer #:nodoc:

      def serializable_record
        returning(serializable_record = {}) do
          serializable_names.each do |name|
            # ------------------------------------
            js_name = name.to_s.delete('?').to_sym
            serializable_record[js_name] = @record.send(name)
            # ------------------------------------
          end
          add_includes do |association, records, opts|
            if records.is_a?(Enumerable)
              serializable_record[association] = records.collect do |r|
                self.class.new(r, opts).serializable_record
              end
            else
              serializable_record[association] =
              self.class.new(records, opts).serializable_record
            end
          end
        end
      end

    end
  end
end

to_json in rails

user.to_json :include => :posts
user.to_json :only => [ :firstname, :lastname]
user.to_json :except => :password
user.to_json :methods => :errors

get prototype trunk

svn co http://dev.rubyonrails.org/svn/rails/spinoffs/prototype/trunk
cd trunk
rake dist