indentation 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ === 0.0.1 2010-05-03
2
+
3
+ * Initial release
@@ -0,0 +1,20 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ features/development.feature
7
+ features/step_definitions/common_steps.rb
8
+ features/support/common.rb
9
+ features/support/env.rb
10
+ features/support/matchers.rb
11
+ lib/indentation.rb
12
+ lib/indentation/array_mod.rb
13
+ lib/indentation/string_mod.rb
14
+ script/console
15
+ script/destroy
16
+ script/generate
17
+ spec/indentation_spec.rb
18
+ spec/spec.opts
19
+ spec/spec_helper.rb
20
+ tasks/rspec.rake
@@ -0,0 +1,5 @@
1
+ -------------------------------------------------------------------------------
2
+ Thanks for installing the indentation gem! :)
3
+
4
+ For more information on indentation, see http://indentation.rubyforge.org
5
+ -------------------------------------------------------------------------------
@@ -0,0 +1,131 @@
1
+ = Indentation
2
+
3
+ * http://github.com/samueldana/indentation
4
+
5
+ == DESCRIPTION:
6
+
7
+ A small library of extensions to Ruby's Array and String classes that allow indentation manipulation of Strings and Arrays of Strings. Has the capability of working with multi-line strings. If you frequently use String arrays to manipulate text, see synopsis below for examples of how indentation can make your life easier.
8
+
9
+ == SYNOPSIS:
10
+
11
+ === Indent
12
+
13
+ # Default indentation is 2 spaces
14
+ "test".indent # => " test"
15
+
16
+ # Amount of indentation can be changed
17
+ "test".indent(3) # => " test"
18
+
19
+ # Indentation character (or string) is set as the second parameter of indent.
20
+ "test".indent(2, "\t") # => "\t\ttest"
21
+
22
+ # Operates on multi-line strings
23
+ "this\nis\na\ntest".indent # => " this\n is\n a\n test"
24
+
25
+ # Indent method accepts negative values (Removes tabs, spaces, and supplied indentation string)
26
+ " test".indent(-1) # => " test"
27
+ "\t test".indent(-5) # => "test"
28
+ "\t-- Test".indent(-10) # => "-- Test"
29
+ "\t-- Test".indent(-10, '-') # => "Test"
30
+ "--- Test".indent(-2, '--') # => "- Test"
31
+
32
+ # Operates on arrays
33
+ ["one", "two"].indent # => [" one", " two"]
34
+ [["one", " two"], ["uno", "\t\tdos"]].indent # => [[" one", " two"], [" uno", " \t\tdos"]]
35
+
36
+
37
+ === Reset Indentation
38
+
39
+ # 'resets' the indentation of a string by finding the least amount of indentation in the String/Array, and
40
+ # removing that amount from every line.
41
+ " def method_name\n # Do stuff\n end".reset_indentation # => "def method_name\n # Do stuff\nend"
42
+
43
+ # Operates on arrays
44
+ [" def method_name", " # Do stuff", " end"].reset_indentation # => ["def method_name", " # Do stuff", "end"]
45
+
46
+ # Useful for heredocs - must chomp to remove trailing newline
47
+ my_string = <<-EOS.chomp.reset_indentation
48
+ def method_name
49
+ # Do stuff
50
+ end
51
+ EOS
52
+ my_string # => "def method_name\n # Do stuff\nend"
53
+
54
+ # Accepts an indentation modifier
55
+ " one\n two".reset_indentation(1) # => " one\n two"
56
+ " one\n two".reset_indentation(0) # => " one\ntwo" - Default behavior
57
+ " one\n two".reset_indentation(-1) # => "one\ntwo"
58
+
59
+
60
+ === Append Separator
61
+
62
+ # Given an Array of Arrays or an Array of Strings, appends a separator object to all but the last element in the Array.
63
+ # NOTE: For an Array of Strings the separator object must be a String, since it is appended to other Strings
64
+ ["arg1", "arg2", "arg3"].append_separator("!") # => ["arg1!", "arg2!", "arg3"]
65
+ [["line1"], ["line2"], ["line3"]].append_separator("") # => [["line1", ""], ["line2", ""], ["line3"]]
66
+ [["line1", "line2"], ["line3", "line4"]].append_separator("") # => [["line1", "line2", ""], ["line3", "line4"]]
67
+
68
+ # Useful combined with indent and join
69
+ vars = ["var1", "var2", "var3", "var4"]
70
+ method_def = ["def add_up(#{vars.join(', ')})"]
71
+ method_body = vars.append_separator(" + ")
72
+ method_def += method_body.indent
73
+ method_def << "end"
74
+ method_def.join("\n") # =>
75
+ # def add_up(var1, var2, var3, var4)
76
+ # var1 +
77
+ # var2 +
78
+ # var3 +
79
+ # var4
80
+ # end
81
+
82
+ # Handy for separating arrays of string arrays
83
+ test_array = [["this", "is", "a", "test"], ["quick", "brown", "fox"], ["lazy", "typist"]]
84
+ test_array.append_separator("").join("\n") # =>
85
+ # this
86
+ # is
87
+ # a
88
+ # test
89
+ #
90
+ # quick
91
+ # brown
92
+ # fox
93
+ #
94
+ # lazy
95
+ # typist
96
+
97
+ == REQUIREMENTS:
98
+
99
+ * rake (for installation)
100
+
101
+ == INSTALL:
102
+
103
+ * git clone git://github.com/samueldana/indentation.git
104
+ * cd indentation
105
+ * rake install_gem
106
+ * Or if using rvm: rake rvm_install_gem (doesn't use sudo)
107
+
108
+ == LICENSE:
109
+
110
+ (The MIT License)
111
+
112
+ Copyright (c) 2010 Prometheus Computing
113
+
114
+ Permission is hereby granted, free of charge, to any person obtaining
115
+ a copy of this software and associated documentation files (the
116
+ 'Software'), to deal in the Software without restriction, including
117
+ without limitation the rights to use, copy, modify, merge, publish,
118
+ distribute, sublicense, and/or sell copies of the Software, and to
119
+ permit persons to whom the Software is furnished to do so, subject to
120
+ the following conditions:
121
+
122
+ The above copyright notice and this permission notice shall be
123
+ included in all copies or substantial portions of the Software.
124
+
125
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
126
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
127
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
128
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
129
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
130
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
131
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/indentation'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'indentation' do
14
+ self.developer 'Samuel Dana', 's.dana@prometheuscomputing.com'
15
+ self.post_install_message = File.read(File.join(File.dirname(__FILE__),'PostInstall.txt'))
16
+ self.rubyforge_name = self.name # TODO this is default value
17
+ # self.extra_deps = [['activesupport','>= 2.0.2']]
18
+
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+ # TODO - want other tests/tasks run by default? Add them to the list
25
+ # remove_task :default
26
+ # task :default => [:spec, :features]
27
+
28
+ # Add rvm gem install tasks
29
+ desc 'Install the package as a gem for rvm (no sudo), without generating documentation(ri/rdoc)'
30
+ task :rvm_install_gem_no_doc => [:clean, :package] do
31
+ sh "gem install pkg/*.gem --no-rdoc --no-ri"
32
+ end
33
+
34
+ desc 'Shortcut to rvm_install_gem_no_doc task'
35
+ task :rig => [:rvm_install_gem_no_doc] do
36
+ end
37
+
38
+ desc 'Install the package as a gem for rvm (no sudo)'
39
+ task :rvm_install_gem => [:clean, :package] do
40
+ sh "gem install pkg/*.gem"
41
+ end
@@ -0,0 +1,13 @@
1
+ Feature: Development processes of newgem itself (rake tasks)
2
+
3
+ As a Newgem maintainer or contributor
4
+ I want rake tasks to maintain and release the gem
5
+ So that I can spend time on the tests and code, and not excessive time on maintenance processes
6
+
7
+ Scenario: Generate RubyGem
8
+ Given this project is active project folder
9
+ And "pkg" folder is deleted
10
+ When I invoke task "rake gem"
11
+ Then folder "pkg" is created
12
+ And file with name matching "pkg/*.gem" is created else you should run "rake manifest" to fix this
13
+ And gem spec key "rdoc_options" contains /(--mainREADME.rdoc|\[\"--main\", \"README.rdoc\"\])/
@@ -0,0 +1,168 @@
1
+ Given /^this project is active project folder/ do
2
+ @active_project_folder = File.expand_path(File.dirname(__FILE__) + "/../..")
3
+ end
4
+
5
+ Given /^env variable \$([\w_]+) set to "(.*)"/ do |env_var, value|
6
+ ENV[env_var] = value
7
+ end
8
+
9
+ Given /"(.*)" folder is deleted/ do |folder|
10
+ in_project_folder { FileUtils.rm_rf folder }
11
+ end
12
+
13
+ When /^I invoke "(.*)" generator with arguments "(.*)"$/ do |generator, arguments|
14
+ @stdout = StringIO.new
15
+ in_project_folder do
16
+ if Object.const_defined?("APP_ROOT")
17
+ APP_ROOT.replace(FileUtils.pwd)
18
+ else
19
+ APP_ROOT = FileUtils.pwd
20
+ end
21
+ run_generator(generator, arguments.split(' '), SOURCES, :stdout => @stdout)
22
+ end
23
+ File.open(File.join(@tmp_root, "generator.out"), "w") do |f|
24
+ @stdout.rewind
25
+ f << @stdout.read
26
+ end
27
+ end
28
+
29
+ When /^I run executable "(.*)" with arguments "(.*)"/ do |executable, arguments|
30
+ @stdout = File.expand_path(File.join(@tmp_root, "executable.out"))
31
+ in_project_folder do
32
+ system "#{executable} #{arguments} > #{@stdout} 2> #{@stdout}"
33
+ end
34
+ end
35
+
36
+ When /^I run project executable "(.*)" with arguments "(.*)"/ do |executable, arguments|
37
+ @stdout = File.expand_path(File.join(@tmp_root, "executable.out"))
38
+ in_project_folder do
39
+ system "ruby #{executable} #{arguments} > #{@stdout} 2> #{@stdout}"
40
+ end
41
+ end
42
+
43
+ When /^I run local executable "(.*)" with arguments "(.*)"/ do |executable, arguments|
44
+ @stdout = File.expand_path(File.join(@tmp_root, "executable.out"))
45
+ executable = File.expand_path(File.join(File.dirname(__FILE__), "/../../bin", executable))
46
+ in_project_folder do
47
+ system "ruby #{executable} #{arguments} > #{@stdout} 2> #{@stdout}"
48
+ end
49
+ end
50
+
51
+ When /^I invoke task "rake (.*)"/ do |task|
52
+ @stdout = File.expand_path(File.join(@tmp_root, "tests.out"))
53
+ in_project_folder do
54
+ system "rake #{task} --trace > #{@stdout} 2> #{@stdout}"
55
+ end
56
+ end
57
+
58
+ Then /^folder "(.*)" (is|is not) created/ do |folder, is|
59
+ in_project_folder do
60
+ File.exists?(folder).should(is == 'is' ? be_true : be_false)
61
+ end
62
+ end
63
+
64
+ Then /^file "(.*)" (is|is not) created/ do |file, is|
65
+ in_project_folder do
66
+ File.exists?(file).should(is == 'is' ? be_true : be_false)
67
+ end
68
+ end
69
+
70
+ Then /^file with name matching "(.*)" is created/ do |pattern|
71
+ in_project_folder do
72
+ Dir[pattern].should_not be_empty
73
+ end
74
+ end
75
+
76
+ Then /^file "(.*)" contents (does|does not) match \/(.*)\// do |file, does, regex|
77
+ in_project_folder do
78
+ actual_output = File.read(file)
79
+ (does == 'does') ?
80
+ actual_output.should(match(/#{regex}/)) :
81
+ actual_output.should_not(match(/#{regex}/))
82
+ end
83
+ end
84
+
85
+ Then /gem file "(.*)" and generated file "(.*)" should be the same/ do |gem_file, project_file|
86
+ File.exists?(gem_file).should be_true
87
+ File.exists?(project_file).should be_true
88
+ gem_file_contents = File.read(File.dirname(__FILE__) + "/../../#{gem_file}")
89
+ project_file_contents = File.read(File.join(@active_project_folder, project_file))
90
+ project_file_contents.should == gem_file_contents
91
+ end
92
+
93
+ Then /^(does|does not) invoke generator "(.*)"$/ do |does_invoke, generator|
94
+ actual_output = File.read(@stdout)
95
+ does_invoke == "does" ?
96
+ actual_output.should(match(/dependency\s+#{generator}/)) :
97
+ actual_output.should_not(match(/dependency\s+#{generator}/))
98
+ end
99
+
100
+ Then /help options "(.*)" and "(.*)" are displayed/ do |opt1, opt2|
101
+ actual_output = File.read(@stdout)
102
+ actual_output.should match(/#{opt1}/)
103
+ actual_output.should match(/#{opt2}/)
104
+ end
105
+
106
+ Then /^I should see "([^\"]*)"$/ do |text|
107
+ actual_output = File.read(@stdout)
108
+ actual_output.should contain(text)
109
+ end
110
+
111
+ Then /^I should see$/ do |text|
112
+ actual_output = File.read(@stdout)
113
+ actual_output.should contain(text)
114
+ end
115
+
116
+ Then /^I should not see$/ do |text|
117
+ actual_output = File.read(@stdout)
118
+ actual_output.should_not contain(text)
119
+ end
120
+
121
+ Then /^I should see exactly$/ do |text|
122
+ actual_output = File.read(@stdout)
123
+ actual_output.should == text
124
+ end
125
+
126
+ Then /^I should see all (\d+) tests pass/ do |expected_test_count|
127
+ expected = %r{^#{expected_test_count} tests, \d+ assertions, 0 failures, 0 errors}
128
+ actual_output = File.read(@stdout)
129
+ actual_output.should match(expected)
130
+ end
131
+
132
+ Then /^I should see all (\d+) examples pass/ do |expected_test_count|
133
+ expected = %r{^#{expected_test_count} examples?, 0 failures}
134
+ actual_output = File.read(@stdout)
135
+ actual_output.should match(expected)
136
+ end
137
+
138
+ Then /^yaml file "(.*)" contains (\{.*\})/ do |file, yaml|
139
+ in_project_folder do
140
+ yaml = eval yaml
141
+ YAML.load(File.read(file)).should == yaml
142
+ end
143
+ end
144
+
145
+ Then /^Rakefile can display tasks successfully/ do
146
+ @stdout = File.expand_path(File.join(@tmp_root, "rakefile.out"))
147
+ in_project_folder do
148
+ system "rake -T > #{@stdout} 2> #{@stdout}"
149
+ end
150
+ actual_output = File.read(@stdout)
151
+ actual_output.should match(/^rake\s+\w+\s+#\s.*/)
152
+ end
153
+
154
+ Then /^task "rake (.*)" is executed successfully/ do |task|
155
+ @stdout.should_not be_nil
156
+ actual_output = File.read(@stdout)
157
+ actual_output.should_not match(/^Don't know how to build task '#{task}'/)
158
+ actual_output.should_not match(/Error/i)
159
+ end
160
+
161
+ Then /^gem spec key "(.*)" contains \/(.*)\// do |key, regex|
162
+ in_project_folder do
163
+ gem_file = Dir["pkg/*.gem"].first
164
+ gem_spec = Gem::Specification.from_yaml(`gem spec #{gem_file}`)
165
+ spec_value = gem_spec.send(key.to_sym)
166
+ spec_value.to_s.should match(/#{regex}/)
167
+ end
168
+ end
@@ -0,0 +1,29 @@
1
+ module CommonHelpers
2
+ def in_tmp_folder(&block)
3
+ FileUtils.chdir(@tmp_root, &block)
4
+ end
5
+
6
+ def in_project_folder(&block)
7
+ project_folder = @active_project_folder || @tmp_root
8
+ FileUtils.chdir(project_folder, &block)
9
+ end
10
+
11
+ def in_home_folder(&block)
12
+ FileUtils.chdir(@home_path, &block)
13
+ end
14
+
15
+ def force_local_lib_override(project_name = @project_name)
16
+ rakefile = File.read(File.join(project_name, 'Rakefile'))
17
+ File.open(File.join(project_name, 'Rakefile'), "w+") do |f|
18
+ f << "$:.unshift('#{@lib_path}')\n"
19
+ f << rakefile
20
+ end
21
+ end
22
+
23
+ def setup_active_project_folder project_name
24
+ @active_project_folder = File.join(@tmp_root, project_name)
25
+ @project_name = project_name
26
+ end
27
+ end
28
+
29
+ World(CommonHelpers)
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + "/../../lib/indentation"
2
+
3
+ gem 'cucumber'
4
+ require 'cucumber'
5
+ gem 'rspec'
6
+ require 'spec'
7
+
8
+ Before do
9
+ @tmp_root = File.dirname(__FILE__) + "/../../tmp"
10
+ @home_path = File.expand_path(File.join(@tmp_root, "home"))
11
+ @lib_path = File.expand_path(File.dirname(__FILE__) + "/../../lib")
12
+ FileUtils.rm_rf @tmp_root
13
+ FileUtils.mkdir_p @home_path
14
+ ENV['HOME'] = @home_path
15
+ end
@@ -0,0 +1,11 @@
1
+ module Matchers
2
+ def contain(expected)
3
+ simple_matcher("contain #{expected.inspect}") do |given, matcher|
4
+ matcher.failure_message = "expected #{given.inspect} to contain #{expected.inspect}"
5
+ matcher.negative_failure_message = "expected #{given.inspect} not to contain #{expected.inspect}"
6
+ given.index expected
7
+ end
8
+ end
9
+ end
10
+
11
+ World(Matchers)
@@ -0,0 +1,9 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'indentation/string_mod.rb'
5
+ require 'indentation/array_mod.rb'
6
+
7
+ module Indentation
8
+ VERSION = '0.0.1'
9
+ end
@@ -0,0 +1,74 @@
1
+ class Array
2
+ # Appends a given separator(s) to all but the last element in an array of Strings
3
+ # or if performed on an array of arrays, appends the separator element to the end of each array except the last
4
+ def append_separator(*separators)
5
+ len = self.length
6
+ ret_array = Array.new
7
+ self.each_with_index do |element, i|
8
+ new_element = element.dup
9
+ unless i == len - 1
10
+ separators.each do |separator|
11
+ new_element << separator
12
+ end
13
+ end
14
+ ret_array << new_element
15
+ end
16
+ ret_array
17
+ end
18
+
19
+ # Append the given separator(s) to the current array
20
+ def append_separator!(*separators)
21
+ len = self.length
22
+ self.each_with_index do |element, i|
23
+ unless i == len - 1
24
+ separators.each do |separator|
25
+ element << separator
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ # Return an indented array of strings (or an array of other arrays containing strings)
32
+ # Arguments:
33
+ # * num - How many of the specified indentation to use.
34
+ # Default for spaces is 2. Default for other is 1.
35
+ # If set to a negative value, removes that many of the specified indentation character,
36
+ # tabs, or spaces from the beginning of the string
37
+ # * i_char - Character (or string) to use for indentation
38
+ def indent(num = nil, i_char = ' ')
39
+ self.collect do |array_element|
40
+ array_element.indent(num, i_char)
41
+ end
42
+ end
43
+
44
+ # Apply indentation to this array
45
+ # Arguments:
46
+ # * num - How many of the specified indentation to use.
47
+ # Default for spaces is 2. Default for other is 1.
48
+ # If set to a negative value, removes that many of the specified indentation character,
49
+ # tabs, or spaces from the beginning of the string
50
+ # * i_char - Character (or string) to use for indentation
51
+ def indent!(num = nil, i_char = ' ')
52
+ self.collect! do |array_element|
53
+ array_element.indent!(num, i_char)
54
+ end
55
+ end
56
+
57
+ # Get the least indentation of all elements
58
+ def find_least_indentation
59
+ self.collect{|array_element| array_element.find_least_indentation }.min
60
+ end
61
+
62
+ # Find the least indentation of all elements and remove that amount (if any)
63
+ # Can pass an optional modifier that changes the indentation amount removed
64
+ def reset_indentation(modifier = 0)
65
+ indent(-find_least_indentation + modifier)
66
+ end
67
+
68
+ # Replaces the current array with one that has its indentation reset
69
+ # Can pass an optional modifier that changes the indentation amount removed
70
+ def reset_indentation!(modifier = 0)
71
+ indent!(-find_least_indentation + modifier)
72
+ end
73
+
74
+ end
@@ -0,0 +1,64 @@
1
+ class String
2
+ # Return an indented copy of this string
3
+ # Arguments:
4
+ # * num - How many of the specified indentation to use.
5
+ # Default for spaces is 2. Default for other is 1.
6
+ # If set to a negative value, removes that many of the specified indentation character,
7
+ # tabs, or spaces from the beginning of the string
8
+ # * i_char - Character (or string) to use for indentation
9
+ def indent(num = nil, i_char = ' ')
10
+ _indent(num, i_char)
11
+ end
12
+
13
+ # Indents this string
14
+ # Arguments:
15
+ # * num - How many of the specified indentation to use.
16
+ # Default for spaces is 2. Default for other is 1.
17
+ # If set to a negative value, removes that many of the specified indentation character,
18
+ # tabs, or spaces from the beginning of the string
19
+ # * i_char - Character (or string) to use for indentation
20
+ def indent!(num = nil, i_char = ' ')
21
+ replace(_indent(num, i_char))
22
+ end
23
+
24
+ # Split across newlines and return the fewest number of indentation characters found on each line
25
+ def find_least_indentation
26
+ empty? ? 0 : split("\n", -1).collect{|substr| substr.match(/^[ \t]*/).to_s.length}.min
27
+ end
28
+
29
+ # Find the least indentation of all lines within this string and remove that amount (if any)
30
+ # Can pass an optional modifier that changes the indentation amount removed
31
+ def reset_indentation(modifier = 0)
32
+ indent(-find_least_indentation + modifier)
33
+ end
34
+
35
+ # Replaces the current string with one that has had its indentation reset
36
+ # Can pass an optional modifier that changes the indentation amount removed
37
+ def reset_indentation!(modifier = 0)
38
+ indent!(-find_least_indentation + modifier)
39
+ end
40
+
41
+ private
42
+ def _indent(num = nil, i_char = ' ')
43
+ # Define number of indentations to use
44
+ number = num
45
+ # Default number to 2 if spaces or 1 if other
46
+ number ||= (i_char == ' ') ? 2 : 1
47
+
48
+ case
49
+ when number >= 0
50
+ split("\n", -1).collect{|line| (i_char * number) + line}.join("\n")
51
+ else
52
+ i_regexp = Regexp.new("^([ \t]|#{i_char})")
53
+ split("\n", -1).collect do |line|
54
+ ret_str = String.new(line)
55
+ number.abs.times do
56
+ match = ret_str.sub!(i_regexp, '')
57
+ break unless match
58
+ end
59
+ ret_str
60
+ end.join("\n")
61
+ end
62
+ end
63
+
64
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/indentation.rb'}"
9
+ puts "Loading indentation gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,356 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ # Time to add your specs!
4
+ # http://rspec.info/
5
+ describe "Single-line String indentation" do
6
+
7
+ it "should indent a string using 2 spaces as the default" do
8
+ "test string".indent.should == " test string"
9
+ end
10
+
11
+ it "should indent a given string the passed amount of indentation" do
12
+ "test string".indent(3).should == " test string"
13
+ "test string".indent(0).should == "test string"
14
+ end
15
+
16
+ it "should indent a string using 1 tab as default, if tabs were specified but an amount was not" do
17
+ "test string".indent(nil, "\t").should == "\ttest string"
18
+ end
19
+
20
+ it "should indent a string the passed amount of tabbed indentation" do
21
+ "test string ".indent(3, "\t").should == "\t\t\ttest string "
22
+ "test string ".indent(0, "\t").should == "test string "
23
+ end
24
+
25
+ it "should indent a string using the passed indentation character" do
26
+ "test string".indent(1, "!").should == "!test string"
27
+ "test string".indent(2, "--").should == "----test string"
28
+ end
29
+
30
+ it "should remove tab and space indentation if a negative amount is specified" do
31
+ " test string ".indent(-2).should == " test string "
32
+ " test string ".indent(-3).should == "test string "
33
+ " test string ".indent(-4).should == "test string "
34
+
35
+ "\t test string ".indent(-2).should == " test string "
36
+ "\t test string ".indent(-3).should == "test string "
37
+ "\t test string ".indent(-4).should == "test string "
38
+
39
+ " \t test string ".indent(-2).should == " test string "
40
+ " \t test string ".indent(-3).should == "test string "
41
+ " \t test string ".indent(-4).should == "test string "
42
+
43
+ " \ttest string ".indent(-2).should == "\ttest string "
44
+ " \ttest string ".indent(-3).should == "test string "
45
+ " \ttest string ".indent(-4).should == "test string "
46
+
47
+ "\t\t\ttest string ".indent(-2).should == "\ttest string "
48
+ "\t\t\ttest string ".indent(-3).should == "test string "
49
+ "\t\t\ttest string ".indent(-4).should == "test string "
50
+ end
51
+
52
+ it "should remove tab, space, and the specified indentation type if a negative amount and indentation character is specified" do
53
+ " \t\t\t---my string".indent(-8, '-').should == "-my string"
54
+ " \t\t\t---my string".indent(-8).should == "---my string"
55
+ " --- my string".indent(-4, "---").should == " my string"
56
+ end
57
+
58
+ end
59
+
60
+ describe "Multi-line String indentation" do
61
+
62
+ it "should indent each line within the given string" do
63
+ " this\nis\na\n test\n".indent.should == " this\n is\n a\n test\n "
64
+ " this\nis\na\n test\n".indent(3).should == " this\n is\n a\n test\n "
65
+ " this\nis\na\n test\n".indent(3, "\t").should == "\t\t\t this\n\t\t\tis\n\t\t\ta\n\t\t\t test\n\t\t\t"
66
+ end
67
+
68
+ it "should de-indent each line within the given string" do
69
+ " \t\t\t This\n is \n a\ntest\n ".indent(-1).should == " \t\t\t This\n is \na\ntest\n"
70
+ " \t\t\t This\n is \n a\ntest\n ".indent(-2).should == " \t\t\t This\n is \na\ntest\n"
71
+ " \t\t\t This\n is \n a\ntest\n ".indent(-3).should == "\t\t\t This\nis \na\ntest\n"
72
+ " \t\t\t This\n is \n a\ntest\n ".indent(-6).should == " This\nis \na\ntest\n"
73
+ " \t\t\t This\n is \n a\ntest\n ".indent(-7).should == "This\nis \na\ntest\n"
74
+ " \t\t\t This\n is \n a\ntest\n ".indent(-8).should == "This\nis \na\ntest\n"
75
+ end
76
+ end
77
+
78
+ describe "Single-line Array indentation" do
79
+ before :all do
80
+ @test_array = ['ali', ' bob', 'charlie']
81
+ end
82
+
83
+ it "should indent each element 2 spaces by default" do
84
+ indented_array = @test_array.indent
85
+
86
+ indented_array[0].should == ' ali'
87
+ indented_array[1].should == ' bob'
88
+ indented_array[2].should == ' charlie'
89
+ end
90
+
91
+ it "should indent each element using 1 tab as default, if tabs were specified but an amount was not" do
92
+ indented_array = @test_array.indent(nil, "\t")
93
+
94
+ indented_array[0].should == "\tali"
95
+ indented_array[1].should == "\t bob"
96
+ indented_array[2].should == "\tcharlie"
97
+ end
98
+
99
+ it "should remove indentation if a negative amount was specified" do
100
+ indented_array = @test_array.indent.indent(1, "\t").indent(3) # => [" \t ali", " \t bob", " \t charlie"]
101
+ # Check that the array was correctly created
102
+ indented_array[0].should == " \t ali"
103
+ indented_array[1].should == " \t bob"
104
+ indented_array[2].should == " \t charlie"
105
+
106
+ # De-indent the array
107
+ deindented_array = indented_array.indent(-7)
108
+ deindented_array[0].should == "ali"
109
+ deindented_array[1].should == " bob"
110
+ deindented_array[2].should == "charlie"
111
+ end
112
+
113
+ end
114
+
115
+ describe "Multi-line Array indentation" do
116
+ before :all do
117
+ @test_array = ["\n \t\n test\n", " One\n Two\n Three", "This\nis\na\ntest"]
118
+ end
119
+
120
+ it "should indent each line of each element 2 spaces by default" do
121
+ indented_array = @test_array.indent
122
+
123
+ indented_array[0].should == " \n \t\n test\n "
124
+ indented_array[1].should == " One\n Two\n Three"
125
+ indented_array[2].should == " This\n is\n a\n test"
126
+ end
127
+
128
+ it "should indent each line of each element using 1 tab as default, if tabs were specified but an amount was not" do
129
+ indented_array = @test_array.indent(nil, "\t")
130
+
131
+ indented_array[0].should == "\t\n\t \t\n\t test\n\t"
132
+ indented_array[1].should == "\t One\n\t Two\n\t Three"
133
+ indented_array[2].should == "\tThis\n\tis\n\ta\n\ttest"
134
+ end
135
+
136
+ it "should remove indentation if a negative amount was specified" do
137
+ deindented_array = @test_array.indent(-1)
138
+
139
+ deindented_array[0].should == "\n\t\ntest\n"
140
+ deindented_array[1].should == "One\n Two\n Three"
141
+ deindented_array[2].should == "This\nis\na\ntest"
142
+ end
143
+
144
+ it "should indent any contained arrays if they exist" do
145
+ array = @test_array
146
+ array << ["This", 'is', "a", "test"]
147
+ indented_array = array.indent
148
+
149
+ indented_array[3][0].should == " This"
150
+ indented_array[3][1].should == " is"
151
+ indented_array[3][2].should == " a"
152
+ indented_array[3][3].should == " test"
153
+ end
154
+ end
155
+
156
+ describe "Append Separator function" do
157
+ before :each do
158
+ @strings = ['bob', 'sue', 'joe']
159
+ @arrays = [['one', 'two', 'three'], ['1', '2', '3'], ['uno', 'dos', 'tres']]
160
+ end
161
+
162
+ it "should append the given separator to the end of each string except the last if called on an array of strings" do
163
+ separated_array = @strings.append_separator("!")
164
+
165
+ separated_array[0].should == "bob!"
166
+ separated_array[1].should == "sue!"
167
+ separated_array[2].should == "joe"
168
+ end
169
+
170
+ it "should append the given separator to the end of each array except the last if called on an array of arrays" do
171
+ separated_array = @arrays.append_separator("!")
172
+
173
+ separated_array[0].last.should == "!"
174
+ separated_array[1].last.should == "!"
175
+ separated_array[2].last.should == "tres"
176
+ end
177
+
178
+ it "should not modify the current array unless the '!' version of the function is used - Strings" do
179
+ @strings.append_separator("!")
180
+ @strings[0].should == 'bob'
181
+ @strings[1].should == 'sue'
182
+ @strings[2].should == 'joe'
183
+ end
184
+
185
+ it "should not modify the current array unless the '!' version of the function is used - Arrays" do
186
+ @arrays.append_separator("!")
187
+ @arrays[0].last.should == "three"
188
+ @arrays[1].last.should == "3"
189
+ @arrays[2].last.should == "tres"
190
+ end
191
+
192
+ it "should apply changes to the current array if the '!' version of the function is used - Strings" do
193
+ @strings.append_separator!("!")
194
+ @strings[0].should == 'bob!'
195
+ @strings[1].should == 'sue!'
196
+ @strings[2].should == 'joe'
197
+ end
198
+
199
+ it "should apply changes to the current array if the '!' version of the function is used - Arrays" do
200
+ @arrays.append_separator!("!")
201
+ @arrays[0].last.should == "!"
202
+ @arrays[1].last.should == "!"
203
+ @arrays[2].last.should == "tres"
204
+ end
205
+ end
206
+
207
+ describe "Find Least Indentation function" do
208
+ it "should return the least amount of indentation on any line within an Array or String" do
209
+ test_string_one = " \tThis\n is a\n \ttest"
210
+ test_string_two = " \t Test\n Number\n Two"
211
+ test_string_one.find_least_indentation.should == 2
212
+ test_string_two.find_least_indentation.should == 7
213
+
214
+ test_array = [test_string_one, test_string_two]
215
+ test_array.find_least_indentation.should == 2
216
+ test_array << " Test\n again!"
217
+ test_array.find_least_indentation.should == 1
218
+ test_array << "No Indentation!"
219
+ test_array.find_least_indentation.should == 0
220
+ test_array << ""
221
+ test_array.find_least_indentation.should == 0
222
+ end
223
+ end
224
+
225
+ describe "Reset Indentation function" do
226
+ it "should remove the least amount of indentation found in a String or Array" do
227
+ test_string = <<-EOS.chomp.reset_indentation
228
+ def method_name(arg)
229
+ # Do stuff here
230
+ end
231
+ EOS
232
+ test_string.should == "def method_name(arg)\n # Do stuff here\nend"
233
+
234
+ test_array = [" def method_name(arg)", " # Do stuff here", " end"]
235
+
236
+ test_array.reset_indentation.join("\n").should == test_string
237
+ end
238
+
239
+ it "should allow a modifier to be passed that changes how much indentation is added/removed" do
240
+
241
+ string = <<-EOS
242
+ def method_name(arg)
243
+ # Do stuff here
244
+ end
245
+ EOS
246
+
247
+ test_string = string.chomp.reset_indentation(2)
248
+ test_string.should == " def method_name(arg)\n # Do stuff here\n end"
249
+
250
+ test_array = [" def method_name(arg)", " # Do stuff here", " end"]
251
+
252
+ test_array.reset_indentation(2).join("\n").should == test_string
253
+
254
+ # Use of negative indentation works here too
255
+ string.chomp.reset_indentation(-1).should == "def method_name(arg)\n # Do stuff here\nend"
256
+ test_array.reset_indentation(-1).join("\n").should == string.chomp.reset_indentation(-1)
257
+ end
258
+
259
+ it "should not modify the current object unless the '!' version of the function is used - String" do
260
+ string = " This\n is\n a test"
261
+ string.reset_indentation.should == "This\n is\na test"
262
+ string.should == " This\n is\n a test"
263
+ end
264
+
265
+ it "should not modify the current object unless the '!' version of the function is used - Array" do
266
+ array = [" This", " is", " a test"]
267
+ array.reset_indentation.should == ["This", " is", "a test"]
268
+ array.should == [" This", " is", " a test"]
269
+ end
270
+
271
+ it "should apply changes to the current object if the '!' version of the function is used - String" do
272
+ string = " This\n is\n a test"
273
+ string.reset_indentation!.should == "This\n is\na test"
274
+ string.should == "This\n is\na test"
275
+ end
276
+
277
+ it "should apply changes to the current object if the '!' version of the function is used - Array" do
278
+ array = [" This", " is", " a test"]
279
+ array.reset_indentation!.should == ["This", " is", "a test"]
280
+ array.should == ["This", " is", "a test"]
281
+ end
282
+
283
+ end
284
+
285
+ describe "README.rdoc" do
286
+ it "should have correct examples for indent" do
287
+
288
+ # Default indentation is 2 spaces
289
+ "test".indent.should == " test"
290
+
291
+ # Amount of indentation can be changed
292
+ "test".indent(3).should == " test"
293
+
294
+ # Indentation character (or string) is set as the second parameter of indent.
295
+ "test".indent(2, "\t").should == "\t\ttest"
296
+
297
+ # Operates on multi-line strings
298
+ "this\nis\na\ntest".indent.should == " this\n is\n a\n test"
299
+
300
+ # Indent method accepts negative values (Removes tabs, spaces, and supplied indentation string)
301
+ " test".indent(-1).should == " test"
302
+ "\t test".indent(-5).should == "test"
303
+ "\t-- Test".indent(-10).should == "-- Test"
304
+ "\t-- Test".indent(-10, '-').should == "Test"
305
+ "--- Test".indent(-2, '--').should == "- Test"
306
+
307
+ # Operates on arrays
308
+ ["one", "two"].indent.should == [" one", " two"]
309
+ [["one", " two"], ["uno", "\t\tdos"]].indent.should == [[" one", " two"], [" uno", " \t\tdos"]]
310
+
311
+ end
312
+
313
+ it "should have correct examples for reset indentation" do
314
+
315
+ # 'resets' the indentation of a string by finding the least amount of indentation in the String/Array, and
316
+ # removing that amount from every line.
317
+ " def method_name\n # Do stuff\n end".reset_indentation.should == "def method_name\n # Do stuff\nend"
318
+
319
+ # Operates on arrays
320
+ [" def method_name", " # Do stuff", " end"].reset_indentation.should == ["def method_name", " # Do stuff", "end"]
321
+
322
+ # Useful for heredocs - must chomp to remove trailing newline
323
+ my_string = <<-EOS.chomp.reset_indentation
324
+ def method_name
325
+ # Do stuff
326
+ end
327
+ EOS
328
+ my_string.should == "def method_name\n # Do stuff\nend"
329
+
330
+ # Accepts an indentation modifier
331
+ " one\n two".reset_indentation(1).should == " one\n two"
332
+ " one\n two".reset_indentation(0).should == " one\ntwo" # Default behavior
333
+ " one\n two".reset_indentation(-1).should == "one\ntwo"
334
+ end
335
+
336
+ it "should have correct examples for append separator" do
337
+
338
+ # Given an Array of Arrays or an Array of Strings, appends a separator object to all but the last element in the Array.
339
+ # NOTE: For an Array of Strings the separator object must be a String, since it is appended to other Strings
340
+ ["arg1", "arg2", "arg3"].append_separator("!").should == ["arg1!", "arg2!", "arg3"]
341
+ [["line1"], ["line2"], ["line3"]].append_separator("").should == [["line1", ""], ["line2", ""], ["line3"]]
342
+ [["line1", "line2"], ["line3", "line4"]].append_separator("").should == [["line1", "line2", ""], ["line3", "line4"]]
343
+
344
+ # Useful combined with indent and join
345
+ vars = ["var1", "var2", "var3", "var4"]
346
+ method_def = ["def add_up(#{vars.join(', ')})"]
347
+ method_body = vars.append_separator(" + ")
348
+ method_def += method_body.indent
349
+ method_def << "end"
350
+ method_def.join("\n").should == "def add_up(var1, var2, var3, var4)\n var1 + \n var2 + \n var3 + \n var4\nend"
351
+
352
+ # Handy for separating arrays of string arrays
353
+ test_array = [["this", "is", "a", "test"], ["quick", "brown", "fox"], ["lazy", "typist"]]
354
+ test_array.append_separator("").join("\n").should == "this\nis\na\ntest\n\nquick\nbrown\nfox\n\nlazy\ntypist"
355
+ end
356
+ end
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'indentation'
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec/models"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ['--options', "spec/spec.opts"]
20
+ t.spec_files = FileList['spec/**/*_spec.rb']
21
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: indentation
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
+ - Samuel Dana
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-06 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rubyforge
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ - 4
31
+ version: 2.0.4
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: hoe
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 6
44
+ - 0
45
+ version: 2.6.0
46
+ type: :development
47
+ version_requirements: *id002
48
+ description: A small library of extensions to Ruby's Array and String classes that allow indentation manipulation of Strings and Arrays of Strings. Has the capability of working with multi-line strings. If you frequently use String arrays to manipulate text, see synopsis below for examples of how indentation can make your life easier.
49
+ email:
50
+ - s.dana@prometheuscomputing.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - History.txt
57
+ - Manifest.txt
58
+ - PostInstall.txt
59
+ files:
60
+ - History.txt
61
+ - Manifest.txt
62
+ - PostInstall.txt
63
+ - README.rdoc
64
+ - Rakefile
65
+ - features/development.feature
66
+ - features/step_definitions/common_steps.rb
67
+ - features/support/common.rb
68
+ - features/support/env.rb
69
+ - features/support/matchers.rb
70
+ - lib/indentation.rb
71
+ - lib/indentation/array_mod.rb
72
+ - lib/indentation/string_mod.rb
73
+ - script/console
74
+ - script/destroy
75
+ - script/generate
76
+ - spec/indentation_spec.rb
77
+ - spec/spec.opts
78
+ - spec/spec_helper.rb
79
+ - tasks/rspec.rake
80
+ has_rdoc: true
81
+ homepage: http://github.com/samueldana/indentation
82
+ licenses: []
83
+
84
+ post_install_message: |-
85
+ -------------------------------------------------------------------------------
86
+ Thanks for installing the indentation gem! :)
87
+
88
+ For more information on indentation, see http://indentation.rubyforge.org
89
+ -------------------------------------------------------------------------------
90
+ rdoc_options:
91
+ - --main
92
+ - README.rdoc
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ requirements: []
110
+
111
+ rubyforge_project: indentation
112
+ rubygems_version: 1.3.6
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: A small library of extensions to Ruby's Array and String classes that allow indentation manipulation of Strings and Arrays of Strings
116
+ test_files: []
117
+