Slide 1

Slide 1 text

DDD Documentation Drive Development

Slide 2

Slide 2 text

TomDoc http://tomdoc.org/

Slide 3

Slide 3 text

# Public: Duplicate some text an arbitrary number of times. # # text - The String to be duplicated. # count - The Integer number of times to duplicate the text. # # Examples # # multiplex('Tom', 4) # # => 'TomTomTomTom' # # Returns the duplicated String. def multiplex(text, count) text * count end

Slide 4

Slide 4 text

1- Find a method you don’t understand

Slide 5

Slide 5 text

def active? return true if ( Time.now.utc > self.start_date && self.end_date.nil?) return (Time.now.utc > self.start_date && Time.now.utc < self.end_date) end

Slide 6

Slide 6 text

2 - Write a documentation to it

Slide 7

Slide 7 text

# Public: checks if the campaign is active. # # Return `true` if the campaign started in the past and the end date is # `nil` or is in the future. def active? return true if ( Time.now.utc > self.start_date && self.end_date.nil?) return (Time.now.utc > self.start_date && Time.now.utc < self.end_date) end

Slide 8

Slide 8 text

3 - See if tests match the documentation

Slide 9

Slide 9 text

it "returns true if the campaign is active right now" do active_voucher_campaign.should be_active end it "returns false if the campaign's end date is in past" do inactive_voucher_campaign.should_not be_active end it "returns true if the campaign has started in past and there is no end date" voucher_campaign_without_end_date.should be_active end

Slide 10

Slide 10 text

5 - Refactor

Slide 11

Slide 11 text

# Public: checks if the campaign is active. # # Return `true` if the campaign started in the past and the end date is # `nil` or is in the future. def active? now = Time.now.utc return true if (started_in_past?(now) && self.end_date.nil?) return (started_in_past?(now) && now < self.end_date) end # Internal: checks if the campaign started in the past. # # time - the time you want to compare. def started_in_past?(time) time > start_date end

Slide 12

Slide 12 text

6 - Repeat

Slide 13

Slide 13 text

# Public: checks if the campaign is active. # # Return `true` if the campaign started in the past and the end date is # `nil` or is in the future. def active? now = Time.now.utc return true if (started_in_past?(now) && self.end_date.nil?) return (started_in_past?(now) && will_end_in_future?(now)) end # Internal: checks if the campaign started in the past. # # time - the time you want to compare. def started_in_past?(time) time > start_date end # Internal: checks if the campaign will end in the future. # # time - the time you want to compare. def will_end_in_future?(time) time < end_date end

Slide 14

Slide 14 text

# Public: checks if the campaign is active. # # Return `true` if the campaign started in the past and the end date is # `nil` or is in the future. def active? now = Time.now.utc started_in_past?(now) && (self.end_date.nil? || will_end_in_future? (now)) end # Internal: checks if the campaign started in the past. # # time - the time you want to compare. def started_in_past?(time) time > start_date end # Internal: checks if the campaign will end in the future. # # time - the time you want to compare. def will_end_in_future?(time) time < end_date end

Slide 15

Slide 15 text

# Public: checks if the campaign is active. def active? now = Time.now.utc started_in_past?(now) && (self.end_date.nil? || will_end_in_future?(now)) end # Internal: checks if the campaign started in the past. # # time - the time you want to compare. def started_in_past?(time) time > start_date end # Internal: checks if the campaign will end in the future. # # time - the time you want to compare. def will_end_in_future?(time) time < end_date end