Class: ProjectSimulator::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/projectsimulator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(e, time: nil, debug: false) ⇒ Event

Returns a new instance of Event.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/projectsimulator.rb', line 137

def initialize(e, time: nil, debug: false)    

  @time, @debug = time, debug
  @title = e.text('event')
  @actions = []
  @triggers = []
  @constraints = []

  e.xpath('trigger/text()').each do |x| 
    @triggers << Trigger.new(x, time: time, debug: debug).to_type
  end

  e.xpath('action/text()').each do |x|
    @actions << Action.new(x, debug: debug).to_type
  end

  e.xpath('constraint/text()').each do |x|
    puts 'before Constraints.new'
    @constraints << Constraint.new(x, time: time, debug: debug).to_type
  end

end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



135
136
137
# File 'lib/projectsimulator.rb', line 135

def actions
  @actions
end

#constraintsObject (readonly)

Returns the value of attribute constraints.



135
136
137
# File 'lib/projectsimulator.rb', line 135

def constraints
  @constraints
end

#messagesObject (readonly)

Returns the value of attribute messages.



135
136
137
# File 'lib/projectsimulator.rb', line 135

def messages
  @messages
end

#titleObject

Returns the value of attribute title.



134
135
136
# File 'lib/projectsimulator.rb', line 134

def title
  @title
end

#triggersObject (readonly)

Returns the value of attribute triggers.



135
136
137
# File 'lib/projectsimulator.rb', line 135

def triggers
  @triggers
end

Instance Method Details

#match(trigger: nil, location: '') ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/projectsimulator.rb', line 160

def match(trigger: nil, location: '')
  
  @messages = []
  
  h = {motion: MotionTrigger}
  
  if @triggers.any? {|x| x.is_a? h[trigger.to_sym] and x.match } then
    
    if @constraints.all?(&:match) then
    
      @messages = @actions.map(&:call)
      
    else
      
      puts 'else reset?' if @debug
      a = @constraints.select {|x| x.is_a? FrequencyConstraint }
      puts 'a:' + a.inspect if @debug
      a.each {|x| x.reset if x.counter > 0 }
      return false
    end
    
  end
  
end

#time=(val) ⇒ Object



185
186
187
188
# File 'lib/projectsimulator.rb', line 185

def time=(val)
  @time = val
  @constraints.each {|x| x.time = val if x.is_a? TimeConstraint }
end