pagify 0.7.1 → 0.8.0

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.
@@ -0,0 +1 @@
1
+ *.rbc
@@ -0,0 +1,265 @@
1
+
2
+ require 'pathname'
3
+
4
+ module Gemgem
5
+ class << self
6
+ attr_accessor :dir, :spec
7
+ end
8
+
9
+ module_function
10
+ def create
11
+ yield(spec = Gem::Specification.new{ |s|
12
+ s.authors = ['Lin Jen-Shin (godfat)']
13
+ s.email = ['godfat (XD) godfat.org']
14
+
15
+ s.description = description.join
16
+ s.summary = description.first
17
+
18
+ s.rubygems_version = Gem::VERSION
19
+ s.date = Time.now.strftime('%Y-%m-%d')
20
+ s.files = gem_files
21
+ s.test_files = gem_files.grep(%r{^test/(.+?/)*test_.+?\.rb$})
22
+ s.executables = Dir['bin/*'].map{ |f| File.basename(f) }
23
+ s.require_paths = %w[lib]
24
+ })
25
+ spec.homepage ||= "https://github.com/godfat/#{spec.name}"
26
+ spec
27
+ end
28
+
29
+ def readme
30
+ path = %w[README.md README].find{ |name|
31
+ File.exist?("#{Gemgem.dir}/#{name}")
32
+ }
33
+ @readme ||=
34
+ if path
35
+ ps = File.read(path).scan(/#+[^\n]+\n\n.+?(?=\n\n#+[^\n]+\n)/m)
36
+ ps.inject({'HEADER' => ps.first}){ |r, s, i|
37
+ r[s[/\w+/]] = s
38
+ r
39
+ }
40
+ else
41
+ {}
42
+ end
43
+ end
44
+
45
+ def description
46
+ @description ||= (readme['DESCRIPTION']||'').sub(/.+\n\n/, '').lines.to_a
47
+ end
48
+
49
+ def changes
50
+ path = %w[CHANGES.md CHANGES].find{ |name|
51
+ File.exist?("#{Gemgem.dir}/#{name}")
52
+ }
53
+ @changes ||=
54
+ if path
55
+ date = '\d+{4}\-\d+{2}\-\d{2}'
56
+ File.read(path).match(
57
+ /([^\n]+#{date}\n\n(.+?))(?=\n\n[^\n]+#{date}\n|\Z)/m)[1]
58
+ else
59
+ ''
60
+ end
61
+ end
62
+
63
+ def ann_md
64
+ "##{readme['HEADER'].sub(/([\w\-]+)/, "[\\1](#{spec.homepage})")}\n\n" \
65
+ "##{readme['DESCRIPTION'][/[^\n]+\n\n[^\n]+/]}\n\n" \
66
+ "### CHANGES:\n\n" \
67
+ "###{changes}\n\n" \
68
+ "##{readme['INSTALLATION']}\n\n" +
69
+ if readme['SYNOPSIS'] then "##{readme['SYNOPSIS']}" else '' end
70
+ end
71
+
72
+ def ann_html
73
+ gem 'nokogiri'
74
+ gem 'kramdown'
75
+
76
+ IO.popen('kramdown', 'r+') do |md|
77
+ md.puts Gemgem.ann_md
78
+ md.close_write
79
+ require 'nokogiri'
80
+ html = Nokogiri::XML.parse("<gemgem>#{md.read}</gemgem>")
81
+ html.css('*').each{ |n| n.delete('id') }
82
+ html.root.children.to_html
83
+ end
84
+ end
85
+
86
+ def ann_email
87
+ "#{readme['HEADER'].sub(/([\w\-]+)/, "\\1 <#{spec.homepage}>")}\n\n" \
88
+ "#{readme['DESCRIPTION']}\n\n" \
89
+ "#{readme['INSTALLATION']}\n\n" +
90
+ if readme['SYNOPSIS'] then "##{readme['SYNOPSIS']}\n\n" else '' end +
91
+ "## CHANGES:\n\n" \
92
+ "##{changes}\n\n"
93
+ end
94
+
95
+ def gem_tag
96
+ "#{spec.name}-#{spec.version}"
97
+ end
98
+
99
+ def write
100
+ File.open("#{dir}/#{spec.name}.gemspec", 'w'){ |f|
101
+ f << split_lines(spec.to_ruby) }
102
+ end
103
+
104
+ def split_lines ruby
105
+ ruby.gsub(/(.+?)\[(.+?)\]/){ |s|
106
+ if $2.index(',')
107
+ "#{$1}[\n #{$2.split(',').map(&:strip).join(",\n ")}]"
108
+ else
109
+ s
110
+ end
111
+ }
112
+ end
113
+
114
+ def all_files
115
+ @all_files ||= find_files(Pathname.new(dir)).map{ |file|
116
+ if file.to_s =~ %r{\.git/}
117
+ nil
118
+ else
119
+ file.to_s
120
+ end
121
+ }.compact.sort
122
+ end
123
+
124
+ def gem_files
125
+ @gem_files ||= all_files - ignored_files
126
+ end
127
+
128
+ def ignored_files
129
+ @ignored_file ||= all_files.select{ |path| ignore_patterns.find{ |ignore|
130
+ path =~ ignore && !git_files.include?(path)}}
131
+ end
132
+
133
+ def git_files
134
+ @git_files ||= if File.exist?("#{dir}/.git")
135
+ `git ls-files`.split("\n")
136
+ else
137
+ []
138
+ end
139
+ end
140
+
141
+ # protected
142
+ def find_files path
143
+ path.children.select(&:file?).map{|file| file.to_s[(dir.size+1)..-1]} +
144
+ path.children.select(&:directory?).map{|dir| find_files(dir)}.flatten
145
+ end
146
+
147
+ def ignore_patterns
148
+ @ignore_files ||= expand_patterns(
149
+ gitignore.split("\n").reject{ |pattern|
150
+ pattern.strip == ''
151
+ }).map{ |pattern| %r{^([^/]+/)*?#{Regexp.escape(pattern)}(/[^/]+)*?$} }
152
+ end
153
+
154
+ def expand_patterns pathes
155
+ pathes.map{ |path|
156
+ if path !~ /\*/
157
+ path
158
+ else
159
+ expand_patterns(
160
+ Dir[path] +
161
+ Pathname.new(File.dirname(path)).children.select(&:directory?).
162
+ map{ |prefix| "#{prefix}/#{File.basename(path)}" })
163
+ end
164
+ }.flatten
165
+ end
166
+
167
+ def gitignore
168
+ if File.exist?(path = "#{dir}/.gitignore")
169
+ File.read(path)
170
+ else
171
+ ''
172
+ end
173
+ end
174
+ end
175
+
176
+ namespace :gem do
177
+
178
+ desc 'Install gem'
179
+ task :install => [:build] do
180
+ sh("#{Gem.ruby} -S gem install pkg/#{Gemgem.gem_tag}")
181
+ end
182
+
183
+ desc 'Build gem'
184
+ task :build => [:spec] do
185
+ sh("#{Gem.ruby} -S gem build #{Gemgem.spec.name}.gemspec")
186
+ sh("mkdir -p pkg")
187
+ sh("mv #{Gemgem.gem_tag}.gem pkg/")
188
+ end
189
+
190
+ desc 'Release gem'
191
+ task :release => [:spec, :check, :build] do
192
+ sh("git tag #{Gemgem.gem_tag}")
193
+ sh("git push")
194
+ sh("git push --tags")
195
+ sh("#{Gem.ruby} -S gem push pkg/#{Gemgem.gem_tag}.gem")
196
+ end
197
+
198
+ task :check do
199
+ ver = Gemgem.spec.version.to_s
200
+
201
+ if ENV['VERSION'].nil?
202
+ puts("\e[35mExpected " \
203
+ "\e[33mVERSION\e[35m=\e[33m#{ver}\e[0m")
204
+ exit(1)
205
+
206
+ elsif ENV['VERSION'] != ver
207
+ puts("\e[35mExpected \e[33mVERSION\e[35m=\e[33m#{ver} " \
208
+ "\e[35mbut got\n " \
209
+ "\e[33mVERSION\e[35m=\e[33m#{ENV['VERSION']}\e[0m")
210
+ exit(2)
211
+ end
212
+ end
213
+
214
+ end # of gem namespace
215
+
216
+ desc 'Run tests in memory'
217
+ task :test do
218
+ require 'bacon'
219
+ Bacon.extend(Bacon::TestUnitOutput)
220
+ Bacon.summary_on_exit
221
+ $LOAD_PATH.unshift('lib')
222
+ Dir['./test/**/test_*.rb'].each{ |file| require file[0..-4] }
223
+ end
224
+
225
+ desc 'Run tests with shell'
226
+ task 'test:shell', :RUBY_OPTS do |t, args|
227
+ files = Dir['test/**/test_*.rb'].join(' ')
228
+
229
+ cmd = [Gem.ruby, args[:RUBY_OPTS],
230
+ '-I', 'lib', '-S', 'bacon', '--quiet', files]
231
+
232
+ sh(cmd.compact.join(' '))
233
+ end
234
+
235
+ desc 'Generate ann markdown'
236
+ task 'ann:md' => ['gem:spec'] do
237
+ puts Gemgem.ann_md
238
+ end
239
+
240
+ desc 'Generate ann html'
241
+ task 'ann:html' => ['gem:spec'] do
242
+ puts Gemgem.ann_html
243
+ end
244
+
245
+ desc 'Generate ann email'
246
+ task 'ann:email' => ['gem:spec'] do
247
+ puts Gemgem.ann_email
248
+ end
249
+
250
+ desc 'Generate rdoc'
251
+ task :doc => ['gem:spec'] do
252
+ sh("yardoc -o rdoc --main README.md" \
253
+ " --files #{Gemgem.spec.extra_rdoc_files.join(',')}")
254
+ end
255
+
256
+ desc 'Remove ignored files'
257
+ task :clean => ['gem:spec'] do
258
+ trash = "~/.Trash/#{Gemgem.spec.name}/"
259
+ sh "mkdir -p #{trash}" unless File.exist?(File.expand_path(trash))
260
+ Gemgem.ignored_files.each{ |file| sh "mv #{file} #{trash}" }
261
+ end
262
+
263
+ task :default do
264
+ puts `#{Gem.ruby} -S #{$PROGRAM_NAME} -T`
265
+ end
@@ -13,12 +13,5 @@ TestCase = begin
13
13
  require 'pagify'
14
14
  require 'dm-core'
15
15
  require 'dm-aggregates'
16
+ require 'dm-migrations'
16
17
  require 'active_record'
17
-
18
- if RUBY_VERSION < '1.8.7'
19
- class Symbol
20
- def to_proc
21
- lambda{ |*args| args.shift.__send__(self, *args) }
22
- end
23
- end
24
- end
@@ -10,14 +10,14 @@ module WebCase
10
10
  end
11
11
 
12
12
  def test_pagify_links
13
- assert_equal('<div class="pagination"><a href="/a/b?page=2">&lt; Previous</a> <a href="/a/b?page=4">Next &gt;</a><br /><a href="/a/b?page=1">&laquo; First</a> <a href="/a/b?page=2">2</a> 3 <a href="/a/b?page=4">4</a> <a href="/a/b?page=5">Last &raquo;</a></div>', pagify_links(@data))
13
+ assert_equal('<div class="pagination"><a href="/a/b?page=2" class="pagination_inactive">&lt; Previous</a> <a href="/a/b?page=4" class="pagination_inactive">Next &gt;</a><br/><a href="/a/b?page=1" class="pagination_inactive">&laquo; First</a> <a href="/a/b?page=2" class="pagination_inactive">2</a> <span class="pagination_active">3</span> <a href="/a/b?page=4" class="pagination_inactive">4</a> <a href="/a/b?page=5" class="pagination_inactive">Last &raquo;</a></div>', pagify_links(@data))
14
14
 
15
15
  @data.pager.html.setting[:links_type] = :links
16
16
 
17
- assert_equal('<div class="pagination"><a href="/a/b?page=1">&laquo; First</a> <a href="/a/b?page=2">2</a> 3 <a href="/a/b?page=4">4</a> <a href="/a/b?page=5">Last &raquo;</a></div>', pagify_links(@data))
17
+ assert_equal('<div class="pagination"><a href="/a/b?page=1" class="pagination_inactive">&laquo; First</a> <a href="/a/b?page=2" class="pagination_inactive">2</a> <span class="pagination_active">3</span> <a href="/a/b?page=4" class="pagination_inactive">4</a> <a href="/a/b?page=5" class="pagination_inactive">Last &raquo;</a></div>', pagify_links(@data))
18
18
  end
19
19
 
20
20
  def test_custom_path
21
- assert_equal('<div class="pagination"><a href="XD?page=2">&lt; Previous</a> <a href="XD?page=4">Next &gt;</a><br /><a href="XD?page=1">&laquo; First</a> <a href="XD?page=2">2</a> 3 <a href="XD?page=4">4</a> <a href="XD?page=5">Last &raquo;</a></div>', pagify_links(@data){'XD'})
21
+ assert_equal('<div class="pagination"><a href="XD?page=2" class="pagination_inactive">&lt; Previous</a> <a href="XD?page=4" class="pagination_inactive">Next &gt;</a><br/><a href="XD?page=1" class="pagination_inactive">&laquo; First</a> <a href="XD?page=2" class="pagination_inactive">2</a> <span class="pagination_active">3</span> <a href="XD?page=4" class="pagination_inactive">4</a> <a href="XD?page=5" class="pagination_inactive">Last &raquo;</a></div>', pagify_links(@data){'XD'})
22
22
  end
23
23
  end
@@ -1,10 +1,28 @@
1
1
 
2
- require 'test/helper'
3
- require 'test/helper_pagify'
4
- require 'test/helper_model'
2
+ if respond_to?(:require_relative, true)
3
+ require_relative 'helper'
4
+ require_relative 'helper_pagify'
5
+ require_relative 'helper_model'
6
+ else
7
+ require 'test/helper'
8
+ require 'test/helper_pagify'
9
+ require 'test/helper_model'
10
+ end
5
11
 
6
12
  require 'pagify/active_record'
7
13
 
14
+ require 'tempfile'
15
+ db = Tempfile.new('pagify')
16
+ db.close
17
+
18
+ require 'sqlite3/sqlite3_native'
19
+ require 'sqlite3'
20
+
21
+ DataMapper.setup(:active_record, "sqlite3:#{db.path}")
22
+
23
+ ActiveRecord::Base.establish_connection(
24
+ :adapter => 'sqlite3', :database => db.path)
25
+
8
26
  class TestActiveRecord < TestCase
9
27
  include PagifyCase
10
28
  def test_for_active_record
@@ -22,8 +40,6 @@ class TestActiveRecord < TestCase
22
40
  end
23
41
  end
24
42
 
25
- DataMapper.setup(:active_record, 'sqlite3:tmp/active_record.sqlite3')
26
-
27
43
  class UserForActiveRecord
28
44
  include DataMapper::Resource
29
45
  def self.default_repository_name
@@ -47,9 +63,6 @@ class TestActiveRecord < TestCase
47
63
  auto_migrate!
48
64
  end
49
65
 
50
- ActiveRecord::Base.establish_connection(
51
- :adapter => 'sqlite3', :database => 'tmp/active_record.sqlite3')
52
-
53
66
  class User < ActiveRecord::Base
54
67
  has_many :pets
55
68
  end
@@ -63,5 +76,4 @@ class TestActiveRecord < TestCase
63
76
  def all_pets_with_name_godfat user
64
77
  user.pets.all(:conditions => ['name = ?', 'godfat'])
65
78
  end
66
-
67
79
  end
@@ -1,6 +1,11 @@
1
1
 
2
- require 'test/helper'
3
- require 'test/helper_pagify'
2
+ if respond_to?(:require_relative, true)
3
+ require_relative 'helper'
4
+ require_relative 'helper_pagify'
5
+ else
6
+ require 'test/helper'
7
+ require 'test/helper_pagify'
8
+ end
4
9
 
5
10
  class TestArray < TestCase
6
11
  include PagifyCase
@@ -1,6 +1,11 @@
1
1
 
2
- require 'test/helper'
3
- require 'test/helper_pagify'
2
+ if respond_to?(:require_relative, true)
3
+ require_relative 'helper'
4
+ require_relative 'helper_pagify'
5
+ else
6
+ require 'test/helper'
7
+ require 'test/helper_pagify'
8
+ end
4
9
 
5
10
  class TestBasic < TestCase
6
11
  include PagifyCase
@@ -1,7 +1,14 @@
1
1
 
2
- require 'test/helper'
3
- require 'test/helper_pagify'
4
- require 'test/helper_model'
2
+
3
+ if respond_to?(:require_relative, true)
4
+ require_relative 'helper'
5
+ require_relative 'helper_pagify'
6
+ require_relative 'helper_model'
7
+ else
8
+ require 'test/helper'
9
+ require 'test/helper_pagify'
10
+ require 'test/helper_model'
11
+ end
5
12
 
6
13
  require 'pagify/data_mapper'
7
14
 
@@ -1,5 +1,9 @@
1
1
 
2
- require 'test/helper'
2
+ if respond_to?(:require_relative, true)
3
+ require_relative 'helper'
4
+ else
5
+ require 'test/helper'
6
+ end
3
7
 
4
8
  require 'pagify/helper/html'
5
9
 
@@ -52,8 +56,9 @@ class TestHTML < TestCase
52
56
 
53
57
  pager = Pagify::ArrayPager.new((1..1000).to_a, :per_page => 10)
54
58
  users = pager[50]
59
+ pager.html.setting[:class] = nil
55
60
 
56
- assert_equal(#'<a href="49">&lt; Previous</a> 50 <a href="51">Next &gt;</a><br />'+
61
+ assert_equal(#'<a href="49">&lt; Previous</a> 50 <a href="51">Next &gt;</a><br/>'+
57
62
  '<a href="1">&laquo; First</a> ' +
58
63
  '<a href="2">2</a> ' + # outer links
59
64
  # '... ' +
@@ -66,7 +71,7 @@ class TestHTML < TestCase
66
71
  '<a href="47">47</a> ' +
67
72
  '<a href="48">48</a> ' +
68
73
  '<a href="49">49</a> ' +
69
- '50 ' +
74
+ '<span class="pagination_active">50</span> ' +
70
75
  '<a href="51">51</a> ' +
71
76
  '<a href="52">52</a> ' +
72
77
  '<a href="53">53</a> ' +
@@ -89,7 +94,7 @@ class TestHTML < TestCase
89
94
  pager.html.setting[:ellipsis] = 'zzz'
90
95
  users = pager[50]
91
96
 
92
- assert_equal(#'<a href="49">&lt; Previous</a> 50 <a href="51">Next &gt;</a><br />'+
97
+ assert_equal(#'<a href="49">&lt; Previous</a> 50 <a href="51">Next &gt;</a><br/>'+
93
98
  '<a href="1" class="pagify">&laquo; First</a>,,' +
94
99
  '<a href="2" class="pagify">2</a>,,' + # outer links
95
100
  'zzz,,' +
@@ -97,7 +102,7 @@ class TestHTML < TestCase
97
102
  '<a href="47" class="pagify">47</a>,,' +
98
103
  '<a href="48" class="pagify">48</a>,,' +
99
104
  '<a href="49" class="pagify">49</a>,,' +
100
- '50,,' +
105
+ '<span class="pagination_active">50</span>,,' +
101
106
  '<a href="51" class="pagify">51</a>,,' +
102
107
  '<a href="52" class="pagify">52</a>,,' +
103
108
  '<a href="53" class="pagify">53</a>,,' +
@@ -139,8 +144,11 @@ class TestHTML < TestCase
139
144
 
140
145
  def test_2_pages
141
146
  pager = Pagify::ArrayPager.new([1,2,3], :per_page => 2)
142
- assert_equal '&laquo; First <a href="2">Last &raquo;</a>', pager.html.links(1, &:to_s)
143
- assert_equal '<a href="1">&laquo; First</a> Last &raquo;', pager.html.links(2, &:to_s)
147
+ pager.html.setting[:inactive_class] = nil
148
+ pager.html.setting[:active_class] = nil
149
+
150
+ assert_equal '<span class="">&laquo; First</span> <a href="2">Last &raquo;</a>', pager.html.links(1, &:to_s)
151
+ assert_equal '<a href="1">&laquo; First</a> <span class="">Last &raquo;</span>', pager.html.links(2, &:to_s)
144
152
 
145
153
  assert_equal '<a href="2">Next &gt;</a>', pager.html.links_navigate(1, &:to_s)
146
154
  assert_equal '<a href="1">&lt; Previous</a>', pager.html.links_navigate(2, &:to_s)
@@ -148,14 +156,16 @@ class TestHTML < TestCase
148
156
 
149
157
  def test_3_pages
150
158
  pager = Pagify::ArrayPager.new([1,2,3,4,5], :per_page => 2)
159
+ pager.html.setting[:class] = nil
160
+ pager.html.setting[:active_class] = 'a'
151
161
 
152
- assert_equal '&laquo; First <a href="2">2</a> <a href="3">Last &raquo;</a>',
162
+ assert_equal '<span class="a">&laquo; First</span> <a href="2">2</a> <a href="3">Last &raquo;</a>',
153
163
  pager.html.links(1, &:to_s)
154
164
 
155
- assert_equal '<a href="1">&laquo; First</a> 2 <a href="3">Last &raquo;</a>',
165
+ assert_equal '<a href="1">&laquo; First</a> <span class="a">2</span> <a href="3">Last &raquo;</a>',
156
166
  pager.html.links(2, &:to_s)
157
167
 
158
- assert_equal '<a href="1">&laquo; First</a> <a href="2">2</a> Last &raquo;',
168
+ assert_equal '<a href="1">&laquo; First</a> <a href="2">2</a> <span class="a">Last &raquo;</span>',
159
169
  pager.html.links(3, &:to_s)
160
170
 
161
171
  assert_equal '<a href="2">Next &gt;</a>', pager.html.links_navigate(1, &:to_s)
@@ -168,19 +178,27 @@ class TestHTML < TestCase
168
178
 
169
179
  def test_more_pages_to_left_or_right
170
180
  pager = Pagify::ArrayPager.new((1..33).to_a, :per_page => 3)
181
+ pager.html.setting[:first_text] = nil
182
+ pager.html.setting[:last_text] = nil
183
+ pager.html.setting[:class] = nil
184
+ pager.html.setting[:active_class] = 'g'
171
185
 
172
- first = ['&laquo; First', '<a href="1">&laquo; First</a>']
173
- last = ['Last &raquo;', '<a href="11">Last &raquo;</a>']
186
+ first = ['<span class="g">1</span>', '<a href="1">1</a>']
187
+ last = ['<span class="g">11</span>', '<a href="11">11</a>']
174
188
 
175
189
  (1..11).each{ |page|
176
- expected = (2..10).map{ |i| i == page ? i.to_s : "<a href=\"#{i}\">#{i}</a>" }
190
+ expected = (2..10).map{ |i|
191
+ if i == page
192
+ %Q{<span class="g">#{i}</span>}
193
+ else
194
+ %Q{<a href="#{i}">#{i}</a>}
195
+ end
196
+ }
177
197
 
178
198
  expected.unshift( page == 1 ? first.first : first.last )
179
199
  expected.push( page == 11 ? last.first : last.last )
180
200
 
181
201
  assert_equal expected.join(' '), pager.html.links(page, &:to_s)
182
202
  }
183
-
184
203
  end
185
-
186
204
  end