command_recorder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,43 @@
1
+
2
+ # -*- encoding: utf-8 -*-
3
+ $:.push('lib')
4
+ require "command_recorder/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "command_recorder"
8
+ s.version = CommandRecorder::VERSION.dup
9
+ s.date = "2012-01-06"
10
+ s.summary = "let's you record actions on a stub and play them back on a real object later (or on a different system)"
11
+ s.email = "tim@galeckas.com"
12
+ s.homepage = "http://todo.project.com/"
13
+ s.authors = ['Tim Galeckas']
14
+
15
+ s.description = <<-EOF
16
+ let's you record actions on a stub and play them back on a real object later (or on a different system)
17
+ EOF
18
+
19
+ dependencies = [
20
+ # Examples:
21
+ [:runtime, "json", "~> 1.6.4"],
22
+ [:development, "hashie", "~> 1.2.0"],
23
+ ]
24
+
25
+ s.files = Dir['**/*']
26
+ s.test_files = Dir['test/**/*'] + Dir['spec/**/*']
27
+ s.executables = Dir['bin/*'].map { |f| File.basename(f) }
28
+ s.require_paths = ["lib"]
29
+
30
+
31
+ ## Make sure you can build the gem on older versions of RubyGems too:
32
+ s.rubygems_version = "1.6.2"
33
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
34
+ s.specification_version = 3 if s.respond_to? :specification_version
35
+
36
+ dependencies.each do |type, name, version|
37
+ if s.respond_to?("add_#{type}_dependency")
38
+ s.send("add_#{type}_dependency", name, version)
39
+ else
40
+ s.add_dependency(name, version)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,75 @@
1
+ require 'rubygems'
2
+ require 'json'
3
+
4
+ class CommandRecorder
5
+ def self.record(&block)
6
+ command_recorder = CommandRecorder.new
7
+ command_recorder.store(&block)
8
+ command_recorder
9
+ end
10
+
11
+ def initialize(parent=nil,label='__cr__')
12
+ self.parent=parent
13
+ @label=label
14
+ end
15
+
16
+ def store(&block)
17
+ block.call(self)
18
+ end
19
+
20
+ def method_missing(*args)
21
+ case(args[0])
22
+ when :to_ary
23
+ super
24
+ else
25
+ returning_command_recorder = CommandRecorder.new(self,args[0].to_s)
26
+ store_invocation(self, returning_command_recorder, args)
27
+ returning_command_recorder
28
+ end
29
+ end
30
+
31
+ def store_invocation(recorder, returning_command_recorder, method_call)
32
+ if self.parent
33
+ self.parent.store_invocation(recorder, returning_command_recorder, method_call)
34
+ else
35
+ self.invocations ||= []
36
+ self.invocations << [recorder, returning_command_recorder, method_call]
37
+ end
38
+ end
39
+
40
+ def label
41
+ "#{parent && "#{parent.label}|"}#{@label}"
42
+ end
43
+
44
+ def to_str
45
+ @label
46
+ end
47
+
48
+ def to_json(*args)
49
+ self.label.to_json
50
+ end
51
+
52
+ def invocation_json
53
+ root.do_json
54
+ end
55
+
56
+ def self.playback_from_json(json,object)
57
+ invocations = JSON.parse(json)
58
+ objects = {'__cr__'=>object}
59
+ invocations.each do |recorder, returning_recorder, method_call|
60
+ objects[returning_recorder] = objects[recorder].send(*method_call)
61
+ end
62
+ end
63
+
64
+ protected
65
+
66
+ attr_accessor :invocations, :parent
67
+
68
+ def root
69
+ self.parent ? self.parent.root : self
70
+ end
71
+
72
+ def do_json
73
+ invocations.to_json
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module CommandRecorder
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'command_recorder'
3
+
4
+ require 'ruby-debug'
5
+ require 'hashie'
6
+
7
+ proc = Proc.new do
8
+ Hashie::Mash.new &proc
9
+ end
10
+ test_object = Hashie::Mash.new &proc
11
+
12
+ cr = CommandRecorder.record do |hello|
13
+ hello.something.something
14
+ end
15
+
16
+ CommandRecorder.playback_from_json(cr.invocation_json,test_object)
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: command_recorder
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Tim Galeckas
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-01-06 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: json
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 1
32
+ - 6
33
+ - 4
34
+ version: 1.6.4
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: hashie
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 31
46
+ segments:
47
+ - 1
48
+ - 2
49
+ - 0
50
+ version: 1.2.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: " let's you record actions on a stub and play them back on a real object later (or on a different system)\n"
54
+ email: tim@galeckas.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files: []
60
+
61
+ files:
62
+ - command_recorder.gemspec
63
+ - lib/command_recorder/version.rb
64
+ - lib/command_recorder.rb
65
+ - test/command_recorder_test.rb
66
+ has_rdoc: true
67
+ homepage: http://todo.project.com/
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options: []
72
+
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.6.2
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: let's you record actions on a stub and play them back on a real object later (or on a different system)
100
+ test_files:
101
+ - test/command_recorder_test.rb