68. class WebClient
def get(url)
url = URI.parse url
Net::HTTP.get(url.host, url.path)
end
end
Saturday, June 18, 2011
69. class WebClient
def get(url)
url = URI.parse url
http_get
end
private
def http_get
Net::HTTP.get(url.host, url.path)
end
end
Saturday, June 18, 2011
70. I don't like it, but
it helps us to
class WebClient reason
def get(url)
url = URI.parse url
http_get(url.host, url.path)
end
private
def http_get(host, path)
Net::HTTP.get(host, path)
end
end
Saturday, June 18, 2011
72. class Foo
def metaclass
class << self; self; end
end
end
x = Foo.new
p x.metaclass.ancestors
x.extend(Module.new { })
p x.metaclass.ancestors
Saturday, June 18, 2011
88. require 'net/http'
class WebClient
def get(url)
url = URI.parse url
Net::HTTP.get(url.host, url.path)
end
end
Saturday, June 18, 2011
89. class MyHTTP
def self.get(host, path)
"hello world"
end
end
WebClient.const_set(:Net, Module.new)
WebClient::Net.const_set(:HTTP, MyHTTP)
Saturday, June 18, 2011
90. class WebTest < MiniTest::Unit::TestCase
def setup
WebClient.const_set(:Net, Module.new)
WebClient::Net.const_set(:HTTP, MyHTTP)
end
def teardown
WebClient.send(:remove_const, :Net)
end
def test_get
client = WebClient.new
response = client.get 'http://www.reddit.com/r/ruby'
assert_equal 'hello world', response
end
end
Saturday, June 18, 2011
91. class WebClient
def get(url)
url = URI.parse url
::Net::HTTP.get(url.host, url.path)
end
end
Saturday, June 18, 2011
92. Localized
Still must changes to
mock one class
entire API
Heavy Handed
Saturday, June 18, 2011
117. def table_rows
rows[table_name] = fixtures.map do |label, fixture|
if model_class && model_class < ActiveRecord::Base
reflection_class.reflect_on_all_associations.each do |association|
case association.macro
when :belongs_to
# Do not replace association name with association foreign key if they are named the
same
fk_name = (association.options[:foreign_key] || "#{association.name}_id").to_s
if association.name.to_s != fk_name && value = row.delete(association.name.to_s)
if association.options[:polymorphic] && value.sub!(/s*(([^)]*))s*$/, "")
# support polymorphic belongs_to as "label (Type)"
row[association.foreign_type] = $1
end
row[fk_name] = ActiveRecord::Fixtures.identify(value)
end
when :has_and_belongs_to_many
if (targets = row.delete(association.name.to_s))
targets = targets.is_a?(Array) ? targets : targets.split(/s*,s*/)
table_name = association.options[:join_table]
rows[table_name].concat targets.map { |target|
{ association.foreign_key => row[primary_key_name],
association.association_foreign_key => ActiveRecord::Fixtures.identify
(target) }
}
end
end
end
end
row
end
rows
end
Saturday, June 18, 2011
119. def belongs_to_row(association, row)
# Do not replace association name with association foreign key if
they are named the same
fk_name = (association.options[:foreign_key] || "#
{association.name}_id").to_s
if association.name.to_s != fk_name && value = row.delete
(association.name.to_s)
if association.options[:polymorphic] && value.sub!(/s*(([^)]
*))s*$/, "")
# support polymorphic belongs_to as "label (Type)"
row[association.foreign_type] = $1
end
row[fk_name] = ActiveRecord::Fixtures.identify(value)
end
Saturday, June 18, 2011
120. def habtm_row(association, row)
if (targets = row.delete(association.name.to_s))
targets = targets.is_a?(Array) ? targets : targets.split(/s*,
s*/)
table_name = association.options[:join_table]
rows[table_name].concat targets.map { |target|
{ association.foreign_key => row
[primary_key_name],
association.association_foreign_key =>
ActiveRecord::Fixtures.identify(target) }
}
end
end
Saturday, June 18, 2011
121. def table_rows
rows[table_name] = fixtures.map do |label, fixture|
row = fixture.to_hash
if model_class && model_class < ActiveRecord::Base
# If STI is used, find the correct subclass for association reflection
reflection_class =
if row.include?(inheritance_column_name)
row[inheritance_column_name].constantize rescue model_class
else
model_class
end
reflection_class.reflect_on_all_associations.each do |association|
case association.macro
when :belongs_to
belongs_to_row(association, row)
when :has_and_belongs_to_many
habtm_row(association, row)
end
end
end
row
end
rows
end
Saturday, June 18, 2011
123. class RowFilter
def rows
fixtures.map do |label, fixture|
row = fixture.to_hash
if model_class && model_class < ActiveRecord::Base
# If STI is used, find the correct subclass for association reflection
reflection_class =
if row.include?(inheritance_column_name)
row[inheritance_column_name].constantize rescue model_class
else
model_class
end
reflection_class.reflect_on_all_associations.each do |association|
case association.macro
when :belongs_to
belongs_to_row(association, row)
when :has_and_belongs_to_many
habtm_row(association, row)
end
end
end
row
end
end
end
Saturday, June 18, 2011
124. class RowFilter
attr_reader :fixtures, :model_class
def initialize(fixtures, model_class)
@fixtures = fixtures
@model_class = model_class
end
end
Saturday, June 18, 2011
125. def table_rows
filter = RowFilter.new(fixtures, model_class)
rows[table_name] = filter.rows
rows
end
Saturday, June 18, 2011
133. Once we had
extracted the HTTP
class WebClient methods to their
own functions, could
def get(url) reason about HTTP
url = URI.parse url api.
http_get(url.host, url.path)
end
private
def http_get(host, path)
Net::HTTP.get(host, path)
end
end
Saturday, June 18, 2011
134. class WebClient
def initialize(client = Net::HTTP)
@client = client
end
def get(url)
url = URI.parse url
http_get(url.host, url.path)
end
private
def http_get(host, path)
@client.get(host, path)
end
end
Saturday, June 18, 2011
135. class TestHTTP < Struct.new(:data)
def get(host, path)
data[[host, path]]
end
end
Saturday, June 18, 2011
136. class WebTest < Test::Unit::TestCase
def test_get_home
mockhttp = TestHTTP.new({
['localhost', '/~aaron/'] => 'hello'
})
wc = WebClient.new mockhttp
assert_equal 'hello',
wc.get('http://localhost/~aaron/')
end
end
Saturday, June 18, 2011
137. We define
expectations
Saturday, June 18, 2011