Module: Eventually::ClassMethods

Defined in:
lib/eventually.rb

Constant Summary collapse

NO_CHECK_ARITY =
-2
DEFAULT_MAX_LISTENERS =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#emittable_eventsObject

Returns the value of attribute emittable_events.



25
26
27
# File 'lib/eventually.rb', line 25

def emittable_events
  @emittable_events
end

Instance Method Details

#arity_for_event(evt) ⇒ Object

Returns the arity validation, nil if event isn't defined



96
97
98
# File 'lib/eventually.rb', line 96

def arity_for_event(evt)
  emits?(evt) ? emittable[evt.to_sym] : nil
end

#disable_strict!Object

The default state of an Eventually instance. Does not require pre-definition of an event to register against it or emit it



81
82
83
# File 'lib/eventually.rb', line 81

def disable_strict!
  @strict = false
end

#emits(*evts) ⇒ Object

Define an event or list of events that instances of this class will potentially emit.

Usage (list of events): emits :started, :stopped

Usage (single event): emits :started

Usage (single event with arity validation): emits :started, :arity => 2



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/eventually.rb', line 39

def emits(*evts)
  if evts && !evts.empty?
    if evts.all?{|e| e.is_a?(Symbol) }
      evts.each{|e| emittable[e.to_sym] = NO_CHECK_ARITY }
    elsif evts.count == 2
      emittable[evts[0].to_sym] = (evts[1].fetch(:arity) { NO_CHECK_ARITY }).to_i
    end
  else
    emittable.keys
  end
end

#emits?(evt) ⇒ Boolean

Check if instances of this class have pre-defined the given event as emittable

Returns:

  • (Boolean)


53
54
55
# File 'lib/eventually.rb', line 53

def emits?(evt)
  emittable.key?(evt.to_sym) rescue false
end

#emits_noneObject

Reset the known emittable events (events defined with #emits) More useful for tests probably, but leaving it in API just 'cause



102
103
104
105
# File 'lib/eventually.rb', line 102

def emits_none
  @emittable = {}
  nil
end

#emittable?(event) ⇒ Boolean Also known as: registerable?

Shorthand predicate to determine if a given event is "emittable" or "registerable"

Returns:

  • (Boolean)


109
110
111
# File 'lib/eventually.rb', line 109

def emittable?(event)
  !strict? || emits?(event)
end

#enable_strict!Object

Puts instances into strict mode. This does two things:

  • Raise an error if registering a callback for an event that has not already been pre-defined (e.g. with #emits)
  • Raise an error if instance attempts to emit an event that has not already been pre-defined (e.g. with #emits)


75
76
77
# File 'lib/eventually.rb', line 75

def enable_strict!
  @strict = true
end

#max_listenersObject

Return the maximum number of listeners before we'll start printing memory warnings. Default max is 10



60
61
62
# File 'lib/eventually.rb', line 60

def max_listeners
  @max_listeners ||= DEFAULT_MAX_LISTENERS
end

#max_listeners=(max) ⇒ Object

Set the maximum listener number. Setting this max to 0 indicates unlimited listeners allowed.



66
67
68
# File 'lib/eventually.rb', line 66

def max_listeners= max
  @max_listeners = max
end

#strict?Boolean

Report on strict mode

Returns:

  • (Boolean)


86
87
88
# File 'lib/eventually.rb', line 86

def strict?
  @strict || false
end

#validates_arity?(evt) ⇒ Boolean

Determines if the given event has an arity validation assigned

Returns:

  • (Boolean)


91
92
93
# File 'lib/eventually.rb', line 91

def validates_arity?(evt)
  emits?(evt) && emittable[evt.to_sym] > NO_CHECK_ARITY
end