Class: Pask

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

Defined Under Namespace

Classes: Event

Constant Summary collapse

NotStartedError =
Class.new(RuntimeError)
NotFinishedError =
Class.new(RuntimeError)
VERSION =
"0.1.0".freeze
RUN_STATE =
"run".freeze
SLEEP_STATE =
"sleep".freeze
TERMINATED_STATE =
[nil, false].freeze
CATCH_ALL =
Proc.new { true }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePask

Returns a new instance of Pask.



18
19
20
21
22
23
24
25
# File 'lib/pask.rb', line 18

def initialize
  @thread = nil
  @queue = nil
  @predicate = CATCH_ALL
  @predicate_copy = nil
  @block = nil
  @block_copy = nil
end

Class Method Details

.versionObject



14
15
16
# File 'lib/pask.rb', line 14

def self.version
  VERSION
end

Instance Method Details

#consume!Object



96
97
98
99
100
101
# File 'lib/pask.rb', line 96

def consume!
  events = [start]
  current_event = nil
  events.push(current_event) while current_event = resume
  events
end

#finished?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
# File 'lib/pask.rb', line 51

def finished?
  if @thread and TERMINATED_STATE.include?(@thread.status)
    true
  else
    false
  end
end

#interested?(callable = nil, &block) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/pask.rb', line 27

def interested?(callable = nil, &block)
  predicate = callable || block
  unless predicate
    raise ArgumentError, "no predicate given to #{__method__}()"
  end
  @predicate = predicate
end

#resumeObject



67
68
69
70
71
72
73
74
75
# File 'lib/pask.rb', line 67

def resume
  unless started?
    raise NotStartedError, "tracer has not been started"
  end
  if sleeping?
    @thread.wakeup
    @queue.deq
  end
end

#running?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
# File 'lib/pask.rb', line 43

def running?
  if @thread and @thread.status == RUN_STATE
    true
  else
    false
  end
end

#sleeping?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
# File 'lib/pask.rb', line 59

def sleeping?
  if @thread and @thread.status == SLEEP_STATE
    true
  else
    false
  end
end

#startObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pask.rb', line 77

def start
  if started? and not finished?
    raise NotFinishedError, "tracer has not finished"
  end
  @queue = Queue.new
  @thread = Thread.new do
    @predicate_copy, @block_copy = @predicate, @block
    Thread.current.set_trace_func method(:on_event).to_proc
    @block_copy.call
    Thread.current.set_trace_func(nil)
    @queue.enq nil
  end
  @queue.deq
end

#started?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/pask.rb', line 35

def started?
  if @queue and @thread
    true
  else
    false
  end
end

#trace(&block) ⇒ Object



92
93
94
# File 'lib/pask.rb', line 92

def trace(&block)
  @block = block
end