perpetual 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a2f7ca74a3592b3a0df4261b330d7b54d59ee814
4
+ data.tar.gz: d1e6f860d063a36060dfba2beb3444c361acaf8f
5
+ SHA512:
6
+ metadata.gz: 091a99556c60540f72f1525a67ce7f7f29ced230a853786a6e3431982963db6cf42015ca45ee821b9934cf273aa2f133283b2bb2411505c064966d3bce21cd83
7
+ data.tar.gz: 47ae6ceee0b51628ac76e0786476d77b8c9a14483f9f661b86539210b94aadd333e9da09eaa470e0e70df38084f9ef1b6b29c8581eb5cc19155ca59b43302eea
data/lib/perpetual.rb ADDED
@@ -0,0 +1,19 @@
1
+ #--
2
+ # This file is part of Perpetual.
3
+ #
4
+ # Copyright(C) 2012 Giovanni Capuano <webmaster@giovannicapuano.net>
5
+ #
6
+ # Perpetual is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Perpetual is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with Perpetual. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+ require 'perpetual/version'
@@ -0,0 +1,55 @@
1
+ #--
2
+ # This file is part of Perpetual.
3
+ #
4
+ # Copyright(C) 2012 Giovanni Capuano <webmaster@giovannicapuano.net>
5
+ #
6
+ # Perpetual is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Perpetual is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with Perpetual. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+ module Enumerable
20
+
21
+ # Returns the division of the elements of the enumerable.
22
+ def divide
23
+ inject(:/)
24
+ end
25
+
26
+ # Returns true if the enumerable has more than 1 element.
27
+ # Can be called with a block too, much like any?, so <tt>people.many? { |p| p.age > 26 }</tt> returns true if more than one person is over 26.
28
+ def many?
29
+ cnt = 0
30
+ if block_given?
31
+ any? do |element|
32
+ cnt += 1 if yield element
33
+ cnt > 1
34
+ end
35
+ else
36
+ any?{ (cnt += 1) > 1 }
37
+ end
38
+ end
39
+
40
+ # Returns the multiplication of the elements of the enumerable.
41
+ def multiply
42
+ inject(:*)
43
+ end
44
+
45
+ # Returns the subtraction of the elements of the enumerable.
46
+ def subtract
47
+ inject(:-)
48
+ end
49
+
50
+ # Returns the sum of the elements of the enumerable.
51
+ def sum
52
+ inject(:+)
53
+ end
54
+
55
+ end
@@ -0,0 +1,39 @@
1
+ #--
2
+ # This file is part of Perpetual.
3
+ #
4
+ # Copyright(C) 2012 Giovanni Capuano <webmaster@giovannicapuano.net>
5
+ #
6
+ # Perpetual is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Perpetual is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with Perpetual. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+ class Fixnum
20
+
21
+ # Defines the methods +n?+ where n is each number from 0-10. Returns true if the number is itself in English.
22
+ (1..10).each { |n|
23
+ define_method "#{(n).in_word}?" do
24
+ self == n
25
+ end
26
+ }
27
+
28
+ # Calculates the factorial of the number.
29
+ def factorial
30
+ self.one? ? 1 : self * (self - 1).factorial
31
+ end
32
+
33
+ # Returns the English word for the number or a empty string if the number is not known. Supports number from 0-10.
34
+ def in_word
35
+ [ 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten' ][self] || ''
36
+ end
37
+ alias_method :in_words, :in_word
38
+
39
+ end
@@ -0,0 +1,96 @@
1
+ #--
2
+ # This file is part of Perpetual.
3
+ #
4
+ # Copyright(C) 2012 Giovanni Capuano <webmaster@giovannicapuano.net>
5
+ #
6
+ # Perpetual is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Perpetual is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with Perpetual. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+ class String
20
+
21
+ # Returns the character at the +position+.
22
+ def at(position)
23
+ self[position, 1].to_s
24
+ end
25
+
26
+ # Returns true if the string contains only whitespaces or is empty.
27
+ def blank?
28
+ self !~ %r![^\s#{[0x3000].pack("U")}]!
29
+ end
30
+
31
+ # Truncates the string after a given length if text is longer than length.
32
+ def cut(limit)
33
+ self.match(%r{^(.{0,#{limit}})})[1]
34
+ end
35
+ alias_method :truncate, :cut
36
+
37
+ # Returns the first character of the string or the first +limit+ characters.
38
+ def first(limit = 1)
39
+ if limit == 0
40
+ ''
41
+ elsif limit >= self.length
42
+ self
43
+ else
44
+ self[0...limit]
45
+ end
46
+ end
47
+
48
+ # Returns the remaining of the string from the +position+.
49
+ def from(position)
50
+ self[position, -1].to_s
51
+ end
52
+
53
+ # Returns the last character of the string or the last +limit+ characters.
54
+ def last(limit = 1)
55
+ if limit == 0
56
+ ''
57
+ elsif limit >= self.length
58
+ self
59
+ else
60
+ self[(-limit)..-1]
61
+ end
62
+ end
63
+
64
+ # Returns true if the string is a valid integer.
65
+ def numeric?
66
+ self.to_i.to_s == self || self.to_f.to_s == self
67
+ end
68
+
69
+ # Removes from the string all the HTML(-like) tags.
70
+ def strip_html_tags
71
+ self.gsub(/<("[^"]*"|'[^']*'|[^'">])*>/, '')
72
+ end
73
+ alias_method :strip_tags, :strip_html_tags
74
+
75
+ def strip_html_tags!
76
+ self.gsub!(/<("[^"]*"|'[^']*'|[^'">])*>/, '')
77
+ end
78
+ alias_method :strip_tags!, :strip_html_tags!
79
+
80
+ # Capitalizes all the single words in the string.
81
+ def titlecase
82
+ self.gsub(/\b('?[a-z])/) { $1.capitalize }
83
+ end
84
+ alias_method :titleize, :titlecase
85
+
86
+ def titlecase!
87
+ self.gsub!(/\b('?[a-z])/) { $1.capitalize }
88
+ end
89
+ alias_method :titleize!, :titlecase!
90
+
91
+ # Returns the beginning of the string up to the +position+
92
+ def to(position)
93
+ self[0..position].to_s
94
+ end
95
+
96
+ end
@@ -0,0 +1,79 @@
1
+ #--
2
+ # This file is part of Perpetual.
3
+ #
4
+ # Copyright(C) 2012 Giovanni Capuano <webmaster@giovannicapuano.net>
5
+ #
6
+ # Perpetual is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Perpetual is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with Perpetual. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+ module Crypt
20
+
21
+ # Returns the base64 encoded hash of one or more strings that will be joined.
22
+ def self.base64_encode(*s)
23
+ require 'base64'
24
+ Base64.encode64 s.join
25
+ end
26
+
27
+ # Returns the base64 decoded string of one or more hashes that will be joined.
28
+ def self.base64_decode(*h)
29
+ require 'base64'
30
+ Base64.decode64 h.join
31
+ end
32
+
33
+ # Returns the MD5 hash of one or more strings that will be joined.
34
+ def self.md5(*s)
35
+ require 'digest/md5'
36
+ Digest::MD5.hexdigest s.join
37
+ end
38
+
39
+ # Returns the SHA1 hash of one or more strings that will be joined.
40
+ def self.sha1(*s)
41
+ require 'digest/sha1'
42
+ Digest::SHA1.hexdigest s.join
43
+ end
44
+
45
+ class << self
46
+ alias_method :to_sha1, :sha1
47
+ alias_method :to_md5, :md5
48
+ alias_method :to_base64, :base64_encode
49
+ end
50
+
51
+ end
52
+
53
+ class String
54
+
55
+ # Returns the base64 encoded hash of the string.
56
+ def base64_encode
57
+ Crypt::base64_encode self
58
+ end
59
+ alias_method :to_base64, :base64_encode
60
+ alias_method :base64, :base64_encode
61
+
62
+ # Returns the base64 decoded string of the hash.
63
+ def base64_decode
64
+ Crypt::base64_decode self
65
+ end
66
+
67
+ # Returns the MD5 hash of the string.
68
+ def md5
69
+ Crypt::md5 self
70
+ end
71
+ alias_method :to_md5, :md5
72
+
73
+ # Returns the SHA1 hash of the string.
74
+ def sha1
75
+ Crypt::sha1 self
76
+ end
77
+ alias_method :to_sha1, :sha1
78
+
79
+ end
@@ -0,0 +1,34 @@
1
+ #--
2
+ # This file is part of Perpetual.
3
+ #
4
+ # Copyright(C) 2012 Giovanni Capuano <webmaster@giovannicapuano.net>
5
+ #
6
+ # Perpetual is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Perpetual is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with Perpetual. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+ module Perpetual; module String
20
+ module Miscs
21
+
22
+ # Generates an alphanumeric pseudo-random string of length +length+ (default: +10+).
23
+ def self.random(length = 10)
24
+ dictionary = (?a..?z).to_a + (?A..?Z).to_a + (?0..?9).to_a
25
+ dictionary_length = dictionary.length
26
+ ''.tap { |v|
27
+ 1.upto(length) {
28
+ v << dictionary[rand(dictionary_length-1)]
29
+ }
30
+ }
31
+ end
32
+
33
+ end
34
+ end; end
@@ -0,0 +1,23 @@
1
+ #--
2
+ # This file is part of Perpetual.
3
+ #
4
+ # Copyright(C) 2012 Giovanni Capuano <webmaster@giovannicapuano.net>
5
+ #
6
+ # Perpetual is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Perpetual is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with Perpetual. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+ module Perpetual
20
+ def self.version
21
+ '0.1.1'
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: perpetual
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Giovanni Capuano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Contains the more used methods not contained in Ruby. Just include the
14
+ classes/modules you need to use them.
15
+ email: webmaster@giovannicapuano.net
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/perpetual/enumerable.rb
21
+ - lib/perpetual/fixnum.rb
22
+ - lib/perpetual/string/crypt.rb
23
+ - lib/perpetual/string/miscs.rb
24
+ - lib/perpetual/string.rb
25
+ - lib/perpetual/version.rb
26
+ - lib/perpetual.rb
27
+ homepage: http://www.giovannicapuano.net
28
+ licenses: []
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.0.3
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Contains some useful methods.
50
+ test_files: []