June 23rd, 2009 cschneid
I was using Active Record in a batch application to manipulate some data, and I needed the timezone handling built into the newer versions. But I had a hell of a time trying to figure out how to make it all work.
In plain Rails, it’s simple:
Rails::Initializer.run do |config|
config.time_zone = 'Pacific Time (US & Canada)'
end
But I wasn’t running Rails proper, and didn’t want to pull in the whole Rails boot sequence. So the Rails::Initializer call didn’t work. To do it manually, time zone config turns out it’s a 3 lines of setup calls:
require 'activerecord'
Time.zone = "Pacific Time (US & Canada)"
ActiveRecord::Base.time_zone_aware_attributes = true
ActiveRecord::Base.default_timezone = "Pacific Time (US & Canada)"
When you do that, time zone attributes work both going into the database, and coming back out. If you miss the last line, they’ll just not work coming out, which is a damn confusing thing to deal with.
Ohh, and one other thing I ran into when getting this setup is that AR pulls in Active Support, which in turn pulls in Builder (to hack xml support or something). Just watch out for that, you’ll get weird crashes if you don’t have builder installed. Honestly, I just went and commented out the areas of Active Support that did it, I didn’t want builder anyway.
Posted in Programming, Rails, Ruby, Sinatra | 1 Comment »
May 12th, 2009 admin
I was getting this error, and it turns out it’s just that builder isn’t installed. sudo gem install builder, and you’re fixed.
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- blankslate (MissingSourceFile)
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
from ./vendor/rails/activerecord/lib/../../activesupport/lib/active_support/basic_object.rb:21
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
from ./vendor/rails/activerecord/lib/../../activesupport/lib/active_support/duration.rb:1
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
from ./vendor/rails/activerecord/lib/../../activesupport/lib/active_support/core_ext/time/calculations.rb:1
... 18 levels...
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
Posted in Programming | 1 Comment »
May 8th, 2009 cschneid
I’m writing this post as a brain dump and for future reference. I was doing research on Tokyo Cabinet and Tyrant today for an upcoming project.
Tokyo Cabinet - A Key Value database that’s stupidly fast
Tokyo Tyrant - A network layer on top of Tokyo Cabinet
Tokyo Dystopia - A full text search (inverted index) on top of Tokyo Cabinet
Official Docs:
These include mostly the official C API docs. They are useful if you skim through them, not for the C definitions, but to get an idea of what is possible. You will get lots of sparse explanations, but it’s a start.
Ruby Libraries:
More About Rufus Library:
OSX Installation:
Macports version of Tokyo Cabinet and Tokyo Tyrant are broken. The current state is that there are incompatible versions of cabinet & tyrant, and that there are Portfile problems as well, where the checksum doesn’t match the downloaded file. I eventually gave up, and just used the instructions at http://openwferu.rubyforge.org/tokyo.html to install it manually (the version numbers in that post are old, but otherwise accurate). Those instructions install it to /usr/local/bin, so you may need to add it to your path.
Key Value Store Theory:
More Cabinet Docs:
Cabinet Storage Engines:
I’m still vague on the real differences in this section. I’ll expand out as I learn and understand more. What I do know is that B-Tree and Table have additional methods on them, and that Tyrant doesn’t support at least the B-Tree ones (at a C level, it’s not a problem with the Ruby bindings).
- Hash - Standard hash structure for storing a key.
- B-Tree - Tree structure to store keys. Allows for ordering, and multiple values.
- Table - ?? Allows storing of arbitrary keys, and indexes (like CouchDB?).
- ?? - I think array is another one, for fixed length values, but I haven’t gone into that one yet
Other Items:
Lightcloud is some sort of distributed layer on top of tyrant, which gives some horizontal scaling ability. I haven’t looked much further than that.
That’s enough for now…. I’ll try to keep this updated as I learn.
Posted in Programming, Ruby | No Comments »
May 6th, 2009 cschneid
I am working on getting my development environment setup on my new mac, and ran into an under-documented error: “could not find any SCM named `git’”.
It turns out that Leopard installs capistrano 2.0 automatically, and in a battle between /usr/bin/ and /opt/local/bin, the first one on the path of course wins. The trick is to run sudo /usr/bin/gem uninstall capistrano which will get rid of both the gem, and more importantly the cap and capify commands that came with it. Now just install capistrano in your macports install of gems sudo /opt/local/bin/gem install capistrano and run cap: /opt/local/bin/cap deploy
This blog post was very helpful while I was hunting down this info.
Posted in Programming | No Comments »
February 24th, 2009 cschneid
Something I hate is the line that goes off into infinity on the right of my editor. And of course, too often, it’s in my HAML code, where it’s totally non-obvious how to break a line into several parts.
The answer is straight forward though. Just end each line of the statement (first, last, and everything in between!) with a pipe | character.
= { :foo => :bar, |
:baz => :foo, |
:quux => :zz}.inspect |
Posted in Programming | 1 Comment »
December 15th, 2008 cschneid
Over the weekend I updated the IRCLogger code. All of the changes are on the visual side, no new features got introduced.
What I did do though was to change my display logic from using dl lists, to using divs. The advantage is that it now renders correction in IE7 (and probably 6).
Anyway, check it out for all your Sinatra logging needs.
Posted in Programming | No Comments »
December 15th, 2008 cschneid
Sinatra makes amazing web service endpoints. The lightweight “nothing in your way” approach is great. Rendering alternate media types isn’t immediately obvious though. Here’s a little snippet that demonstrates rendering JSON. XML would work in a very similar way.
Posted in Programming, Ruby, Sinatra | 4 Comments »
November 5th, 2008 cschneid
Check out the new public Sinatra website at http://sinatra.rubyforge.org. It has a up-to-date build of the Sinatra Book, Sinatra API documentation, and all the hot mini-framework action you could want.
Posted in Programming | 1 Comment »
September 10th, 2008 cschneid
Sinatra provides a helpful pair of special handlers for 404 and 500 errors. By default, Sinatra has an ugly “Not Found” and “Error” message, these allow you to customize them, and render your own pages.
It’s pretty simple:
# Render views/404.haml
not_found do
haml :'404'
end
# Render views/500.haml
# @e holds whatever was thrown, in this example, a string,
# but it could have an Error class of some sort.
error do
@e = request.env['sinatra_error']
haml :'500'
end
get '/' do
raise "Error happened!"
end
Notice that I had to surround the templates in single quotes. This is because ruby syntax doesn’t let symbol’s first character be a number. By quoting it, it gets around that issue.
Posted in Programming | 2 Comments »
September 9th, 2008 cschneid
Ryan Tomayko provided a great changelist for the new gem release of Sinatra, which I hope to expand on a bit here, by pointing you to more details on the changes.
Here’s a quick summary of changes since the 0.2.2 release:
-
Add sinatra.gemspec w/ support for github gem builds. Forks can now
enable the build gem option in github to get free username-sinatra.gem
builds: gem install username-sinatra.gem — source=http://gems.github.com/
-
Require Rack 0.4; removes frozen rack dir.
-
Basic RSpec support; require ‘sinatra/test/rspec’ instead of
‘sinatra/test/spec’ to use. [avdi]
-
before filters can modify request environment vars used for
routing (e.g., PATH_INFO, REQUEST_METHOD, etc.) for URL rewriting
type functionality.
-
In-file templates now uses @@ instead of ## as template separator.
-
Top-level environment test predicates: development?, test?, production?
-
Top-level “set”, “enable”, and “disable” methods for tweaking
app options. [rtomayko] — This allows the syntax in my post about options
-
Top-level “use” method for building Rack middleware pipelines
leading to app. see readme for usage. [rtomayko] — this is the use syntax in my post about sessions
-
New “reload” option - set false to disable reloading in development.
-
New “host” option - host/ip to bind to [cschneid]
-
New “app_file” option - override the file to reload in development
mode [cschneid] — This makes embedding Sinatra inside other apps easier to do during development. More info in my app_file post.
-
Development error/not_found page cleanup [sr, adamwiggins]
-
Remove a bunch of core extensions (String#to_param, String#from_param,
Hash#from_params, Hash#to_params, Hash#symbolize_keys, Hash#pass)
-
Various grammar and formatting fixes to README; additions on
community and contributing [cypher]
-
Build RDoc using Hanna template: http://sinatra.rubyforge.org/api/
-
Specs, documentation and fixes for splat’n routes [vic] — See how to use splat routes in my post on it.
-
Fix whitespace errors across all source files. [rtomayko]
-
Fix streaming issues with Mongrel (body not closed). [bmizerany]
-
Fix various issues with environment not being set properly (configure
blocks not running, error pages not registering, etc.) [cypher]
-
Fix to allow locals to be passed to ERB templates [cschneid] — More Details
-
Fix locking issues causing random errors during reload in development.
-
Fix for escaped paths not resolving static files [Matthew Walker]
Posted in Programming, Ruby, Sinatra | No Comments »