This file contains 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
Note 1. | |
Single Table Inheritance | |
http://api.rubyonrails.org/classes/ActiveRecord/Base.html | |
class Teacher < User | |
has_many :courses | |
end | |
class Course |
This file contains 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
DATES = ["2012-01-01", | |
"2012-02-01", | |
"2012-03-01", | |
"2012-04-01", | |
"2012-05-01", | |
"2012-06-01", | |
"2012-07-01", | |
"2012-08-01", | |
"2012-09-01", | |
"2012-10-01", |
This file contains 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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'gattica' | |
# Login | |
ga = Gattica.new({ | |
:email => '', | |
:password => '' | |
}) |
This file contains 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
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
# ====================================================== | |
# <N+1 problem> | |
# | |
# Post.all.each do |p| | |
# puts p.category.name | |
# end |
This file contains 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
unless File.exist?('Gemfile') | |
File.write('Gemfile', <<-GEMFILE) | |
source 'https://rubygems.org' | |
gem 'rails', github: 'rails/rails' | |
gem 'arel', github: 'rails/arel' | |
gem 'sqlite3' | |
GEMFILE | |
system 'bundle' | |
end |
This file contains 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
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
# ====================================================== | |
# <N+1 problem> | |
# | |
# Post.all.each do |p| | |
# puts p.category.name | |
# end |
This file contains 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
# Despite the added complexity, method_missing is a powerful tool that needs to be used when the interface of a class cannot be predetermined. | |
# Use Isolate Dynamic Receptor to move the method_missing behavior to a new class: a class whose sole responsibility is to handle the method_missing cases. | |
class MessageCollector | |
instance_methods.each do |meth| | |
undef_method meth unless meth =~ /^(__|inspect)/ | |
end | |
def messages | |
@messages ||= [] |
This file contains 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 Decorator | |
def initialize(subject) | |
@subject = subject | |
end | |
def method_missing(sym, *args, &block) | |
@subject.send sym, *args, &block | |
end | |
end |
This file contains 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
module CustomInitializers | |
def hash_initializer(*attribute_names) | |
define_method(:initialize) do |*args| | |
data = args.first | |
attribute_names.each do |attr| | |
instance_variable_set "@#{attr}", data[attr] | |
end | |
end | |
self.class_eval do |
This file contains 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
#!/usr/bin/env ruby | |
require 'mongoid' | |
require 'mongoid/support/query_counter' | |
Mongoid.configure.connect_to("mongoid_test") | |
def count_queries(&block) | |
query_counter = Mongoid::QueryCounter.new | |
query_counter.instrument(&block) |
OlderNewer