ozeias-railsbox 0.0.4

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ozéias Sant'ana
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.
@@ -0,0 +1,17 @@
1
+ # Railsbox: Package with simple and opinionated helpers for ruby / rails developer
2
+
3
+ ## Installing
4
+
5
+ # Run the following if you haven't done so before:
6
+ gem sources -a http://gems.github.com
7
+ # Install the gem:
8
+ sudo gem install ozeias-railsbox
9
+
10
+ ## Author
11
+
12
+ * [Ozéias Sant'ana](http://railsbox.org)
13
+ * [Recommend me on Working With Rails](http://www.workingwithrails.com/recommendation/new/person/8228-oz-ias-sant-ana)
14
+
15
+ ## Copyright
16
+
17
+ Copyright (c) 2009 Ozéias Sant'ana. See LICENSE for details.
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 4
3
+ :major: 0
4
+ :minor: 0
@@ -0,0 +1,9 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module Railsbox
5
+ VERSION = '0.0.4'
6
+ end
7
+
8
+ require 'railsbox/core_ext/array'
9
+ require 'railsbox/core_ext/enumerable'
@@ -0,0 +1,40 @@
1
+ class Array
2
+ # Returns the natural order of the array
3
+ # The problem is that the sort method from the Array class cannot "see" the embedded numbers.
4
+ # %w( foobar a2 a30 a3 a20 a13bb20 ).sort # => %w( a13bb20 a2 a20 a3 a30 foobar)
5
+ # with natural_sort
6
+ # %w( foobar a2 a30 a3 a20 a13bb20 ).natural_sort # => %w( a2 a3 a13bb20 a20 a30 foobar )
7
+ # with block
8
+ # obj = Obj.find(:all)
9
+ # obj.natural_sort { |o| o.name }
10
+ #
11
+ # - author => Ozéias Sant'ana - railsbox.org - oz.santana@gmail.com
12
+ def natural_sort(&block)
13
+ reg_number = /\d+/
14
+
15
+ self.sort do |str1, str2|
16
+
17
+ # Get string in block
18
+ if block_given?
19
+ str1 = yield str1
20
+ str2 = yield str2
21
+ end
22
+
23
+ min = lambda { |a,b| a < b ? a : b }
24
+
25
+ # Split the strings into digits and non-digits
26
+ strArrays = [str1,str2].inject(Array.new) do |arr, str|
27
+ arr << str.tr(" \t\r\n", '').split(/(\d+)/)
28
+ end
29
+
30
+ # Loop through all the digit parts and convert to integers if neither of them begin with a zero
31
+ 1.step(min.call(strArrays[0].size, strArrays[1].size)-1, 2) do |idx|
32
+ if (strArrays[0][idx] !~ /^0/) and (strArrays[1][idx] !~ /^0/)
33
+ strArrays.each { |arr| arr[idx] = arr[idx].to_i }
34
+ end
35
+ end
36
+
37
+ strArrays[0] <=> strArrays[1]
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,12 @@
1
+ module Enumerable
2
+ # Alternative Block execution on empty Enumerable
3
+ # e.g., <% results.each do |x| %>
4
+ # <li>hit:<%=x-%></li>
5
+ # <% end.else do %>
6
+ # <li>no hits found</li>
7
+ # <% end %>
8
+ # - author => http://blog.sponagl.de/2009/1/23/alternative-block-execution-on-empty-enumerable
9
+ def else(&block)
10
+ self.respond_to?('empty?') && self.empty? ? yield : self
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Array" do
4
+ before do
5
+ @array = %w( foobar a2 a30 a3 a20 a13bb20 )
6
+ @block = [{ :name => 'foobar' } , { :name => 'a2' }, { :name => 'a30' }, { :name => 'a3' }, { :name => 'a20' }, { :name => 'a13bb20' }]
7
+ end
8
+
9
+ it "should array with block in natural order" do
10
+ @block.natural_sort { |h| h[:name] }.should == [{ :name => 'a2' } , { :name => 'a3' }, { :name => 'a13bb20' }, { :name => 'a20' }, { :name => 'a30' }, { :name => 'foobar' }]
11
+ end
12
+
13
+ it "should array in natural order" do
14
+ @array.natural_sort.should == %w( a2 a3 a13bb20 a20 a30 foobar )
15
+ end
16
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Enumerable" do
4
+ before do
5
+ @hash = { :one => 1, :two => 2, :three => 3, :four => 4 }
6
+ @array = [1, 2, 3, 4]
7
+ end
8
+
9
+ it "should hash" do
10
+ create_hash(@hash).should == @hash
11
+ end
12
+
13
+ it "should message with empty hash" do
14
+ create_hash({}).should == 'no hits found'
15
+ end
16
+
17
+ it "should array" do
18
+ create_array(@array).should == @array
19
+ end
20
+
21
+ it "should message with empty array" do
22
+ create_array([]).should == 'no hits found'
23
+ end
24
+
25
+ private
26
+ def create_hash(hash)
27
+ hash.each do |x|
28
+ end.else do
29
+ return 'no hits found'
30
+ end
31
+ hash
32
+ end
33
+
34
+ def create_array(array)
35
+ array.each do |x|
36
+ end.else do
37
+ return 'no hits found'
38
+ end
39
+ array
40
+ end
41
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Railsbox" do
4
+ # it "fails" do
5
+ # fail "hey buddy, you should probably rename this file and start specing for real"
6
+ # end
7
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 'railsbox'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ozeias-railsbox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - "Oz\xC3\xA9ias Sant'ana"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-17 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Simple and opinionated helpers for ruby / rails developer
17
+ email: oz.santana@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.markdown
26
+ - VERSION.yml
27
+ - lib/railsbox
28
+ - lib/railsbox/core_ext
29
+ - lib/railsbox/core_ext/array.rb
30
+ - lib/railsbox/core_ext/enumerable.rb
31
+ - lib/railsbox.rb
32
+ - spec/array_spec.rb
33
+ - spec/enumerable_spec.rb
34
+ - spec/railsbox_spec.rb
35
+ - spec/spec_helper.rb
36
+ - LICENSE
37
+ has_rdoc: false
38
+ homepage: http://github.com/ozeias/railsbox
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --inline-source
42
+ - --charset=UTF-8
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.2.0
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Simple and opinionated helpers for ruby / rails developer
64
+ test_files: []
65
+