Skip to content

Commit

Permalink
Whitespace fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mlipper committed Apr 16, 2016
1 parent 1b9db3d commit 7ab3887
Show file tree
Hide file tree
Showing 48 changed files with 1,465 additions and 1,466 deletions.
22 changes: 11 additions & 11 deletions README.md
Expand Up @@ -21,12 +21,12 @@ a_monday = Date.new(2013,5,13) # Monday, May 13 - has "day-leve
a_wednesday = DateTime.new(2013,5,15,8,45) # Wednesday, May 15 at 8:45am - has "minute-level" precision

monday_expr = DIWeek.new(Mon) # Matches any Monday
monday_expr.include?(a_monday) # => true
monday_expr.include?(a_wednesday) # => false
monday_expr.include?(a_monday) # => true
monday_expr.include?(a_wednesday) # => false

wednesday_expr = DIWeek.new(Wed) # Matches any Wednesday
wednesday_expr.include?(a_monday) # => false
wednesday_expr.include?(a_wednesday) # => true
wednesday_expr.include?(a_monday) # => false
wednesday_expr.include?(a_wednesday) # => true

#
# Use an "OR" between two expressions
Expand All @@ -35,17 +35,17 @@ mon_or_wed_expr = monday_expr | wednesday_expr # Matches any Monday OR any Wedn
mon_or_wed_expr.include?(a_monday) # => true
mon_or_wed_expr.include?(a_wednesday) # => true

daily_8_to_11_expr =REDay.new(8,00,11,00,false) # Matches from 8am to 11am on ANY date.
# The 'false' argument says not to auto-match
daily_8_to_11_expr =REDay.new(8,00,11,00,false) # Matches from 8am to 11am on ANY date.
# The 'false' argument says not to auto-match
# expressions of lesser precision.

at_9 = DateTime.new(2013,5,12,9,0) # Sunday, May 12 at 9:00am
daily_8_to_11_expr.include?(at_9) # => true
daily_8_to_11_expr.include?(at_9) # => true
#
# On the next line, the given Date instance is "promoted" to the minute-level precision
# On the next line, the given Date instance is "promoted" to the minute-level precision
# required by the temporal expression so the time component defaults to 00:00
#
daily_8_to_11_expr.include?(a_monday) # => false
daily_8_to_11_expr.include?(a_monday) # => false

#
# Use an "AND" between two expressions to match
Expand All @@ -68,9 +68,9 @@ mon_or_wed_8_to_11_expr.include?(a_wednesday) # => true - a Wednesday at 8:45

## Etc...

**Author:** Matthew Lipper <mlipper@gmail.com>
**Author:** Matthew Lipper <mlipper@gmail.com>

**Requires:** Tested with J/Ruby 1.8.7, 1.9.3 and Ruby 2.0.x
**Requires:** Tested with J/Ruby 1.8.7, 1.9.3 and Ruby 2.0.x

**License:** Released under the MIT License (see LICENSE.txt).

Expand Down
42 changes: 21 additions & 21 deletions doc/tutorial_schedule.md
Expand Up @@ -4,7 +4,7 @@

* In his [paper](http://martinfowler.com/apsupp/recurring.pdf) about recurring events, Martin Fowler also discusses a simple schedule API which is used, surprisingly enough, to build a schedule. We're not going to cover the pattern itself in this tutorial as Fowler already does a nice job. Because it is such a simple pattern (once you invent it!), you'll be able understand it even if you decide not to read his paper.

So, let's pretend that I own a car. Since I don't want to get a ticket, I decide to create an application which will tell me where and when I can park it on my street. (Since this is all make believe anyway, my car is a late 60's model black Ford Mustang with flame detailing (and on the back seat is one million dollars)).
So, let's pretend that I own a car. Since I don't want to get a ticket, I decide to create an application which will tell me where and when I can park it on my street. (Since this is all make believe anyway, my car is a late 60's model black Ford Mustang with flame detailing (and on the back seat is one million dollars)).

We'll build a Runt Schedule that models the parking regulations. Our app will check this Schedule at regular intervals and send us reminders to move our car so we don't get a ticket. YAY!

Expand All @@ -18,28 +18,28 @@ Thus...

<pre>
############################# #############################
# # # #
# NO PARKING # # NO PARKING #
# # # #
# Mon, Wed, Fri 8am-11am # # Tu, Th 11:30am-2:00pm #
# # # #
# # # #
# Violators will be towed! # # Violaters will be towed! #
# # # #
############################# #############################
# # # #
# NO PARKING # # NO PARKING #
# # # #
# Mon, Wed, Fri 8am-11am # # Tu, Th 11:30am-2:00pm #
# # # #
# # # #
# Violators will be towed! # # Violaters will be towed! #
# # # #
############################# #############################
# # # #
# # # #
# # # #

North side of the street South side of the street
</pre>

We'll start by creating temporal expressions which describe the verboten parking times:
We'll start by creating temporal expressions which describe the verboten parking times:

```ruby
north_expr = (DIWeek.new(Mon) | DIWeek.new(Wed) | DIWeek.new(Fri)) & REDay.new(8,00,11,00)
north_expr = (DIWeek.new(Mon) | DIWeek.new(Wed) | DIWeek.new(Fri)) & REDay.new(8,00,11,00)

south_expr = (DIWeek.new(Tue) | DIWeek.new(Thu)) & REDay.new(11,30,14,00)
south_expr = (DIWeek.new(Tue) | DIWeek.new(Thu)) & REDay.new(11,30,14,00)
```

What we need at this point is a way to write queries against these expressions to determine whether we need to send a reminder. For this purpose, we can use a Schedule and an associated Event, both of which are supplied by Runt.
Expand Down Expand Up @@ -76,7 +76,7 @@ class Schedule
...
```

Now that we have a Schedule configured, we need something to check it and then let us know if we need to move the car. For this, we'll create a simple class called Reminder which will function as the "main-able" part of our app. We'll start by creating an easily testable constructor which will be passed a Schedule instance (like the one we just created) and an SMTP server.
Now that we have a Schedule configured, we need something to check it and then let us know if we need to move the car. For this, we'll create a simple class called Reminder which will function as the "main-able" part of our app. We'll start by creating an easily testable constructor which will be passed a Schedule instance (like the one we just created) and an SMTP server.

```ruby
class Reminder
Expand All @@ -100,7 +100,7 @@ class ReminderTest < Test::Unit::TestCase
def setup
@schedule = Schedule.new
@north_event = Event.new("north side of the street will be ticketed")
north_expr = (DIWeek.new(Mon) | DIWeek.new(Wed) | DIWeek.new(Fri)) & REDay.new(8,00,11,00)
north_expr = (DIWeek.new(Mon) | DIWeek.new(Wed) | DIWeek.new(Fri)) & REDay.new(8,00,11,00)
@schedule.add(@north_event, north_expr)
@south_event = Event.new("south side of the street will be ticketed")
south_expr = (DIWeek.new(Tue) | DIWeek.new(Thu)) & REDay.new(11,30,14,00)
Expand Down Expand Up @@ -128,7 +128,7 @@ class MailServer

def send(to, from, subject, text)
Struct::Email.new(to, from, subject, text)
# etc...
# etc...
end

end
Expand Down Expand Up @@ -174,7 +174,7 @@ class Reminder
...
```

Testing this is simple thanks to our MailServer stub which simply regurgitates the text argument it's passed as a result.
Testing this is simple thanks to our MailServer stub which simply regurgitates the text argument it's passed as a result.

```ruby
class ReminderTest < Test::Unit::TestCase
Expand Down Expand Up @@ -227,7 +227,7 @@ north_event = Event.new("north side")
north_expr = (DIWeek.new(Mon) | DIWeek.new(Wed) | DIWeek.new(Fri)) & REDay.new(8,00,11,00)
schedule.add(north_event, north_expr)
south_event = Event.new("south side")
south_expr = (DIWeek.new(Tue) | DIWeek.new(Thu)) & REDay.new(11,30,14,00)
south_expr = (DIWeek.new(Tue) | DIWeek.new(Thu)) & REDay.new(11,30,14,00)
schedule.add(south_event, south_expr)
reminder = Reminder.new(schedule, MailServer.new)
while true
Expand Down Expand Up @@ -278,7 +278,7 @@ class MailServer
def send(to, from, subject, text)
puts "Sending message TO: #{to} FROM: #{from} RE: #{subject}..." if $DEBUG
Struct::Email.new(to, from, subject, text)
# etc...
# etc...
end

end
Expand All @@ -292,7 +292,7 @@ if __FILE__ == $0
north_expr = (DIWeek.new(Mon) | DIWeek.new(Wed) | DIWeek.new(Fri)) & REDay.new(8,00,11,00)
schedule.add(north_event, north_expr)
south_event = Event.new("south side")
south_expr = (DIWeek.new(Tue) | DIWeek.new(Thu)) & REDay.new(11,30,14,00)
south_expr = (DIWeek.new(Tue) | DIWeek.new(Thu)) & REDay.new(11,30,14,00)
schedule.add(south_event, south_expr)
reminder = Reminder.new(schedule, MailServer.new)
while true
Expand All @@ -315,7 +315,7 @@ class ReminderTest < Test::Unit::TestCase
def setup
@schedule = Schedule.new
@north_event = Event.new("north side of the street will be ticketed")
north_expr = (DIWeek.new(Mon) | DIWeek.new(Wed) | DIWeek.new(Fri)) & REDay.new(8,00,11,00)
north_expr = (DIWeek.new(Mon) | DIWeek.new(Wed) | DIWeek.new(Fri)) & REDay.new(8,00,11,00)
@schedule.add(@north_event, north_expr)
@south_event = Event.new("south side of the street will be ticketed")
south_expr = (DIWeek.new(Tue) | DIWeek.new(Thu)) & REDay.new(11,30,14,00)
Expand Down
6 changes: 3 additions & 3 deletions doc/tutorial_sugar.md
Expand Up @@ -46,7 +46,7 @@ Here's a quick summary of patterns and the expressions they create.

**example**: `monthly_2nd_to_24th`

**action**: `REMonth.new(2,24)`
**action**: `REMonth.new(2,24)`


### REYear
Expand Down Expand Up @@ -100,7 +100,7 @@ d = ExpressionBuilder.new

# Call define with a block
expression = d.define do
on REDay.new(8,45,9,30)
on REDay.new(8,45,9,30)
on DIWeek.new(Friday) # "And"
possibly DIWeek.new(Saturday) # "Or"
except DIMonth.new(Last, Friday) # "Not"
Expand Down Expand Up @@ -149,7 +149,7 @@ ExpressionBuilder creates expressions by evaluating a block passed to the `:defi

**:occurs** alias for `:on` method

**:maybe** alias for `:possibly` method
**:maybe** alias for `:possibly` method


Of course it's easy to open the builder class and add you own aliases if the ones provided don't work for you:
Expand Down
6 changes: 3 additions & 3 deletions doc/tutorial_te.md
Expand Up @@ -35,7 +35,7 @@ expr.include?(Date.new(2004,3,18)) #Thurs 3/18/04 => false
expr.include?(Date.new(2004,8,27)) #Fri 8/27/04 => false
```

A couple things are worth noting before we move on to more complicated expressions.
A couple things are worth noting before we move on to more complicated expressions.

Clients use temporal expressions by creating specific instances (`DIMonth` == day in month, `REYear` == range each year) and then, optionally, combining them using various familiar operators `( & , | , - )`.

Expand Down Expand Up @@ -82,7 +82,7 @@ mon_wed_fri.include?( DateTime.new(2004,3,10,19,15) ) # Wed => true
mon_wed_fri.include?( DateTime.new(2004,3,14,9,00) ) # Sun => false
```

8am to 11am:
8am to 11am:

```ruby
eight_to_eleven = REDay.new(8,00,11,00)
Expand All @@ -93,7 +93,7 @@ combine the two:
expr1 = mon_wed_fri & eight_to_eleven
```

and, logically speaking, we now have '(Mon **OR** Wed **OR** Fri) **AND** (8am to 11am)'. We're halfway there.
and, logically speaking, we now have '(Mon **OR** Wed **OR** Fri) **AND** (8am to 11am)'. We're halfway there.

Tuesdays and Thursdays:

Expand Down
118 changes: 59 additions & 59 deletions examples/payment_report.rb
@@ -1,59 +1,59 @@
#!/usr/bin/ruby

require 'runt'

class Report
attr_reader :schedule

def initialize(schedule)
@schedule = schedule
end
def list(range)
result = {}
range.each do |dt|
events = @schedule.events(dt)
result[dt]=events unless events.empty?
end
result
end
end

class Payment < Runt::Event
attr_accessor :amount
def initialize(id, amount)
super(id)
@amount = amount
end
end


if __FILE__ == $0

include Runt

schedule = Schedule.new

# Gas payment on the first Wednesday of every month
gas_payment = Payment.new("Gas", 234)
gas_expr = DIMonth.new(First, Wednesday)
schedule.add(gas_payment, gas_expr)

# Insurance payment every year on January 7th
insurance_payment = Payment.new("Insurance", 345)
insurance_expr = REYear.new(1, 7, 1, 7)
schedule.add(insurance_payment, insurance_expr)
# Run a report
report = Report.new(schedule)
result = report.list(PDate.day(2008, 1, 1)..PDate.day(2008,1,31))
result.keys.sort.each do |dt|
unless result[dt].empty? then
print "#{dt.ctime} - "
result[dt].each do |event|
puts "#{event.id}, $#{event.amount}"
end
end
end

end
#!/usr/bin/ruby

require 'runt'

class Report

attr_reader :schedule

def initialize(schedule)
@schedule = schedule
end
def list(range)
result = {}
range.each do |dt|
events = @schedule.events(dt)
result[dt]=events unless events.empty?
end
result
end
end

class Payment < Runt::Event
attr_accessor :amount
def initialize(id, amount)
super(id)
@amount = amount
end
end


if __FILE__ == $0

include Runt

schedule = Schedule.new

# Gas payment on the first Wednesday of every month
gas_payment = Payment.new("Gas", 234)
gas_expr = DIMonth.new(First, Wednesday)
schedule.add(gas_payment, gas_expr)

# Insurance payment every year on January 7th
insurance_payment = Payment.new("Insurance", 345)
insurance_expr = REYear.new(1, 7, 1, 7)
schedule.add(insurance_payment, insurance_expr)

# Run a report
report = Report.new(schedule)
result = report.list(PDate.day(2008, 1, 1)..PDate.day(2008,1,31))
result.keys.sort.each do |dt|
unless result[dt].empty? then
print "#{dt.ctime} - "
result[dt].each do |event|
puts "#{event.id}, $#{event.amount}"
end
end
end

end

0 comments on commit 7ab3887

Please sign in to comment.