make_permalink 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +17 -0
- data/lib/make_permalink.rb +2 -0
- data/lib/make_permalink/base.rb +43 -0
- data/lib/make_permalink/version.rb +3 -0
- data/make_permalink.gemspec +24 -0
- data/spec/make_permalink/base_spec.rb +44 -0
- data/spec/spec_helper.rb +37 -0
- metadata +87 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'rake'
|
6
|
+
require 'rspec'
|
7
|
+
require 'rspec/core/rake_task'
|
8
|
+
|
9
|
+
spec_files = Rake::FileList["spec/**/*_spec.rb"]
|
10
|
+
|
11
|
+
desc "Run specs"
|
12
|
+
RSpec::Core::RakeTask.new do |t|
|
13
|
+
t.rspec_opts = ["-c", "-f n", "-r ./spec/spec_helper.rb"]
|
14
|
+
t.pattern = 'spec/**/*_spec.rb'
|
15
|
+
end
|
16
|
+
|
17
|
+
task :default => :spec
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module MakePermalink
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend Base
|
5
|
+
end
|
6
|
+
|
7
|
+
def create_url_string(method, replace_non_ascii)
|
8
|
+
if replace_non_ascii
|
9
|
+
self.send(method.to_sym).to_s.to_url
|
10
|
+
else
|
11
|
+
create_non_ascii_url(method)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def create_non_ascii_url(method)
|
16
|
+
self.send(method.to_sym).gsub(/[^\w]+/,"-").downcase
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_permalink_prefix(include_id)
|
20
|
+
"#{id}-" if include_id
|
21
|
+
end
|
22
|
+
|
23
|
+
module Base
|
24
|
+
def default_options
|
25
|
+
{
|
26
|
+
:replace_non_ascii => true,
|
27
|
+
:include_id => true
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
def make_permalink(method, options = {})
|
32
|
+
define_method "permalink" do
|
33
|
+
options = self.class.default_options.merge!(options)
|
34
|
+
# Remember to remove the trailing dashes! That's the last gsub
|
35
|
+
"#{self.create_permalink_prefix(options[:include_id]).to_s +
|
36
|
+
self.create_url_string(method, options[:replace_non_ascii]).to_s}".gsub(/-$/, "")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "make_permalink/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "make_permalink"
|
7
|
+
s.version = MakePermalink::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Nicolás Hock Isaza"]
|
10
|
+
s.email = ["nhocki@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/nhocki/make_permalink"
|
12
|
+
s.summary = %q{Create dynamic permalinks from an attribute}
|
13
|
+
s.description = %q{Gem that creates a permalink based on an objects attribute. Use make_permalink :attribute to make it work}
|
14
|
+
|
15
|
+
s.rubyforge_project = "make_permalink"
|
16
|
+
|
17
|
+
s.add_dependency('stringex')
|
18
|
+
s.add_development_dependency('rspec')
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
|
+
s.require_paths = ["lib"]
|
24
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module MakePermalink
|
4
|
+
describe Base do
|
5
|
+
context "#included" do
|
6
|
+
it "creates a permalink method" do
|
7
|
+
Thingie.new("Test this thing!").should respond_to(:permalink)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "fails if the field is not defined" do
|
11
|
+
lambda { BadThingie.new.permalink }.should raise_error
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "with default options" do
|
16
|
+
it "should have cool permalinks with the object's id first" do
|
17
|
+
Thingie.new("Test this thing!").permalink.should == "1-test-this-thing"
|
18
|
+
Thingie.new("$12 Apple Pie").permalink.should == "1-12-dollars-apple-pie"
|
19
|
+
Thingie.new("Let's Rock & Roll").permalink.should == "1-lets-rock-and-roll"
|
20
|
+
Thingie.new("Simple english text").permalink.should == "1-simple-english-text"
|
21
|
+
Thingie.new("Welcome to Zombo.com").permalink.should == "1-welcome-to-zombo-dot-com"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "without the id on the permalink" do
|
26
|
+
it "should have even cooler permalinks without the object's id" do
|
27
|
+
Post.new("Welcome to the Jungle!").permalink.should == "welcome-to-the-jungle"
|
28
|
+
Post.new("It's $5 Darling!").permalink.should == "its-5-dollars-darling"
|
29
|
+
Post.new("Jekyll & Hide").permalink.should == "jekyll-and-hide"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "with non-extended-ascii characters" do
|
34
|
+
it "should have some wired permalinks" do
|
35
|
+
NonAsciiPost.new("This.should.remove.the.dots..").permalink.should == "1-this-should-remove-the-dots"
|
36
|
+
NonAsciiPost.new("Simple English Text").permalink.should == "1-simple-english-text"
|
37
|
+
NonAsciiPost.new("Let's rock & roll").permalink.should == "1-let-s-rock-roll"
|
38
|
+
NonAsciiPost.new("foo*.,/\<>%&[]{}bar").permalink.should == "1-foo-bar"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'make_permalink'
|
2
|
+
|
3
|
+
class NonAsciiPost
|
4
|
+
include MakePermalink
|
5
|
+
attr_reader :a, :id
|
6
|
+
make_permalink :a, :replace_non_ascii => false
|
7
|
+
def initialize(value)
|
8
|
+
@a = value; @id = 1
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Thingie
|
13
|
+
include MakePermalink
|
14
|
+
attr_reader :a, :id
|
15
|
+
make_permalink :a
|
16
|
+
|
17
|
+
def initialize(value)
|
18
|
+
@a = value; @id = 1
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Post
|
23
|
+
include MakePermalink
|
24
|
+
attr_reader :title, :id
|
25
|
+
make_permalink :title, :include_id => false
|
26
|
+
|
27
|
+
def initialize(value)
|
28
|
+
@title = value; @id = 1
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
class BadThingie
|
34
|
+
include MakePermalink
|
35
|
+
make_permalink :i_dont_exist
|
36
|
+
end
|
37
|
+
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: make_permalink
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- "Nicol\xC3\xA1s Hock Isaza"
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-02-15 00:00:00 -05:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: stringex
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "0"
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id002
|
38
|
+
description: Gem that creates a permalink based on an objects attribute. Use make_permalink :attribute to make it work
|
39
|
+
email:
|
40
|
+
- nhocki@gmail.com
|
41
|
+
executables: []
|
42
|
+
|
43
|
+
extensions: []
|
44
|
+
|
45
|
+
extra_rdoc_files: []
|
46
|
+
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- Rakefile
|
51
|
+
- lib/make_permalink.rb
|
52
|
+
- lib/make_permalink/base.rb
|
53
|
+
- lib/make_permalink/version.rb
|
54
|
+
- make_permalink.gemspec
|
55
|
+
- spec/make_permalink/base_spec.rb
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
has_rdoc: true
|
58
|
+
homepage: https://github.com/nhocki/make_permalink
|
59
|
+
licenses: []
|
60
|
+
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "0"
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: "0"
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project: make_permalink
|
81
|
+
rubygems_version: 1.5.2
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: Create dynamic permalinks from an attribute
|
85
|
+
test_files:
|
86
|
+
- spec/make_permalink/base_spec.rb
|
87
|
+
- spec/spec_helper.rb
|