Module: Riaction::EventInstanceMethods

Defined in:
lib/riaction/riaction.rb

Instance Method Summary collapse

Instance Method Details

#riaction_event(event_name) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/riaction/riaction.rb', line 194

def riaction_event(event_name)
  events = self.class.class_variable_defined?(:@@riaction_events) ? self.class.class_variable_get(:@@riaction_events) : {}
  event = events.fetch(event_name.to_sym)
  
  profile = riaction_event_profile(event[:profile])
  unless profile.class.respond_to?(:riaction_profile?) && profile.class.riaction_profile?
    raise TypeError.new("Object defined for #{self.class} on event #{event_name} as a profile must itself declare riaction(:profile ...)")
  end
  params = riaction_event_params(event[:params])
  raise TypeError.new("Params defined for #{self.class} on event #{event_name} must be a hash") unless params.kind_of? Hash
  
  {
    :key => event_name.to_sym,
    :profile => profile,
    :params => params
  }
rescue KeyError => e
  raise NoEventDefined.new("no such event #{event_name} defined on #{self.class}")
end

#riaction_log_event?(event_name) ⇒ Boolean

Returns:

  • (Boolean)


214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/riaction/riaction.rb', line 214

def riaction_log_event?(event_name)
  events = self.class.class_variable_defined?(:@@riaction_events) ? self.class.class_variable_get(:@@riaction_events) : {}
  event = events.fetch(event_name.to_sym)
  guard = event[:guard]
  
  case guard
  when NilClass
    true
  when Symbol
    self.send guard
  when Proc
    guard.call self
  else 
    true
  end
rescue KeyError => e
  raise NoEventDefined.new("no such event #{event_name} defined on #{self.class}")
end