objects_extensions 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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 jduarte
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = objects_extensions
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 jduarte. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "objects_extensions"
8
+ gem.summary = %Q{Some convenient methods to String and Hash}
9
+ gem.description = %Q{Convenience Methods}
10
+ gem.email = "jose.fduarte@gmail.com"
11
+ gem.homepage = "http://github.com/jduarte/objects_extensions"
12
+ gem.authors = ["jduarte"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "objects_extensions #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/init.rb ADDED
File without changes
@@ -0,0 +1,14 @@
1
+ Array.class_eval do
2
+ def sel_insert(index, obj)
3
+ self.insert(index,obj) unless self.include? obj
4
+ self
5
+ end
6
+
7
+ def map_with_index!
8
+ each_with_index do |e, idx| self[idx] = yield(e, idx); end
9
+ end
10
+
11
+ def map_with_index(&block)
12
+ dup.map_with_index!(&block)
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ Hash.class_eval do
2
+ def without_id
3
+ self.delete_if {|key,val| key == "id" or key == :id}
4
+ end
5
+
6
+ def without(*args)
7
+ args.each { |arg| self.delete_if {|k,v| k == arg.to_sym or k == arg.to_s} }
8
+ self
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ require 'hash_extensions'
2
+ require 'string_extensions'
3
+ require 'array_extensions'
@@ -0,0 +1,175 @@
1
+ String.instance_eval do
2
+ def generate(length=6)
3
+ chars = 'ABCDEFGHJKLMNOPQRSTUVWXYZ23456789'
4
+ str = ''
5
+ length.times { |i| str << chars[rand(chars.length)] }
6
+ str
7
+ end
8
+ end
9
+
10
+ String.class_eval do
11
+ def delim(max_length=10,concat_at_the_end_with="...")
12
+ if self.length >= max_length
13
+ return "#{self[0,max_length - concat_at_the_end_with.length]}#{concat_at_the_end_with}"
14
+ else
15
+ return self
16
+ end
17
+ end
18
+
19
+ def split_delim(str_delim,maintain_delim=true)
20
+ arr=self.split(str_delim)
21
+ ret = []
22
+ arr.each_with_index do |subarr, i|
23
+ if i == 0
24
+ ret << "#{subarr}"
25
+ else
26
+ ret << "#{str_delim}#{subarr}"
27
+ end
28
+ end
29
+ ret
30
+ end
31
+
32
+ # If it has a string like "message_12" it gets the 12 ignoring the rest
33
+ def get_id_by_dom
34
+ self.scan(/\d+/).map { |n| n.to_i }.first
35
+ end
36
+
37
+ # Replaces all \n or \r\n for <br />
38
+ def html_text
39
+ self.gsub(/\r/, '').gsub(/\n/, '<br />')
40
+ end
41
+
42
+ # Removes first num_chars from string
43
+ # If num_chars is a negative number then it will start counting from the end
44
+ def substr(num_chars)
45
+ raise "Expected something that respond to to_i but received #{num_chars.class}" unless num_chars.respond_to?(:to_i)
46
+ if(num_chars.to_i > 0)
47
+ self[num_chars,self.length] || ""
48
+ elsif num_chars.to_i < 0
49
+ self[0,(self.length - num_chars.abs)] || ""
50
+ else
51
+ self
52
+ end
53
+ end
54
+
55
+ # chars should be an array of chars to be remove
56
+ # where can be :start or :end
57
+
58
+ def remove_chars(chars, where=:end)
59
+ original = (where == :end ? self.reverse : self)
60
+ newstr = ""
61
+ chars_ascii = chars.map {|c| c[0]}
62
+
63
+ arr_original_chars = []
64
+ original.each_char do |c|
65
+ arr_original_chars << c[0]
66
+ end
67
+
68
+ counter = 0
69
+ arr_original_chars.each do |char|
70
+ if chars_ascii.include? char
71
+ counter = counter + 1
72
+ else
73
+ break
74
+ end
75
+ end
76
+
77
+ newstr = original.substr(counter)
78
+ where == :end ? newstr.reverse : newstr
79
+ end
80
+
81
+ end
82
+
83
+
84
+
85
+
86
+
87
+
88
+
89
+
90
+
91
+ # RemoveAccents version 1.0.3 (c) 2008-2009 Solutions Informatiques Techniconseils inc.
92
+ #
93
+ # This module adds 2 methods to the string class.
94
+ # Up-to-date version and documentation available at:
95
+ #
96
+ # http://www.techniconseils.ca/en/scripts-remove-accents-ruby.php
97
+ #
98
+ # This script is available under the following license :
99
+ # Creative Commons Attribution-Share Alike 2.5.
100
+ #
101
+ # See full license and details at :
102
+ # http://creativecommons.org/licenses/by-sa/2.5/ca/
103
+ #
104
+ # Version history:
105
+ # * 1.0.3 : July 23 2009
106
+ # Corrected some incorrect character codes. Source is now wikipedia at:
107
+ # http://en.wikipedia.org/wiki/ISO/IEC_8859-1#Related_character_maps
108
+ # Thanks to Raimon Fernandez for pointing out the incorrect codes.
109
+ # * 1.0.2 : October 29 2008
110
+ # Slightly optimized version of urlize - Jonathan Grenier (jgrenier@techniconseils.ca)
111
+ # * 1.0.1 : October 29 2008
112
+ # First public revision - Jonathan Grenier (jgrenier@techniconseils.ca)
113
+ #
114
+
115
+ class String
116
+ # The extended characters map used by removeaccents. The accented characters
117
+ # are coded here using their numerical equivalent to sidestep encoding issues.
118
+ # These correspond to ISO-8859-1 encoding.
119
+ ACCENTS_MAPPING = {
120
+ 'E' => [200,201,202,203],
121
+ 'e' => [232,233,234,235],
122
+ 'A' => [192,193,194,195,196,197],
123
+ 'a' => [224,225,226,227,228,229,230],
124
+ 'C' => [199],
125
+ 'c' => [231],
126
+ 'O' => [210,211,212,213,214,216],
127
+ 'o' => [242,243,244,245,246,248],
128
+ 'I' => [204,205,206,207],
129
+ 'i' => [236,237,238,239],
130
+ 'U' => [217,218,219,220],
131
+ 'u' => [249,250,251,252],
132
+ 'N' => [209],
133
+ 'n' => [241],
134
+ 'Y' => [221],
135
+ 'y' => [253,255],
136
+ 'AE' => [306],
137
+ 'ae' => [346],
138
+ 'OE' => [188],
139
+ 'oe' => [189]
140
+ }
141
+
142
+
143
+ # Remove the accents from the string. Uses String::ACCENTS_MAPPING as the source map.
144
+ def removeaccents
145
+ str = String.new(self)
146
+ String::ACCENTS_MAPPING.each {|letter,accents|
147
+ packed = accents.pack('U*')
148
+ rxp = Regexp.new("[#{packed}]", nil, 'U')
149
+ str.gsub!(rxp, letter)
150
+ }
151
+
152
+ str
153
+ end
154
+
155
+
156
+ # Convert a string to a format suitable for a URL without ever using escaped characters.
157
+ # It calls strip, removeaccents, downcase (optional) then removes the spaces (optional)
158
+ # and finally removes any characters matching the default regexp (/[^-_A-Za-z0-9]/).
159
+ #
160
+ # Options
161
+ #
162
+ # * :downcase => call downcase on the string (defaults to true)
163
+ # * :convert_spaces => Convert space to underscore (defaults to false)
164
+ # * :regexp => The regexp matching characters that will be converting to an empty string (defaults to /[^-_A-Za-z0-9]/)
165
+ def urlize(options = {})
166
+ options[:downcase] ||= true
167
+ options[:convert_spaces] ||= false
168
+ options[:regexp] ||= /[^-_A-Za-z0-9]/
169
+
170
+ str = self.strip.removeaccents
171
+ str.downcase! if options[:downcase]
172
+ str.gsub!(/\ /,'_') if options[:convert_spaces]
173
+ str.gsub(options[:regexp], '')
174
+ end
175
+ end
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{objects_extensions}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["jduarte"]
12
+ s.date = %q{2010-04-08}
13
+ s.description = %q{Convenience Methods}
14
+ s.email = %q{jose.fduarte@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "init.rb",
27
+ "lib/array_extensions.rb",
28
+ "lib/hash_extensions.rb",
29
+ "lib/objects_extensions.rb",
30
+ "lib/string_extensions.rb",
31
+ "objects_extensions.gemspec",
32
+ "test/helper.rb",
33
+ "test/test_objects_extensions.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/jduarte/objects_extensions}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.5}
39
+ s.summary = %q{Some convenient methods to String and Hash}
40
+ s.test_files = [
41
+ "test/helper.rb",
42
+ "test/test_objects_extensions.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
56
+ end
57
+ end
58
+
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'objects_extensions'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,15 @@
1
+ require 'helper'
2
+
3
+ class TestObjectsExtensions < Test::Unit::TestCase
4
+ def test_default_delim
5
+ assert_equal "Mama mia this is a large sentence".delim(8), "Mama ..."
6
+ end
7
+
8
+ def test_more_complete_delim
9
+ assert_equal "And there goes the train".delim(8, "?!?"), "And t?!?"
10
+ end
11
+
12
+ def test_remove_accents
13
+ assert_equal "O Pé Diabético dá trabalho".removeaccents, "O Pe Diabetico da trabalho"
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: objects_extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - jduarte
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-04-08 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Convenience Methods
26
+ email: jose.fduarte@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - init.rb
42
+ - lib/array_extensions.rb
43
+ - lib/hash_extensions.rb
44
+ - lib/objects_extensions.rb
45
+ - lib/string_extensions.rb
46
+ - objects_extensions.gemspec
47
+ - test/helper.rb
48
+ - test/test_objects_extensions.rb
49
+ has_rdoc: true
50
+ homepage: http://github.com/jduarte/objects_extensions
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --charset=UTF-8
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.5
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Some convenient methods to String and Hash
77
+ test_files:
78
+ - test/helper.rb
79
+ - test/test_objects_extensions.rb