ordinalizatron 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ log/*
6
+ *.db
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ordinalizatron.gemspec
4
+ gemspec
data/License ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2011 Piotr Marciniak
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/Readme.md ADDED
@@ -0,0 +1,35 @@
1
+ Ordinalizatron
2
+ ==========
3
+
4
+ Simple way to get ordinal numbers for your collections.
5
+
6
+ Example
7
+ -------
8
+
9
+ Without ordinalizatron:
10
+
11
+ <% User.paginate(page: 5).each_with_index do |user,index| %>
12
+ <% ordinal = get_the_offset_part_helper + 1 + index %>
13
+ <%= "#{ordinal}. #{user.name} %>
14
+ <% end %>
15
+
16
+ With ordinalizatron:
17
+
18
+ <% User.paginate(page: 5).each_with_ordinal_number do |user,ordinal_number| %>
19
+ <%= "#{ordinal_number}. #{user.name} %>
20
+ <% end %>
21
+
22
+ Custom offset (still better than adding 1 to index and then offset):
23
+
24
+ <% User.page(5).each_with_ordinal_number(offset: get_the_offset_part_helper) do |user,ordinal_number| %>
25
+ <%= "#{ordinal_number}. #{user.name} %>
26
+ <% end %>
27
+
28
+ Features
29
+ --------
30
+
31
+ Gives you easy ordinal numbers
32
+
33
+ Integrates with will_paginate
34
+
35
+ Doesn't integrate with Kaminari, but you can still use the custom offset method
@@ -0,0 +1,19 @@
1
+ require "ordinalizatron/version"
2
+
3
+ module Ordinalizatron
4
+ def each_with_ordinal_number(options = {}, &block)
5
+ options.reverse_merge!(offset: find_offset)
6
+ self.each_with_index do |element, index|
7
+ yield(element, options[:offset] + index + 1)
8
+ end
9
+ end
10
+
11
+ private
12
+ #gets the offset from WillPaginate if present
13
+ def find_offset
14
+ return self.offset if self.respond_to? :offset #detect WillPaginate
15
+ return 0
16
+ end
17
+ end
18
+
19
+ Array.send(:include, Ordinalizatron)
@@ -0,0 +1,3 @@
1
+ module Ordinalizatron
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ordinalizatron/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ordinalizatron"
7
+ s.version = Ordinalizatron::VERSION
8
+ s.authors = ["Piotr Marciniak"]
9
+ s.email = ["mandaryyyn@gmail.com"]
10
+ s.homepage = "https://github.com/Mandaryn/ordinalizatron"
11
+ s.summary = %q{Add orinal numbering to your collection}
12
+ s.description = %q{Adds each_with_ordinal_number iterator to collections.}
13
+
14
+ s.rubyforge_project = "ordinalizatron"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec", "~> 2.7"
22
+ s.add_development_dependency "rake"
23
+ s.add_development_dependency "activerecord", "~> 3.0.0"
24
+ s.add_development_dependency "will_paginate", "~> 3.0"
25
+ s.add_development_dependency "sqlite3-ruby"
26
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ordinalizatron do
4
+ describe "array" do
5
+ let(:array) { ['dupa', nil, 1, Object.new] }
6
+ it "should add proper ordinal numbers" do
7
+ ordinals = (1..4).to_a
8
+ array.each_with_ordinal_number do |element, ordinal_number|
9
+ ordinal_number.should == ordinals.shift
10
+ end
11
+ end
12
+
13
+ it "should not mess with the array elements" do
14
+ dup = array.clone
15
+ array.each_with_ordinal_number do |element, ordinal_number|
16
+ element.should == dup.shift
17
+ end
18
+ end
19
+
20
+ it "should properly apply ordinal offset" do
21
+ ordinals = (5..8).to_a
22
+ array.each_with_ordinal_number(offset: 4) do |element, ordinal_number|
23
+ ordinal_number.should == ordinals.shift
24
+ end
25
+ end
26
+
27
+ it "should try guessing the offset from predefined array methods (will_paginate integration)" do
28
+ ordinals = (4..7).to_a
29
+ array.stub(:offset).and_return(3)
30
+ array.each_with_ordinal_number do |element, ordinal_number|
31
+ ordinal_number.should == ordinals.shift
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,17 @@
1
+ require 'ordinalizatron'
2
+
3
+ RSpec.configure do |config|
4
+ config.mock_with :rspec
5
+ end
6
+
7
+ def setup_database
8
+ root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
9
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: "#{root}/ordinalizatron.db")
10
+ ActiveRecord::Base.connection.create_table(:posts) do |t|
11
+ t.string :value
12
+ end
13
+ end
14
+
15
+ def cleanup_database
16
+ ActiveRecord::Base.connection.drop_table(:posts)
17
+ end
@@ -0,0 +1,4 @@
1
+ require 'will_paginate/active_record'
2
+
3
+ class Post < ActiveRecord::Base
4
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ require 'support/will_paginate/post'
3
+
4
+ describe Ordinalizatron do
5
+ before(:all) do
6
+ setup_database
7
+ i = 0
8
+ 10.times { Post.create(value: "name#{i+=1}") }
9
+ end
10
+
11
+ after(:all) do
12
+ cleanup_database
13
+ end
14
+
15
+ describe "will_paginate array" do
16
+ let(:array) { Post.paginate(page: 2, per_page: 3) }
17
+ it "should add proper ordinal numbers to all elements" do
18
+ ordinals = [4,5,6]
19
+ array.each_with_ordinal_number do |element, ordinal_number|
20
+ ordinal_number.should == ordinals.shift
21
+ end
22
+ end
23
+
24
+ it "should not mess with original elements" do
25
+ ordinals = [4,5,6]
26
+ array.each_with_ordinal_number do |element, ordinal_number|
27
+ element.value.should == "name#{ordinals.shift}"
28
+ end
29
+ end
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ordinalizatron
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Piotr Marciniak
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2151827840 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.7'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2151827840
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &2151824560 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2151824560
36
+ - !ruby/object:Gem::Dependency
37
+ name: activerecord
38
+ requirement: &2151836180 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 3.0.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2151836180
47
+ - !ruby/object:Gem::Dependency
48
+ name: will_paginate
49
+ requirement: &2151853280 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2151853280
58
+ - !ruby/object:Gem::Dependency
59
+ name: sqlite3-ruby
60
+ requirement: &2151848540 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *2151848540
69
+ description: Adds each_with_ordinal_number iterator to collections.
70
+ email:
71
+ - mandaryyyn@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - License
79
+ - Rakefile
80
+ - Readme.md
81
+ - lib/ordinalizatron.rb
82
+ - lib/ordinalizatron/version.rb
83
+ - ordinalizatron.gemspec
84
+ - spec/ordinalizatron_spec.rb
85
+ - spec/spec_helper.rb
86
+ - spec/support/will_paginate/post.rb
87
+ - spec/will_paginate_ordinalizatron_spec.rb
88
+ homepage: https://github.com/Mandaryn/ordinalizatron
89
+ licenses: []
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project: ordinalizatron
108
+ rubygems_version: 1.8.10
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Add orinal numbering to your collection
112
+ test_files:
113
+ - spec/ordinalizatron_spec.rb
114
+ - spec/spec_helper.rb
115
+ - spec/support/will_paginate/post.rb
116
+ - spec/will_paginate_ordinalizatron_spec.rb