squarecoder 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/README.rdoc +12 -1
  2. data/lib/squarecoder/core_ext/string.rb +11 -0
  3. data/lib/squarecoder/errors.rb +4 -1
  4. data/lib/squarecoder/transcoder.rb +13 -7
  5. data/lib/squarecoder.rb +2 -0
  6. metadata +2 -39
  7. data/lib/doc/Squarecoder/Transcoder.html +0 -236
  8. data/lib/doc/Squarecoder.html +0 -116
  9. data/lib/doc/String.html +0 -266
  10. data/lib/doc/created.rid +0 -5
  11. data/lib/doc/images/add.png +0 -0
  12. data/lib/doc/images/brick.png +0 -0
  13. data/lib/doc/images/brick_link.png +0 -0
  14. data/lib/doc/images/bug.png +0 -0
  15. data/lib/doc/images/bullet_black.png +0 -0
  16. data/lib/doc/images/bullet_toggle_minus.png +0 -0
  17. data/lib/doc/images/bullet_toggle_plus.png +0 -0
  18. data/lib/doc/images/date.png +0 -0
  19. data/lib/doc/images/delete.png +0 -0
  20. data/lib/doc/images/find.png +0 -0
  21. data/lib/doc/images/loadingAnimation.gif +0 -0
  22. data/lib/doc/images/macFFBgHack.png +0 -0
  23. data/lib/doc/images/package.png +0 -0
  24. data/lib/doc/images/page_green.png +0 -0
  25. data/lib/doc/images/page_white_text.png +0 -0
  26. data/lib/doc/images/page_white_width.png +0 -0
  27. data/lib/doc/images/plugin.png +0 -0
  28. data/lib/doc/images/ruby.png +0 -0
  29. data/lib/doc/images/tag_blue.png +0 -0
  30. data/lib/doc/images/tag_green.png +0 -0
  31. data/lib/doc/images/transparent.png +0 -0
  32. data/lib/doc/images/wrench.png +0 -0
  33. data/lib/doc/images/wrench_orange.png +0 -0
  34. data/lib/doc/images/zoom.png +0 -0
  35. data/lib/doc/index.html +0 -73
  36. data/lib/doc/js/darkfish.js +0 -153
  37. data/lib/doc/js/jquery.js +0 -18
  38. data/lib/doc/js/navigation.js +0 -142
  39. data/lib/doc/js/search.js +0 -94
  40. data/lib/doc/js/search_index.js +0 -1
  41. data/lib/doc/js/searcher.js +0 -228
  42. data/lib/doc/rdoc.css +0 -543
  43. data/lib/doc/table_of_contents.html +0 -64
data/README.rdoc CHANGED
@@ -1,6 +1,17 @@
1
1
  = squarecoder
2
2
 
3
- Squarecoder encapsulates a Transcoder singleton that encodes and decodes a string using the {square code}[link:http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html] method. You will also find two new String methods: String.square_encode and String.square_decode.
3
+ Squarecoder encapsulates a Squarecoder::Transcoder singleton that encodes and decodes a string using the {square code}[link:http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html] method. You will also find two new String methods: String.square_encode and String.square_decode.
4
+
5
+ == Installation
6
+
7
+ $ gem install squarecoder
8
+
9
+ Or
10
+
11
+ # in Gemfile
12
+ gem 'squarecoder'
13
+
14
+ $ bundle install
4
15
 
5
16
  == Examples
6
17
 
@@ -1,5 +1,16 @@
1
1
  class String
2
2
 
3
+ # Convert a String to a Grouped array of Characters of a specific length
4
+ #
5
+ # Example:
6
+ #
7
+ # "abcdefgh".in_character_arrays_of_length(3) # => [["a","b","c"],["d","e","f"],["g","h"]]
8
+ #
9
+ # Returns an Array.
10
+ def in_character_arrays_of_length( length )
11
+ scan( /./ ).each_slice( length ).to_a
12
+ end
13
+
3
14
  # Encode a String
4
15
  def square_encode
5
16
  dup.square_encode!
@@ -1,4 +1,7 @@
1
1
  module Squarecoder
2
- class InputTooLongError < StandardError # :nodoc:
2
+ class InputTooLong < StandardError # :nodoc:
3
+ end
4
+
5
+ class NoWhitespace < StandardError # :nodoc:
3
6
  end
4
7
  end
@@ -2,6 +2,8 @@ module Squarecoder
2
2
 
3
3
  # A Transcoder is a Singleton used to encode and decode strings.
4
4
  #
5
+ # Examples
6
+ #
5
7
  # Squarecoder::Transcoder.encode("haveaniceday") # => "hae and via ecy"
6
8
  # Squarecoder::Transcoder.decode("hae and via ecy") # => "haveaniceday"
7
9
  # "haveaniceday".square_encode # => "hae and via ecy"
@@ -11,20 +13,24 @@ module Squarecoder
11
13
  include Singleton
12
14
 
13
15
  # Encode a String in square code
14
- def self.encode a_string
15
- this_string = a_string
16
- raise Squarecoder::InputTooLongError if this_string.length > 81
17
- len = ( this_string.length ** ( 0.5 ) ).ceil
18
- arry = this_string.scan( /./ ).each_slice( len ).to_a
16
+ def self.encode a_string, ignore_whitespace = false, ignore_max_length = false
17
+ if !ignore_max_length && a_string.length > MAX_LEN
18
+ raise Squarecoder::InputTooLong
19
+ end
20
+ if !ignore_whitespace && a_string.include?(' ')
21
+ raise Squarecoder::NoWhitespace
22
+ end
23
+ len = ( a_string.length ** ( 0.5 ) ).ceil
24
+ arry = a_string.in_character_arrays_of_length( len )
19
25
  if arry.last.length != len
20
- arry.last.fill( ' ', arry.last.length..( len - 1 ) )
26
+ ( len - arry.last.length ).times { arry.last << ' ' } # a tiny bit faster than arry.fill
21
27
  end
22
28
  arry.transpose.collect { |line| line.join }.join(' ').rstrip
23
29
  end
24
30
 
25
31
  # Decode a String from square code
26
32
  def self.decode a_string
27
- encode( a_string ).gsub(' ', '')
33
+ encode( a_string, true, true ).gsub(/ +/, '')
28
34
  end
29
35
 
30
36
  end
data/lib/squarecoder.rb CHANGED
@@ -2,6 +2,8 @@ require 'rubygems'
2
2
  require 'singleton'
3
3
 
4
4
  module Squarecoder
5
+ # Maximum length of an input string
6
+ MAX_LEN = 81
5
7
  require 'squarecoder/core_ext/string'
6
8
  require 'squarecoder/errors'
7
9
  require 'squarecoder/transcoder'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: squarecoder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-25 00:00:00.000000000Z
12
+ date: 2011-11-28 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: A library to encode and decode strings using the square code encryption
15
15
  method.
@@ -20,43 +20,6 @@ extra_rdoc_files:
20
20
  - README.rdoc
21
21
  files:
22
22
  - README.rdoc
23
- - lib/doc/created.rid
24
- - lib/doc/images/add.png
25
- - lib/doc/images/brick.png
26
- - lib/doc/images/brick_link.png
27
- - lib/doc/images/bug.png
28
- - lib/doc/images/bullet_black.png
29
- - lib/doc/images/bullet_toggle_minus.png
30
- - lib/doc/images/bullet_toggle_plus.png
31
- - lib/doc/images/date.png
32
- - lib/doc/images/delete.png
33
- - lib/doc/images/find.png
34
- - lib/doc/images/loadingAnimation.gif
35
- - lib/doc/images/macFFBgHack.png
36
- - lib/doc/images/package.png
37
- - lib/doc/images/page_green.png
38
- - lib/doc/images/page_white_text.png
39
- - lib/doc/images/page_white_width.png
40
- - lib/doc/images/plugin.png
41
- - lib/doc/images/ruby.png
42
- - lib/doc/images/tag_blue.png
43
- - lib/doc/images/tag_green.png
44
- - lib/doc/images/transparent.png
45
- - lib/doc/images/wrench.png
46
- - lib/doc/images/wrench_orange.png
47
- - lib/doc/images/zoom.png
48
- - lib/doc/index.html
49
- - lib/doc/js/darkfish.js
50
- - lib/doc/js/jquery.js
51
- - lib/doc/js/navigation.js
52
- - lib/doc/js/search.js
53
- - lib/doc/js/search_index.js
54
- - lib/doc/js/searcher.js
55
- - lib/doc/rdoc.css
56
- - lib/doc/Squarecoder/Transcoder.html
57
- - lib/doc/Squarecoder.html
58
- - lib/doc/String.html
59
- - lib/doc/table_of_contents.html
60
23
  - lib/squarecoder/core_ext/string.rb
61
24
  - lib/squarecoder/errors.rb
62
25
  - lib/squarecoder/transcoder.rb
@@ -1,236 +0,0 @@
1
- <!DOCTYPE html>
2
-
3
- <html>
4
- <head>
5
- <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
6
-
7
- <title>Class: Squarecoder::Transcoder</title>
8
-
9
- <link type="text/css" media="screen" href="../rdoc.css" rel="stylesheet">
10
-
11
- <script type="text/javascript">
12
- var rdoc_rel_prefix = "../";
13
- </script>
14
-
15
- <script type="text/javascript" charset="utf-8" src="../js/jquery.js"></script>
16
- <script type="text/javascript" charset="utf-8" src="../js/navigation.js"></script>
17
- <script type="text/javascript" charset="utf-8" src="../js/search_index.js"></script>
18
- <script type="text/javascript" charset="utf-8" src="../js/search.js"></script>
19
- <script type="text/javascript" charset="utf-8" src="../js/searcher.js"></script>
20
- <script type="text/javascript" charset="utf-8" src="../js/darkfish.js"></script>
21
-
22
-
23
- <body id="top" class="class">
24
- <nav id="metadata">
25
- <nav id="home-section" class="section">
26
- <h3 class="section-header">
27
- <a href="../index.html">Home</a>
28
- <a href="../table_of_contents.html#classes">Classes</a>
29
- <a href="../table_of_contents.html#methods">Methods</a>
30
- </h3>
31
- </nav>
32
-
33
-
34
- <nav id="search-section" class="section project-section" class="initially-hidden">
35
- <form action="#" method="get" accept-charset="utf-8">
36
- <h3 class="section-header">
37
- <input type="text" name="search" placeholder="Search" id="search-field"
38
- title="Type to search, Up and Down to navigate, Enter to load">
39
- </h3>
40
- </form>
41
-
42
- <ul id="search-results" class="initially-hidden"></ul>
43
- </nav>
44
-
45
-
46
- <div id="file-metadata">
47
- <nav id="file-list-section" class="section">
48
- <h3 class="section-header">Defined In</h3>
49
- <ul>
50
- <li>squarecoder/transcoder.rb
51
- </ul>
52
- </nav>
53
-
54
-
55
- </div>
56
-
57
- <div id="class-metadata">
58
-
59
- <nav id="parent-class-section" class="section">
60
- <h3 class="section-header">Parent</h3>
61
-
62
- <p class="link">Object
63
-
64
- </nav>
65
-
66
- <!-- Included Modules -->
67
- <nav id="includes-section" class="section">
68
- <h3 class="section-header">Included Modules</h3>
69
-
70
- <ul class="link-list">
71
-
72
-
73
- <li><span class="include">Singleton</span>
74
-
75
-
76
- </ul>
77
- </nav>
78
-
79
- <!-- Method Quickref -->
80
- <nav id="method-list-section" class="section">
81
- <h3 class="section-header">Methods</h3>
82
-
83
- <ul class="link-list">
84
-
85
- <li><a href="#method-c-decode">::decode</a>
86
-
87
- <li><a href="#method-c-encode">::encode</a>
88
-
89
- </ul>
90
- </nav>
91
-
92
- </div>
93
-
94
- <div id="project-metadata">
95
-
96
- <nav id="classindex-section" class="section project-section">
97
- <h3 class="section-header">Class and Module Index</h3>
98
-
99
- <ul class="link-list">
100
-
101
- <li><a href="../Squarecoder.html">Squarecoder</a>
102
-
103
- <li><a href="../Squarecoder/Transcoder.html">Squarecoder::Transcoder</a>
104
-
105
- <li><a href="../String.html">String</a>
106
-
107
- </ul>
108
- </nav>
109
-
110
- </div>
111
- </nav>
112
-
113
- <div id="documentation">
114
- <h1 class="class">class Squarecoder::Transcoder</h1>
115
-
116
- <div id="description" class="description">
117
-
118
- <p>A <a href="Transcoder.html">Transcoder</a> is a Singleton used to encode
119
- and decode strings.</p>
120
-
121
- <pre class="ruby"><span class="ruby-constant">Squarecoder</span><span class="ruby-operator">::</span><span class="ruby-constant">Transcoder</span>.<span class="ruby-identifier">encode</span>(<span class="ruby-string">&quot;haveaniceday&quot;</span>) <span class="ruby-comment"># =&gt; &quot;hae and via ecy&quot;</span>
122
- <span class="ruby-constant">Squarecoder</span><span class="ruby-operator">::</span><span class="ruby-constant">Transcoder</span>.<span class="ruby-identifier">decode</span>(<span class="ruby-string">&quot;hae and via ecy&quot;</span>) <span class="ruby-comment"># =&gt; &quot;haveaniceday&quot;</span>
123
- <span class="ruby-string">&quot;haveaniceday&quot;</span>.<span class="ruby-identifier">square_encode</span> <span class="ruby-comment"># =&gt; &quot;hae and via ecy&quot;</span>
124
- <span class="ruby-string">&quot;hae and via ecy&quot;</span>.<span class="ruby-identifier">square_decode</span> <span class="ruby-comment"># =&gt; &quot;haveaniceday&quot;</span>
125
- </pre>
126
-
127
- </div><!-- description -->
128
-
129
-
130
-
131
-
132
- <section id="5Buntitled-5D" class="documentation-section">
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
- <!-- Methods -->
142
-
143
- <section id="public-class-5Buntitled-5D-method-details" class="method-section section">
144
- <h3 class="section-header">Public Class Methods</h3>
145
-
146
-
147
- <div id="method-c-decode" class="method-detail ">
148
-
149
- <div class="method-heading">
150
- <span class="method-name">decode</span><span
151
- class="method-args">(a_string, force = false)</span>
152
- <span class="method-click-advice">click to toggle source</span>
153
- </div>
154
-
155
-
156
- <div class="method-description">
157
-
158
- <p>Decode a <a href="../String.html">String</a></p>
159
-
160
- <pre>+a_string+ is the String to decode
161
- +force+ is a Boolean that tells the decoder to skip a cached value (unsupported)</pre>
162
-
163
- <p>Returns a <a href="../String.html">String</a></p>
164
-
165
-
166
-
167
- <div class="method-source-code" id="decode-source">
168
- <pre><span class="ruby-comment"># File squarecoder/transcoder.rb, line 34</span>
169
- <span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">decode</span> <span class="ruby-identifier">a_string</span>, <span class="ruby-identifier">force</span> = <span class="ruby-keyword">false</span>
170
- <span class="ruby-identifier">encode</span>( <span class="ruby-identifier">a_string</span> ).<span class="ruby-identifier">gsub</span>(<span class="ruby-string">' '</span>, <span class="ruby-string">''</span>)
171
- <span class="ruby-keyword">end</span></pre>
172
- </div><!-- decode-source -->
173
-
174
- </div>
175
-
176
-
177
-
178
-
179
- </div><!-- decode-method -->
180
-
181
-
182
- <div id="method-c-encode" class="method-detail ">
183
-
184
- <div class="method-heading">
185
- <span class="method-name">encode</span><span
186
- class="method-args">(a_string, force = false)</span>
187
- <span class="method-click-advice">click to toggle source</span>
188
- </div>
189
-
190
-
191
- <div class="method-description">
192
-
193
- <p>Encode a <a href="../String.html">String</a></p>
194
-
195
- <pre>+a_string+ is the String to encode and must be less than 81 characters
196
- +force+ is a Boolean that tells the encoder to skip a cached value (unsupported)</pre>
197
-
198
- <p>Returns a <a href="../String.html">String</a></p>
199
-
200
-
201
-
202
- <div class="method-source-code" id="encode-source">
203
- <pre><span class="ruby-comment"># File squarecoder/transcoder.rb, line 18</span>
204
- <span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">encode</span> <span class="ruby-identifier">a_string</span>, <span class="ruby-identifier">force</span> = <span class="ruby-keyword">false</span>
205
- <span class="ruby-identifier">this_string</span> = <span class="ruby-identifier">a_string</span>
206
- <span class="ruby-identifier">raise</span> <span class="ruby-constant">Squarecoder</span><span class="ruby-operator">::</span><span class="ruby-constant">TooLongError</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">this_string</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">81</span>
207
- <span class="ruby-identifier">len</span> = ( <span class="ruby-identifier">this_string</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">**</span> ( <span class="ruby-value">0.5</span> ) ).<span class="ruby-identifier">ceil</span>
208
- <span class="ruby-identifier">arry</span> = <span class="ruby-identifier">this_string</span>.<span class="ruby-identifier">scan</span>( <span class="ruby-regexp">/./</span> ).<span class="ruby-identifier">each_slice</span>( <span class="ruby-identifier">len</span> ).<span class="ruby-identifier">to_a</span>
209
- <span class="ruby-keyword">if</span> <span class="ruby-identifier">arry</span>.<span class="ruby-identifier">last</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">len</span>
210
- <span class="ruby-identifier">arry</span>.<span class="ruby-identifier">last</span>.<span class="ruby-identifier">fill</span>( <span class="ruby-string">' '</span>, <span class="ruby-identifier">arry</span>.<span class="ruby-identifier">last</span>.<span class="ruby-identifier">length</span><span class="ruby-operator">..</span>( <span class="ruby-identifier">len</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span> ) )
211
- <span class="ruby-keyword">end</span>
212
- <span class="ruby-identifier">arry</span>.<span class="ruby-identifier">transpose</span>.<span class="ruby-identifier">collect</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span> <span class="ruby-identifier">line</span>.<span class="ruby-identifier">join</span> }.<span class="ruby-identifier">join</span>(<span class="ruby-string">' '</span>).<span class="ruby-identifier">rstrip</span>
213
- <span class="ruby-keyword">end</span></pre>
214
- </div><!-- encode-source -->
215
-
216
- </div>
217
-
218
-
219
-
220
-
221
- </div><!-- encode-method -->
222
-
223
-
224
- </section><!-- public-class-method-details -->
225
-
226
- </section><!-- 5Buntitled-5D -->
227
-
228
- </div><!-- documentation -->
229
-
230
-
231
- <footer id="validator-badges">
232
- <p><a href="http://validator.w3.org/check/referer">[Validate]</a>
233
- <p>Generated by <a href="https://github.com/rdoc/rdoc">RDoc</a> 3.11.
234
- <p>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish Rdoc Generator</a> 3.
235
- </footer>
236
-
@@ -1,116 +0,0 @@
1
- <!DOCTYPE html>
2
-
3
- <html>
4
- <head>
5
- <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
6
-
7
- <title>Module: Squarecoder</title>
8
-
9
- <link type="text/css" media="screen" href="./rdoc.css" rel="stylesheet">
10
-
11
- <script type="text/javascript">
12
- var rdoc_rel_prefix = "./";
13
- </script>
14
-
15
- <script type="text/javascript" charset="utf-8" src="./js/jquery.js"></script>
16
- <script type="text/javascript" charset="utf-8" src="./js/navigation.js"></script>
17
- <script type="text/javascript" charset="utf-8" src="./js/search_index.js"></script>
18
- <script type="text/javascript" charset="utf-8" src="./js/search.js"></script>
19
- <script type="text/javascript" charset="utf-8" src="./js/searcher.js"></script>
20
- <script type="text/javascript" charset="utf-8" src="./js/darkfish.js"></script>
21
-
22
-
23
- <body id="top" class="module">
24
- <nav id="metadata">
25
- <nav id="home-section" class="section">
26
- <h3 class="section-header">
27
- <a href="./index.html">Home</a>
28
- <a href="./table_of_contents.html#classes">Classes</a>
29
- <a href="./table_of_contents.html#methods">Methods</a>
30
- </h3>
31
- </nav>
32
-
33
-
34
- <nav id="search-section" class="section project-section" class="initially-hidden">
35
- <form action="#" method="get" accept-charset="utf-8">
36
- <h3 class="section-header">
37
- <input type="text" name="search" placeholder="Search" id="search-field"
38
- title="Type to search, Up and Down to navigate, Enter to load">
39
- </h3>
40
- </form>
41
-
42
- <ul id="search-results" class="initially-hidden"></ul>
43
- </nav>
44
-
45
-
46
- <div id="file-metadata">
47
- <nav id="file-list-section" class="section">
48
- <h3 class="section-header">Defined In</h3>
49
- <ul>
50
- <li>squarecoder/errors.rb
51
- <li>squarecoder/transcoder.rb
52
- <li>squarecoder.rb
53
- </ul>
54
- </nav>
55
-
56
-
57
- </div>
58
-
59
- <div id="class-metadata">
60
-
61
-
62
-
63
-
64
- </div>
65
-
66
- <div id="project-metadata">
67
-
68
- <nav id="classindex-section" class="section project-section">
69
- <h3 class="section-header">Class and Module Index</h3>
70
-
71
- <ul class="link-list">
72
-
73
- <li><a href="./Squarecoder.html">Squarecoder</a>
74
-
75
- <li><a href="./Squarecoder/Transcoder.html">Squarecoder::Transcoder</a>
76
-
77
- <li><a href="./String.html">String</a>
78
-
79
- </ul>
80
- </nav>
81
-
82
- </div>
83
- </nav>
84
-
85
- <div id="documentation">
86
- <h1 class="module">module Squarecoder</h1>
87
-
88
- <div id="description" class="description">
89
-
90
- </div><!-- description -->
91
-
92
-
93
-
94
-
95
- <section id="5Buntitled-5D" class="documentation-section">
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
- <!-- Methods -->
105
-
106
- </section><!-- 5Buntitled-5D -->
107
-
108
- </div><!-- documentation -->
109
-
110
-
111
- <footer id="validator-badges">
112
- <p><a href="http://validator.w3.org/check/referer">[Validate]</a>
113
- <p>Generated by <a href="https://github.com/rdoc/rdoc">RDoc</a> 3.11.
114
- <p>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish Rdoc Generator</a> 3.
115
- </footer>
116
-