rubyscriptwriter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,27 @@
1
+ = RubyScriptWriter
2
+
3
+ RubyScriptWriter helps programatically create new ruby scripts:
4
+
5
+ r = RubyScriptWriter.new
6
+ r.put_class "One" do
7
+ r.put_method "a", "arg1","arg2" do
8
+ r.puts "return arg1 + arg2"
9
+ end
10
+ end
11
+ puts r
12
+
13
+ class One
14
+ def a(arg1,arg2)
15
+ return arg1 + arg2
16
+ end
17
+
18
+ end
19
+
20
+ It can be installed as a gem:
21
+ gem install rubyscriptwriter
22
+
23
+ This software is (c) 2010 Green on Black Ltd and distributed under the open source MIT[http://www.opensource.org/licenses/mit-license.php] licence. (See LICENCE for the wording).
24
+
25
+ If you like this code, employ us: http://www.greenonblack.com
26
+
27
+
@@ -0,0 +1,70 @@
1
+ require 'stringio'
2
+
3
+ class RubyScriptWriter
4
+ attr_reader :r
5
+
6
+ def initialize
7
+ @r = StringIO.new
8
+ @tab_count = 0
9
+ end
10
+
11
+ def indent
12
+ @tab_count += 1
13
+ end
14
+
15
+ def outdent
16
+ @tab_count -= 1
17
+ end
18
+
19
+ def put_class(class_name,superclass_name = nil)
20
+ puts "class ", class_name, superclass_name && " < ", superclass_name
21
+ indent
22
+ yield self
23
+ outdent
24
+ puts "end"
25
+ puts
26
+ end
27
+
28
+ def put_simple_method(method_name,*method_code)
29
+ puts "def ", method_name,"; ",*method_code.join,"; end"
30
+ end
31
+
32
+ def put_method(method_name,*arguments)
33
+ if arguments.empty?
34
+ puts "def ", method_name
35
+ else
36
+ puts "def ", method_name, "(", arguments.join(','), ")"
37
+ end
38
+ indent
39
+ yield self
40
+ outdent
41
+ puts "end"
42
+ puts
43
+ end
44
+
45
+ def put_coding(coding = 'utf-8')
46
+ comment 'coding: ', coding
47
+ end
48
+
49
+ def comment(*args)
50
+ puts "# ", *args
51
+ end
52
+
53
+ def puts(*args)
54
+ if args.empty?
55
+ r.puts
56
+ return self
57
+ end
58
+ args.compact!
59
+ r.puts tabs + args.join unless args.empty?
60
+ self
61
+ end
62
+
63
+ def tabs
64
+ " " * @tab_count
65
+ end
66
+
67
+ def to_s
68
+ r.string
69
+ end
70
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubyscriptwriter'
2
+
3
+ class RubySpecWriter < RubyScriptWriter
4
+ def put_description(thing_being_described)
5
+ puts "describe ", thing_being_described, " do"
6
+ indent
7
+ yield self
8
+ outdent
9
+ puts "end"
10
+ puts
11
+ end
12
+
13
+ def put_spec(specficiation_in_words)
14
+ puts "it '",specficiation_in_words.tr("'",""), "' do"
15
+ indent
16
+ yield self
17
+ outdent
18
+ puts "end"
19
+ puts
20
+ end
21
+ end
@@ -0,0 +1,100 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
2
+ require 'rubyscriptwriter'
3
+
4
+ describe RubyScriptWriter do
5
+
6
+ it "#initialize should create an empty string by default" do
7
+ r = RubyScriptWriter.new
8
+ r.to_s.should == ""
9
+ end
10
+
11
+ it "#puts should allow any text to be put, with newlines added automatically" do
12
+ r = RubyScriptWriter.new
13
+ r.puts "# Hello world"
14
+ r.puts "# again"
15
+ r.to_s.should == "# Hello world\n# again\n"
16
+ end
17
+
18
+ it "#puts doesn't put anything if a nil value is supplied" do
19
+ r = RubyScriptWriter.new
20
+ r.puts nil
21
+ r.to_s.should == ""
22
+ end
23
+
24
+ it "#puts puts a new line if no argument is supplied" do
25
+ r = RubyScriptWriter.new
26
+ r.puts
27
+ r.to_s.should == "\n"
28
+ end
29
+
30
+ it "#puts should handle several arguments, concatenating them onto the same line" do
31
+ r = RubyScriptWriter.new
32
+ r.puts "one", " ", 2, 3.0
33
+ r.to_s.should == "one 23.0\n"
34
+ end
35
+
36
+ it "#indent should add two spaces to the start a line for every time indent is called" do
37
+ r = RubyScriptWriter.new
38
+ r.puts "no indent"
39
+ r.indent
40
+ r.puts "one indent"
41
+ r.indent
42
+ r.puts "two indents"
43
+ r.to_s.should == "no indent\n one indent\n two indents\n"
44
+ end
45
+
46
+ it "#outdent should reverse the effect of an indent every time outdent is called" do
47
+ r = RubyScriptWriter.new
48
+ r.puts "no indent"
49
+ r.indent
50
+ r.outdent
51
+ r.puts "still no indent"
52
+ r.indent
53
+ r.indent
54
+ r.outdent
55
+ r.puts "one indent"
56
+ r.to_s.should == "no indent\nstill no indent\n one indent\n"
57
+ end
58
+
59
+ it "#comment adds a # to the start of the given line" do
60
+ r = RubyScriptWriter.new
61
+ r.comment "This is a comment"
62
+ r.to_s.should == "# This is a comment\n"
63
+ end
64
+
65
+ it "#put_class generates classes, optionally with a superclass" do
66
+ r = RubyScriptWriter.new
67
+ r.put_class "One" do
68
+ r.comment "part of class One"
69
+ end
70
+ r.put_class "Two", "One" do
71
+ r.comment "part of class Two"
72
+ end
73
+ r.to_s.should == "class One\n # part of class One\nend\n\nclass Two < One\n # part of class Two\nend\n\n"
74
+ end
75
+
76
+ it "#put_simple_method generates single line methods" do
77
+ r = RubyScriptWriter.new
78
+ r.put_simple_method "a23", "b23 + 10"
79
+ r.to_s.should == "def a23; b23 + 10; end\n"
80
+ end
81
+
82
+ it "#put_method for generating multiple line methods, optionally with arguments" do
83
+ r = RubyScriptWriter.new
84
+ r.put_method "one" do
85
+ r.comment "part of method one"
86
+ end
87
+ r.put_method "two","a","b","c" do
88
+ r.comment "part of method two"
89
+ end
90
+ r.to_s.should == "def one\n # part of method one\nend\n\ndef two(a,b,c)\n # part of method two\nend\n\n"
91
+ end
92
+
93
+ it "#put_coding for generating a ruby 1.9 encoding line at the start. Defaults to utf-8" do
94
+ r = RubyScriptWriter.new
95
+ r.put_coding
96
+ r.put_coding 'ascii'
97
+ r.to_s.should == "# coding: utf-8\n# coding: ascii\n"
98
+ end
99
+
100
+ end
@@ -0,0 +1,30 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
2
+ require 'rubyspecwriter'
3
+
4
+ describe RubySpecWriter do
5
+
6
+ it "#put_description for generating rspec describe statements" do
7
+ r = RubySpecWriter.new
8
+ r.put_description "Sheet1" do
9
+ r.puts "Hello"
10
+ end
11
+ r.to_s.should == "describe Sheet1 do\n Hello\nend\n\n"
12
+ end
13
+
14
+ it "#put_spec for generating rspec it should type statements" do
15
+ r = RubySpecWriter.new
16
+ r.put_spec "a1 should be equal to b2" do
17
+ r.puts "a1.should == b2"
18
+ end
19
+ r.to_s.should == "it 'a1 should be equal to b2' do\n a1.should == b2\nend\n\n"
20
+ end
21
+
22
+ it "#put_spec for generating rspec it should type statements should deal with single quotes" do
23
+ r = RubySpecWriter.new
24
+ r.put_spec %Q{a1 should be equal to "One's own"} do
25
+ r.puts %Q{a1.should == "One's own"}
26
+ end
27
+ r.to_s.should == %Q{it 'a1 should be equal to "Ones own"' do\n a1.should == "One's own"\nend\n\n}
28
+ end
29
+
30
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubyscriptwriter
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Thomas Counsell, Green on Black Ltd
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-25 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: ruby-script-writer@greenonblack.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - README
31
+ - spec/rubyscriptwriter_spec.rb
32
+ - spec/rubyspecwriter_spec.rb
33
+ - lib/rubyscriptwriter.rb
34
+ - lib/rubyspecwriter.rb
35
+ has_rdoc: true
36
+ homepage: http://wiki.github.com/tamc/ruby-script-writer
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.6
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: A small library for programmatically creating well formatted ruby scripts
65
+ test_files: []
66
+