Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fractal Design

Fractal Design

Given at the 2011 Ruby Hoedown

Ben Scofield

August 27, 2011
Tweet

More Decks by Ben Scofield

Other Decks in Programming

Transcript

  1. Methods def add_messages
 delivery_lists.each do |list_id|
 ponies = ponies_in_batches(:list_id =>

    list_id)
 
 while ponies.any?
 create_messages_for_ ponies(ponies).each { |message| add_message(message) }
 ponies = ponies_in_batches(:last_id => ponies.last.id, :list_id => list_id)
 end
 end
 end

  2. Classes class Pony
 def initialize(options = {})
 # ...
 end


    
 def prance
 # ...
 end
 
 # ...
 end

  3. Libraries Gem::Specification.new do |s|
 s.name = %q{brohoof}
 s.version = "0.3.0"


    
 s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? ... s.authors = ["Rainbow Dash"]
 s.date = %q{2011-08-27}
 s.description = %q{Brohoof gives hoof-fives to your fellow bronies.}
 s.email = %q{[email protected]}
 s.extra_rdoc_files = [
 "LICENSE.txt",
 "README.md"
 ]
 
 # ...
 end
  4. Applications # This file is used by Rack-based servers to

    start the application.
 
 require ::File.expand_path('../config/environment', __FILE__)
 run Celestia::Application

  5. Data CREATE TABLE `cities` (
 `id` int(11) NOT NULL AUTO_INCREMENT,


    `name` varchar(255) DEFAULT NULL,
 `created_at` datetime DEFAULT NULL,
 `updated_at` datetime DEFAULT NULL,
 `latitude` double DEFAULT NULL,
 `longitude` double DEFAULT NULL,
 `seo_name` varchar(255) DEFAULT NULL,
 `country_id` int(11) DEFAULT NULL,
 `inhabitants` int(11) NOT NULL DEFAULT '0',
 PRIMARY KEY (`id`),
 UNIQUE KEY `index_cities_on_seo_name` (`seo_name`),
 KEY `index_cities_on_country_id` (`country_id`),
 ) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8;
  6. Tests class UnicornTest < ActiveSupport::TestCase
 test "unicorns should have magic"

    do
 trixie = Unicorn.new
 
 assert trixie.magical?
 end
 end
  7. Database Changes class AddVelocityToPonies
 def self.up
 add_column :ponies, :max_velocity, :float


    end
 
 def self.down
 remove_column :ponies, :max_velocity
 end
 end
  8. Methods def plan_party(date)
 if is_a_good_day_for_a_party?(date)
 possible_venues = Venue.available_on(date)
 venue =

    possible_venues.first
 
 bands = Band.order('awesome DESC').all
 bands.each do |band|
 break if band.book(date)
 end
 
 buy_food
 
 invite_ponies(date, venue)
 end
 end
  9. Bitfields class AddPowersToPonies
 def self.up
 add_column :ponies, :powers, :int
 end


    
 def self.down
 remove_column :ponies, :powers
 end
 end Pony.powers << :magic
 Pony.powers << :flight
 
 # Time passes
 
 Pony.powers << :likes_to_plan_parties
 Pony.powers << :pink_mane
  10. Modules module PonyUtilities
 def comb_mane
 # ...
 end
 
 def

    cleanse_parasprites
 # ...
 end
 
 def expose_impostor
 # ...
 end
 end
  11. Mocks and Stubs test "a unicorn should be able to

    ask her friends for advice" do
 spike = stub(:name => 'Spike', :advice => "Keep trying!")
 twilight_sparkle = Unicorn.new
 twilight_sparkle.friends << spike
 
 assert_equal ['Spike: Keep trying!'], twilight_sparkle.ask_for_advice
 end
  12. ...

  13. Methods def right_pony_for_the_job(pony, job)
 case job.class.name
 when 'Party': pony ==

    PINKIE_PIE
 when 'Magic': pony.is_a?(Unicorn)
 when 'AnimalSoothing': pony == FLUTTERSHY
 when 'Flight': pony.can_fly?
 else false
 end
 end

  14. AJAX $.ajax('/friendship-lessons', {
 error: function(xhr, status, err) {
 // ...


    },
 success: function(data, status, xhr) {
 alert("Here's what I learned...");
 }
 });
  15. EventMachine class Orchard
 def initialize(owner)
 request = EM::HttpRequest.new('http://orchards.com').get
 
 request.callback

    {
 if request.response_header.status == 200
 data = JSON.parse(request.response)["responseData"]
 self.succeed data['ripe_apple_count']
 else
 self.fail("Orchard retrieval error")
 end
 }
 end
 end
 
 EM.run {
 orchard = Orchard.new('Applejack')
 orchard.callback { |count| puts "We’ve got #{count} ripe apples!" }
 }