Class: ExpressionBuilder

Inherits:
Object
  • Object
show all
Includes:
Runt
Defined in:
lib/runt/expressionbuilder.rb

Overview

Convenience class for building temporal expressions in a more human-friendly way. Used in conjunction with shortcuts defined in the sugar.rb file, this allows one to create expressions like the following:

b = ExpressionBuilder.new
expr = b.define do
  occurs daily_8_30am_to_9_45am
  on tuesday
  possibly wednesday
end

This equivalent to:

expr = REDay.new(8,30,9,45) & DIWeek.new(Tuesday) | DIWeek.new(Wednesday)

ExpressionBuilder creates expressions by evaluating a block passed to the :define method. From inside the block, methods :occurs, :on, :every, :possibly, and :maybe can be called with a temporal expression which will be added to a composite expression as follows:

  • :on - creates an “and” (&)

  • :possibly - creates an “or” (|)

  • :except - creates a “not” (-)

  • :every - alias for :on method

  • :occurs - alias for :on method

  • :maybe - alias for :possibly method

Constant Summary

Constants included from Runt

Runt::April, Runt::August, Runt::DAYS, Runt::December, Runt::Eighth, Runt::Eigth, Runt::February, Runt::Fifth, Runt::First, Runt::Fourth, Runt::Fri, Runt::Friday, Runt::January, Runt::July, Runt::June, Runt::Last, Runt::Last_of, Runt::MONTHS, Runt::March, Runt::May, Runt::Mon, Runt::Monday, Runt::Ninth, Runt::November, Runt::ORDINAL_ABBR, Runt::ORDINAL_SUFFIX, Runt::October, Runt::Sat, Runt::Saturday, Runt::Second, Runt::Second_to_last, Runt::September, Runt::Seventh, Runt::Sixth, Runt::Sun, Runt::Sunday, Runt::Tenth, Runt::Third, Runt::Thu, Runt::Thursday, Runt::Tue, Runt::Tuesday, Runt::VERSION, Runt::WEEK_OF_MONTH_ORDINALS, Runt::Wed, Runt::Wednesday

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Runt

#build, const, day_name, format_date, format_time, #method_missing, month_name, ordinalize, #parse_time

Constructor Details

#initializeExpressionBuilder

Returns a new instance of ExpressionBuilder.



36
37
38
# File 'lib/runt/expressionbuilder.rb', line 36

def initialize
  @ctx = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Runt

Instance Attribute Details

#ctxObject

Returns the value of attribute ctx.



34
35
36
# File 'lib/runt/expressionbuilder.rb', line 34

def ctx
  @ctx
end

Instance Method Details

#add(expr, op) ⇒ Object



48
49
50
51
52
# File 'lib/runt/expressionbuilder.rb', line 48

def add(expr, op)
  @ctx ||= expr
  @ctx = @ctx.send(op, expr) unless @ctx == expr
  @ctx # explicit return, previous line may not execute
end

#define(&block) ⇒ Object



40
41
42
# File 'lib/runt/expressionbuilder.rb', line 40

def define(&block)
  instance_eval(&block)
end

#except(expr) ⇒ Object



54
55
56
# File 'lib/runt/expressionbuilder.rb', line 54

def except(expr)
  add(expr, :-)
end

#on(expr) ⇒ Object Also known as: every, occurs



44
45
46
# File 'lib/runt/expressionbuilder.rb', line 44

def on(expr)
  add(expr, :&)
end

#possibly(expr) ⇒ Object Also known as: maybe



58
59
60
# File 'lib/runt/expressionbuilder.rb', line 58

def possibly(expr)
  add(expr, :|)
end