Class: TaskLoop::BeforeScopeRule

Inherits:
ScopeRule show all
Defined in:
lib/taskloop/rules/before_scope_rule.rb

Constant Summary

Constants inherited from ScopeRule

ScopeRule::SCOPE

Constants inherited from Rule

Rule::UNIT

Instance Attribute Summary collapse

Attributes inherited from ScopeRule

#scope

Attributes inherited from Rule

#unit

Instance Method Summary collapse

Constructor Details

#initialize(unit, right) ⇒ BeforeScopeRule

Returns a new instance of BeforeScopeRule.



6
7
8
9
# File 'lib/taskloop/rules/before_scope_rule.rb', line 6

def initialize(unit, right)
  super unit, :before
  @right = right
end

Instance Attribute Details

#rightObject

Returns the value of attribute right.



4
5
6
# File 'lib/taskloop/rules/before_scope_rule.rb', line 4

def right
  @right
end

Instance Method Details

#descObject



79
80
81
# File 'lib/taskloop/rules/before_scope_rule.rb', line 79

def desc
  super + " #{right}"
end

#is_conform_rule?(last_exec_time) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/taskloop/rules/before_scope_rule.rb', line 58

def is_conform_rule?(last_exec_time)
  current = Time.now
  value = right_value
  result = false
  case @unit
  when :year then
    result = current.year <= value
  when :month then
    result = current.month <= value
  when :week then
    result = current.wday <= value
  when :day then
    result = current.day <= value
  when :hour then
    result = current.hour <= value
  when :minute then
    result = current.min <= value
  end
  return result
end

#right_valueObject

def invalidate!

super
if @unit == :day
  unless Task::WEEK.has_key?(@right) || Task::DAY.has_key?(@right)
    raise ArgumentError, "#{@right} must be a Symbol defined in Task::WEEK or Task::DAY"
  end
  return
end

if @unit == :month
  unless Task::MONTH.has_key?(@right)
    raise ArgumentError, "#{@right} must be a Symbol defined in Task::MONTH"
  end
  return
end

unless @right.is_a?(Integer)
  raise TypeError, "'right' need to be Symbol or Integer"
end

if @unit == :minute && (@right < 0 || @right > 59)
  raise ArgumentError, "'right' for 'minute' must >= 0 and <= 59"
end

if @unit == :hour && (@right < 0 || @right > 23)
  raise ArgumentError, "'right' for 'hour' must >= 0 and <= 23"
end

end



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/taskloop/rules/before_scope_rule.rb', line 40

def right_value
  if (Task::DAY.has_key?(@right))
    return Task::DAY[@right]
  end
  if (Task::WEEK.has_key?(@right))
    return Task::WEEK[@right]
  end
  if (Task::MONTH.has_key?(@right))
    return Task::MONTH[@right]
  end

  unless @right != nil && @right.is_a?(Integer)
    return -1
  end

  return @right
end