This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def valid_dob? value | |
begin | |
date = Date.parse value | |
if date.year < 1900 || date.year > Date.today.year #some sensible years | |
return false | |
end | |
return true | |
rescue ArgumentError # malformed string | |
false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe Object do | |
describe "#blank?" do | |
- it "should return true for blank objects" do | |
- [nil, false, [], {}].each { |object| object.should be_blank } | |
+ [nil, false, [], {}].each do |blanky| | |
+ it %Q{should return true for '#{blanky.inspect}' objects} do | |
+ blanky.blank?.should be_true | |
+ end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is the current solution. It merely validates the string's inclusion in | |
# the given list, and saves it as a string. | |
ActiveRecord::Schema.define(:version => 20110101000000) do | |
create_table "foos", :force => true do |t| | |
t.string "bar" | |
end | |
end | |
class Foo < ActiveRecord::Base |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "bigdecimal" | |
# It would be nice if BigDecimal( BigDecimal("1") ) returned '1' as bigdecimal instead of an argument error, as it generally expects a String. | |
#I ran into this problem upgrading a project to rails3 from rails2 that was using the old mysql adapter. In the conversion, we switched to mysql2 which auto converts query results to ruby-land types, hence BigDecimal(sql_decimal_result_that_used_to_be_a_string) will now fail as the adapter returns a BigDecimal. | |
# The real world solution was to remove the BigDecimal wrapper around those sql queries, but the below was an interesting exercise nonetheless. | |
# BigDecimal("1") really means call a special global function 'BigDecimal' defined on Kernel. The C code is straight copy/pasta of BigDecimal.new :[ | |
# Make it go away | |
Kernel.send :remove_method, :BigDecimal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Compile CityHash | |
#g++ -shared city.cc -o libcity.dylib | |
# | |
# Figure out the symbol names.. this doesn't look right. Probably user error. | |
# nm -g libcity.dylib | |
# | |
=begin | |
0000000000000db7 T __Z10CityHash64PKcm | |
0000000000001800 T __Z11CityHash128PKcm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | |
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | |
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | |
/*!40101 SET NAMES utf8 */; | |
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; | |
/*!40103 SET TIME_ZONE='+00:00' */; | |
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; | |
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; | |
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PhoneNumber < ActiveRecord::Base | |
belongs_to :phonable, :polymorphic => true | |
validates :number, :length => { :minimum => 10, :maximum => 12} | |
validates_length_of :area_code, :is => 3 # 205 | |
validates_length_of :prefix, :is => 3 # 867 | |
validates_length_of :line, :is => 4 # 5309 | |
def number=(val) | |
val.gsub! /[^\d]/, '' # Prune dashes, parenthesis, etc | |
self.area_code, self.prefix, self.line = val.scan( /(\d{3})(\d{3})(\d{4})/ ).flatten |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
combatlog "/path/to/your_combat_log.txt" do | |
find :target => [:player, :pet] # Only show events where the target is a player | |
remove :spell => [:FallDamage, :BuffGain, :BuffFade] # Filter out any world damage & buff events | |
end | |
=> returns instance of EventCollection |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## My branch that fixes some n+1 madness | |
Started GET "/dashboard/project?id=219" for 127.0.0.1 at 2011-02-25 16:57:44 -0600 | |
Processing by Dashboard::BaseController#project as JSON | |
Parameters: {"id"=>"219"} | |
SQL (0.4ms) describe `roles_users` | |
User Load (0.0ms) SELECT `users`.* FROM `users` WHERE (`users`.`id` = 11) LIMIT 1 | |
SQL (0.2ms) describe `roles_users` | |
Role Load (0.1ms) SELECT `roles`.* FROM `roles` INNER JOIN `roles_users` ON `roles`.id = `roles_users`.role_id WHERE (`roles_users`.user_id = 11 ) AND (`roles`.`name` = 'admin') LIMIT 1 | |
Role Load (0.0ms) SELECT `roles`.* FROM `roles` WHERE (name = 'admin' and authorizable_type IS NULL and authorizable_id IS NULL) LIMIT 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/rmodeling/dsl.rb b/lib/rmodeling/dsl.rb | |
new file mode 100644 | |
index 0000000..ff1a795 | |
--- /dev/null | |
+++ b/lib/rmodeling/dsl.rb | |
@@ -0,0 +1,30 @@ | |
+module RModeling | |
+ module DSL | |
+ def self.included(base) | |
+ base.send(:include, InstanceMethods) |