Class: TaskLoop::BetweenScopeRule

Inherits:
ScopeRule show all
Defined in:
lib/taskloop/rules/between_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, left, right) ⇒ BetweenScopeRule

Returns a new instance of BetweenScopeRule.



8
9
10
11
12
# File 'lib/taskloop/rules/between_scope_rule.rb', line 8

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

Instance Attribute Details

#leftObject

Returns the value of attribute left.



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

def left
  @left
end

#rightObject

Returns the value of attribute right.



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

def right
  @right
end

Instance Method Details

#descObject



119
120
121
# File 'lib/taskloop/rules/between_scope_rule.rb', line 119

def desc
  super + " #{left}, #{right}"
end

#is_conform_rule?(last_exec_time) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/taskloop/rules/between_scope_rule.rb', line 97

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

#left_valueObject

def invalidate!

super
if @unit == :day
  if Task::WEEK.has_key?(@left) && Task::WEEK.has_key?(@right)
    unless Task::WEEK[@left] < Task::WEEK[@right]
      raise ArgumentError, "'left' must less than 'right'"
    end
    return
  end
  if Task::DAY.has_key?(@left) && Task::DAY.has_key?(@right)
    unless Task::WEEK[@left] < Task::WEEK[@right]
      raise ArgumentError, "'left' must less than 'right'"
    end
    return
  end

  raise ArgumentError, "'left' and 'right' must be the same type."
end

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

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

unless @left < @right
  raise ArgumentError, "'left' must less than 'right'"
end

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

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

if @unit == :year && @left < 0
  raise ArgumentError, "'left' must greater than 0"
end

end



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

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

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

  return @left
end

#right_valueObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/taskloop/rules/between_scope_rule.rb', line 79

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