-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ext/psych/lib/psych/handlers/recorder.rb: added a class for
recording YAML parse and emit events. * ext/psych/lib/psych/handler.rb: adding a list of events so that handler classes can more easily be meta-programmed. * test/psych/handlers/test_recorder.rb: tests for the change.
- Loading branch information
1 parent
1f4812f
commit c37649b
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'psych/handler' | ||
|
||
module Psych | ||
module Handlers | ||
### | ||
# This handler will capture an event and record the event. Recorder events | ||
# are available vial Psych::Handlers::Recorder#events. | ||
# | ||
# For example: | ||
# | ||
# recorder = Psych::Handlers::Recorder.new | ||
# parser = Psych::Parser.new recorder | ||
# parser.parse '--- foo' | ||
# | ||
# recorder.events # => [list of events] | ||
# | ||
# # Replay the events | ||
# | ||
# emitter = Psych::Emitter.new $stdout | ||
# recorder.events.each do |m, args| | ||
# emitter.send m, *args | ||
# end | ||
|
||
class Recorder < Psych::Handler | ||
attr_reader :events | ||
|
||
def initialize | ||
@events = [] | ||
super | ||
end | ||
|
||
EVENTS.each do |event| | ||
define_method event do |*args| | ||
@events << [event, args] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'psych/helper' | ||
require 'psych/handlers/recorder' | ||
|
||
module Psych | ||
module Handlers | ||
class TestRecorder < TestCase | ||
def test_replay | ||
yaml = "--- foo\n...\n" | ||
output = StringIO.new | ||
|
||
recorder = Psych::Handlers::Recorder.new | ||
parser = Psych::Parser.new recorder | ||
parser.parse yaml | ||
|
||
assert_equal 5, recorder.events.length | ||
|
||
emitter = Psych::Emitter.new output | ||
recorder.events.each do |m, args| | ||
emitter.send m, *args | ||
end | ||
assert_equal yaml, output.string | ||
end | ||
end | ||
end | ||
end |