monkey_string 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +1 -0
- data/lib/monkey_string.rb +13 -0
- data/lib/monkey_string/introspection.rb +31 -0
- data/lib/monkey_string/random.rb +33 -0
- data/lib/monkey_string/version.rb +3 -0
- data/monkey_string.gemspec +18 -0
- data/spec/.rspec +2 -0
- data/spec/monkey_string_spec.rb +159 -0
- data/spec/spec_helper.rb +8 -0
- metadata +62 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Diego Salazar
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# MonkeyString
|
2
|
+
|
3
|
+
Some neat monkey patches on Ruby's beloved String class.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'monkey_string'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install monkey_string
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Random String Generator
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
String.random
|
25
|
+
# => 's' # a random letter
|
26
|
+
|
27
|
+
String.random size: 7
|
28
|
+
# => 'tcoqntv' # 7 random lower cased letters
|
29
|
+
|
30
|
+
String.random size: 7, mixed_cased: true
|
31
|
+
# => 'osPiyeM' # 7 random mixed case letters
|
32
|
+
|
33
|
+
String.random size: 7, alphanumeric: true
|
34
|
+
# => 'rcwq3l2' # 7 random alphanumerals
|
35
|
+
|
36
|
+
'as4vLm2W'.random
|
37
|
+
# => 'e4vmHjku' # random letters of the same size and mixed-case-ness
|
38
|
+
```
|
39
|
+
|
40
|
+
### String Introspection
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
'asd'.mixed_case? # => true
|
44
|
+
'asd'.upcased? # => false
|
45
|
+
'asd'.downcased? # => true
|
46
|
+
'asd'.alphanumeric? # => false
|
47
|
+
'asd123'.alphanumeric? # => true
|
48
|
+
'asd'.numeric? # => false
|
49
|
+
'123'.numeric? # => true
|
50
|
+
'asd123'.to_alpha # => 'asd'
|
51
|
+
'asd123'.num_alpha # => 3
|
52
|
+
```
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require "monkey_string/version"
|
2
|
+
|
3
|
+
module MonkeyString
|
4
|
+
autoload :Random, 'monkey_string/random'
|
5
|
+
autoload :Introspection, 'monkey_string/introspection'
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.send :include, Introspection
|
9
|
+
base.send :include, Random
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
String.send :include, MonkeyString
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module MonkeyString
|
2
|
+
module Introspection
|
3
|
+
def mixed_case?
|
4
|
+
!upcased? && !downcased? || alphanumeric? && !numeric?
|
5
|
+
end
|
6
|
+
|
7
|
+
def downcased?
|
8
|
+
eql? downcase
|
9
|
+
end
|
10
|
+
|
11
|
+
def upcased?
|
12
|
+
eql? upcase
|
13
|
+
end
|
14
|
+
|
15
|
+
def alphanumeric?
|
16
|
+
!numeric? && size > num_alpha
|
17
|
+
end
|
18
|
+
|
19
|
+
def numeric?
|
20
|
+
num_alpha == 0
|
21
|
+
end
|
22
|
+
|
23
|
+
def num_alpha
|
24
|
+
to_alpha.size
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_alpha
|
28
|
+
gsub(/[^a-zA-Z]/, '')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module MonkeyString
|
2
|
+
module Random
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
base.send :include, InstanceMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def random(options = {})
|
10
|
+
options = { :size => 1 }.merge options
|
11
|
+
|
12
|
+
a = array options
|
13
|
+
s = a.size-1
|
14
|
+
(0...options[:size]).map { a[rand(s)] }.join
|
15
|
+
end
|
16
|
+
|
17
|
+
def array(options = {})
|
18
|
+
options = { :mixed_case => false, :alphanumeric => false }.merge options
|
19
|
+
|
20
|
+
a = [('a'..'z')]
|
21
|
+
a << ('A'..'Z') if options[:mixed_case]
|
22
|
+
a << ('0'..'9') if options[:alphanumeric]
|
23
|
+
a.map(&:to_a).flatten
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module InstanceMethods
|
28
|
+
def random
|
29
|
+
self.class.random :size => size, :alphanumeric => alphanumeric?, :mixed_case => mixed_case?
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'monkey_string/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "monkey_string"
|
8
|
+
gem.version = MonkeyString::VERSION
|
9
|
+
gem.authors = ["Diego Salazar"]
|
10
|
+
gem.email = ["diego@greyrobot.com"]
|
11
|
+
gem.description = %q{Adds neat methods to String}
|
12
|
+
gem.summary = %q{Adds class and instance methods for random strings, alphanumeric introspection, and more.}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
end
|
data/spec/.rspec
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MonkeyString do
|
4
|
+
describe MonkeyString::Introspection do
|
5
|
+
describe '#mixed_case?' do
|
6
|
+
it 'is true when self contains both lower and upper case letters' do
|
7
|
+
'asdASD'.mixed_case?.should be true
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'is false when self is all lower cased' do
|
11
|
+
'asd'.mixed_case?.should be false
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'is false when self is all upper cased' do
|
15
|
+
'ASD'.mixed_case?.should be false
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'is true when self is alphanumeric' do
|
19
|
+
'asd123'.mixed_case?.should be true
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'is false when self is all numeric' do
|
23
|
+
'123'.mixed_case?.should be false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '#upcased?' do
|
28
|
+
it 'returns true when all letters upper cased' do
|
29
|
+
'ASD'.upcased?.should be true
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'returns false when not all letters upper cased' do
|
33
|
+
'ASd'.upcased?.should be false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#downcased?' do
|
38
|
+
it 'returns true when all letters lower cased' do
|
39
|
+
'asd'.downcased?.should be true
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'returns false when not all letters lower cased' do
|
43
|
+
'asD'.downcased?.should be false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#alphanumeric?' do
|
48
|
+
it 'is true when self contains letters and numbers' do
|
49
|
+
'asd123'.alphanumeric?.should be true
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'is false when self is all letters' do
|
53
|
+
'asd'.alphanumeric?.should be false
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'is false when self is all numbers' do
|
57
|
+
'123'.alphanumeric?.should be false
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe '#numeric?' do
|
62
|
+
it 'is true when self is all numbers' do
|
63
|
+
'123'.numeric?.should be true
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'is false when self is all letters' do
|
67
|
+
'asd'.numeric?.should be false
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'is false when self is alphanumeric' do
|
71
|
+
'asd123'.numeric?.should be false
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe '#num_alpha' do
|
76
|
+
it 'returns number of letters in self' do
|
77
|
+
'asd'.num_alpha.should be 3
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'does not count numbers' do
|
81
|
+
'asd123'.num_alpha.should be 3
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe '#to_alpha' do
|
86
|
+
it 'returns just the letters in self' do
|
87
|
+
'asd123'.to_alpha.should eq 'asd'
|
88
|
+
end
|
89
|
+
|
90
|
+
it "does not return the numbers in self" do
|
91
|
+
'asd123'.to_alpha.should_not include '123'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe MonkeyString::Random do
|
97
|
+
describe '.random' do
|
98
|
+
it 'returns one random letter if no options given' do
|
99
|
+
String.random.size.should be 1
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'returns a random lower cased letter if no options given' do
|
103
|
+
String.random.should
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'returns a random alpha string of the size given' do
|
107
|
+
String.random(:size => 7).size.should be 7
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'can return a random alpha string in mixed case' do
|
111
|
+
String.random(:size => 7, :mixed_case => true).should be_mixed_case
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'can return a random alphanumeric string' do
|
115
|
+
String.random(:size => 7, :alphanumeric => true).should be_alphanumeric
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe '.array' do
|
120
|
+
it 'returns an array of letters in lower case' do
|
121
|
+
String.array.should eq ('a'..'z').to_a
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'can return an array of letters in mixed case' do
|
125
|
+
mixed_case = [('a'..'z').to_a, ('A'..'Z').to_a].flatten
|
126
|
+
String.array(:mixed_case => true).should eq mixed_case
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'can return an array of alphanumerals' do
|
130
|
+
alphanumerals = [('a'..'z').to_a, ('0'..'9').to_a].flatten
|
131
|
+
String.array(:alphanumeric => true).should eq alphanumerals
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
describe '#random' do
|
136
|
+
it 'returns a random string of the same size as self' do
|
137
|
+
'Hello'.random.size.should be 5
|
138
|
+
end
|
139
|
+
|
140
|
+
it 'returns mixed case if self is mixed case' do
|
141
|
+
# Sufficiently long string to give better change for a numeral to be picked.
|
142
|
+
# The random algorithm doesn't ensure that the returned string is mixed cased
|
143
|
+
# only that it may pick an upper case letter
|
144
|
+
('Hello'*100).random.should be_mixed_case
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'does not return mixed case if self is not' do
|
148
|
+
'hello'.random.should_not be_mixed_case
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'can return alphanumerals if self is alphanumeric' do
|
152
|
+
# Sufficiently long string to give better change for a numeral to be picked.
|
153
|
+
# The random algorithm doesn't ensure that the returned string has numerals
|
154
|
+
# only that it may pick a numeral
|
155
|
+
('H3ll0'*500).random.should be_alphanumeric
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: monkey_string
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Diego Salazar
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-05 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Adds neat methods to String
|
15
|
+
email:
|
16
|
+
- diego@greyrobot.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/monkey_string.rb
|
27
|
+
- lib/monkey_string/introspection.rb
|
28
|
+
- lib/monkey_string/random.rb
|
29
|
+
- lib/monkey_string/version.rb
|
30
|
+
- monkey_string.gemspec
|
31
|
+
- spec/.rspec
|
32
|
+
- spec/monkey_string_spec.rb
|
33
|
+
- spec/spec_helper.rb
|
34
|
+
homepage: ''
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.24
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Adds class and instance methods for random strings, alphanumeric introspection,
|
58
|
+
and more.
|
59
|
+
test_files:
|
60
|
+
- spec/.rspec
|
61
|
+
- spec/monkey_string_spec.rb
|
62
|
+
- spec/spec_helper.rb
|