Skip to content

Instantly share code, notes, and snippets.

View cheeyeo's full-sized avatar
💭
Researching on use of transformers in computer vision

Chee Yeo cheeyeo

💭
Researching on use of transformers in computer vision
View GitHub Profile
@cheeyeo
cheeyeo / adapter_pattern_renderer_class.rb
Created November 2, 2010 11:33
Renderer class for Adapter pattern
class Renderer
def render(text_object)
text = text_object.text
colour = text_object.colour
size = text_object.size_inches
# render the text here
end
end
@cheeyeo
cheeyeo / text_object.rb
Created November 2, 2010 11:34
Text object class for Adapter pattern
class TextObject
attr_accessor :text, :colour, :size_inches
def initialize(text,colour,size_inches)
@text = text
@colour = colour
@size_inches = size_inches
end
end
@cheeyeo
cheeyeo / differen_text_class.rb
Created November 2, 2010 11:35
A different text object for Adapter Pattern
class DifferentTextObject
attr_accessor :string, :color, :size_mm
def initialize(string,color,size_mm)
@string= string
@color = color
@size_mm = size_mm
end
end
@cheeyeo
cheeyeo / text_adapter.rb
Created November 2, 2010 11:36
Text adapter object to wrap different classes using Adapter Pattern
class TextObjectAdapter
def initialize(different_text_object)
@different_text_obj = different_text_object
end
def text
@different_text_obj.string
end
@cheeyeo
cheeyeo / different_text_class_meta.rb
Created November 2, 2010 11:38
Using the singleton class of DifferentTextObject class without an Adapter object
diff_text_obj = DifferentTextObject.new(‘test’,’red’,10)
class << diff_text_obj
def text
@diff_text_obj.string
end
def colour
@diff_text_obj.color
end
@cheeyeo
cheeyeo / spec1.rb
Created November 6, 2010 18:08
Example on how to mock out an API call using RSpec and EM:Http
before :each do
@url = 'http://sns.us-east-1.amazonaws.com:80/?Action=ListTopics&Signature=ItTAjeexIPC43pHMZLCL7utnpK8j8AbTUZ3KGUSMzNc%3D&AWSAccessKeyId=123456&Timestamp=123&SignatureVersion=2&SignatureMethod=HmacSHA256'
EventMachine::MockHttpRequest.reset_registry!
EventMachine::MockHttpRequest.reset_counts!
EventMachine::MockHttpRequest.pass_through_requests = false #set to false to not hit the actual API endpoint
end
it 'should be able to access the API endpoint' do
@cheeyeo
cheeyeo / failing_spec.rb
Created November 7, 2010 18:30
Failing Webmock rspec test
describe 'making requests to SNS API' do
before :each do
AmazeSNS.akey = '123456'
AmazeSNS.skey = '123456'
@time_stub = stub("Time")
WebMock.reset!
WebMock.disable_net_connect!
@cheeyeo
cheeyeo / request_object.rb
Created November 7, 2010 18:32
This is the file where WebMock keeps pointing to 'Can't convert string to hash'
@httpresponse ||= http_class.new("http://#{AmazeSNS.host}/?").get({
:query => querystring2, :timeout => 2
})
@httpresponse.callback{
begin
success_callback
deferrable.succeed
rescue => e
deferrable.fail(e)
end
@cheeyeo
cheeyeo / rspec_optimize.rb
Created January 21, 2011 18:01
Rspec optmization techniques borrowed from 37Signals
# method for clearing out instance variables from code blocks
# usage:
Spec::Runner.configure do |config|
.....
config.after(:each) do
scrub_instance_variables
end
end
@cheeyeo
cheeyeo / rspec_optmization_2.rb
Created January 21, 2011 18:15
Optimizing the garbage collector in Rspec
# delay the GC - usage as below
# Spec::Runner.configure do |config|
.....
# config.before(:each) do
# begin_gc_deferment
# end
# config.after(:each) do