jeremyboles-dm-is-sluggable 0.9.8

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1 @@
1
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 John Doe
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/Manifest.txt ADDED
@@ -0,0 +1,12 @@
1
+ History.txt
2
+ LICENSE
3
+ Manifest.txt
4
+ README.markdown
5
+ Rakefile
6
+ TODO
7
+ lib/dm-is-sluggable.rb
8
+ lib/dm-is-sluggable/is/sluggable.rb
9
+ lib/dm-is-sluggable/is/version.rb
10
+ spec/integration/sluggable_spec.rb
11
+ spec/spec.opts
12
+ spec/spec_helper.rb
data/README.markdown ADDED
@@ -0,0 +1,35 @@
1
+ # dm-is-sluggble #
2
+
3
+ DataMapper plugin that allows records to be found as a slug, mostly
4
+ used in pretty urls.
5
+
6
+
7
+ ## Example DataMapper resource (i.e. model) ##
8
+
9
+ # /app/models/post.rb
10
+ class Post
11
+ include DataMapper::Resource
12
+
13
+ property :id, Serial
14
+ property :slug, String
15
+
16
+ is :sluggable
17
+
18
+ end
19
+
20
+ @post = Post.create
21
+ @post.slug # => "@1"
22
+ Post.by_slug # => @post
23
+
24
+ @post.update_attributes(:slug => 'some-slug-here')
25
+ @post.slug # => "some-slug-here"
26
+ Post.by_slug # => @post
27
+
28
+ ## Example Merb route ##
29
+
30
+ # /config/routes.rb
31
+ identify(Post => :slug) do
32
+ resources :posts
33
+ end
34
+
35
+ # url(:post, 1) => /posts/some-slug-here
data/README.txt ADDED
@@ -0,0 +1,5 @@
1
+ = dm-is-sluggable
2
+
3
+ == DESCRIPTION:
4
+
5
+ See README.markdown
data/Rakefile ADDED
@@ -0,0 +1,102 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'spec/rake/spectask'
4
+ require 'pathname'
5
+
6
+ ROOT = Pathname(__FILE__).dirname.expand_path
7
+ require ROOT + 'lib/dm-is-sluggable/is/version'
8
+
9
+ AUTHOR = "Jeremy Boles"
10
+ EMAIL = "jeremy [a] jeremyboles [d] com"
11
+ GEM_NAME = "dm-is-sluggable"
12
+ GEM_VERSION = DataMapper::Is::Sluggable::VERSION
13
+ GEM_DEPENDENCIES = [["dm-core", GEM_VERSION]]
14
+ GEM_CLEAN = ["log", "pkg"]
15
+ GEM_EXTRAS = { :has_rdoc => true, :extra_rdoc_files => %w[ README.txt LICENSE TODO ] }
16
+
17
+ PROJECT_NAME = "datamapper"
18
+ PROJECT_URL = "http://github.com/jeremyboles/dm-is-sluggable/tree/master"
19
+ PROJECT_DESCRIPTION = PROJECT_SUMMARY = "Simple Datamapper plugin for referencing Resources by 'URL slugs'"
20
+
21
+ require 'hoe'
22
+
23
+ @config_file = "~/.rubyforge/user-config.yml"
24
+ @config = nil
25
+ RUBYFORGE_USERNAME = "unknown"
26
+ def rubyforge_username
27
+ unless @config
28
+ begin
29
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
30
+ rescue
31
+ puts <<-EOS
32
+ ERROR: No rubyforge config file found: #{@config_file}
33
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
34
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
35
+ EOS
36
+ exit
37
+ end
38
+ end
39
+ RUBYFORGE_USERNAME.replace @config["username"]
40
+ end
41
+
42
+ # Remove hoe dependency
43
+ class Hoe
44
+ def extra_dev_deps
45
+ @extra_dev_deps.reject! { |dep| dep[0] == "hoe" }
46
+ @extra_dev_deps
47
+ end
48
+ end
49
+
50
+ hoe = Hoe.new(GEM_NAME, GEM_VERSION) do |p|
51
+
52
+ p.developer(AUTHOR, EMAIL)
53
+
54
+ p.description = PROJECT_DESCRIPTION
55
+ p.summary = PROJECT_SUMMARY
56
+ p.url = PROJECT_URL
57
+
58
+ p.rubyforge_name = PROJECT_NAME if PROJECT_NAME
59
+
60
+ p.clean_globs |= GEM_CLEAN
61
+ p.spec_extras = GEM_EXTRAS if GEM_EXTRAS
62
+
63
+ GEM_DEPENDENCIES.each do |dep|
64
+ p.extra_deps << dep
65
+ end
66
+
67
+ end
68
+
69
+ task :default => [ :spec ]
70
+
71
+ WIN32 = (RUBY_PLATFORM =~ /win32|mingw|cygwin/) rescue nil
72
+ SUDO = WIN32 ? '' : ('sudo' unless ENV['SUDOLESS'])
73
+
74
+ desc "Install #{GEM_NAME} #{GEM_VERSION}"
75
+ task :install => [ :package ] do
76
+ sh "#{SUDO} gem install --local pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources", :verbose => false
77
+ end
78
+
79
+ desc "Uninstall #{GEM_NAME} #{GEM_VERSION} (default ruby)"
80
+ task :uninstall => [ :clobber ] do
81
+ sh "#{SUDO} gem uninstall #{GEM_NAME} -v#{GEM_VERSION} -I -x", :verbose => false
82
+ end
83
+
84
+ task :cultivate do
85
+ system "touch Manifest.txt; rake check_manifest | grep -v \"(in \" | patch"
86
+ system "rake debug_gem | grep -v \"(in \" > `basename \\`pwd\\``.gemspec"
87
+ end
88
+
89
+ desc 'Run specifications'
90
+ Spec::Rake::SpecTask.new(:spec) do |t|
91
+ t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
92
+ t.spec_files = Pathname.glob((ROOT + 'spec/**/*_spec.rb').to_s)
93
+
94
+ begin
95
+ t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true
96
+ t.rcov_opts << '--exclude' << 'spec'
97
+ t.rcov_opts << '--text-summary'
98
+ t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
99
+ rescue Exception
100
+ # rcov not installed
101
+ end
102
+ end
data/TODO ADDED
@@ -0,0 +1,2 @@
1
+ TODO
2
+ ====
@@ -0,0 +1,19 @@
1
+ # Needed to import datamapper and other gems
2
+ require 'rubygems'
3
+ require 'pathname'
4
+
5
+ # Add all external dependencies for the plugin here
6
+ gem 'dm-core', '~>0.9.7'
7
+ require 'dm-core'
8
+
9
+ # Require plugin-files
10
+ require Pathname(__FILE__).dirname.expand_path / 'dm-is-sluggable' / 'is' / 'sluggable.rb'
11
+
12
+ # Include the plugin in Resource
13
+ module DataMapper
14
+ module Resource
15
+ module ClassMethods
16
+ include DataMapper::Is::Sluggable
17
+ end # module ClassMethods
18
+ end # module Resource
19
+ end # module DataMapper
@@ -0,0 +1,37 @@
1
+ module DataMapper
2
+ module Is
3
+ module Sluggable
4
+
5
+ def is_sluggable(options={})
6
+ options = { :identifier => '@', :on => :slug, :splitter => '-' }.merge(options)
7
+
8
+ self.instance_eval <<-RUBY, __FILE__, __LINE__ + 1
9
+ def by_#{options[:on]}(slug)
10
+ if slug[0..0] == '#{options[:identifier]}'
11
+ id = slug[1..(slug.length - 1)].split('#{options[:splitter]}')
12
+ get(id)
13
+ else
14
+ first(:#{options[:on]} => slug) || raise(DataMapper::ObjectNotFoundError, "Could not find \#{self.name} with slug \#{slug}")
15
+ end
16
+ end
17
+ RUBY
18
+
19
+ define_method(options[:on]) do
20
+ instance_variable_get("@#{options[:on]}") || options[:identifier] + key.map.join(options[:splitter])
21
+ end
22
+ end
23
+
24
+ module ClassMethods
25
+ def by_slug(slug)
26
+ if slug[0..0] == '@'
27
+ id = slug[1..(slug.length - 1)]
28
+ get!(id)
29
+ else
30
+ first(:slug => slug) || raise(DataMapper::ObjectNotFoundError, "Could not find #{self.name} with slug #{slug}")
31
+ end
32
+ end
33
+ end # ClassMethods
34
+
35
+ end # Sluggable
36
+ end # Is
37
+ end # DataMapper
@@ -0,0 +1,7 @@
1
+ module DataMapper
2
+ module Is
3
+ module Sluggable
4
+ VERSION = '0.9.8'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,44 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
+
4
+ class Post
5
+ include DataMapper::Resource
6
+
7
+ property :id, Integer, :serial => true
8
+ property :slug, String
9
+
10
+ is :sluggable
11
+
12
+ end
13
+
14
+ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
15
+ describe 'DataMapper::Is::Sluggable' do
16
+ before(:each) do
17
+ Post.auto_migrate!
18
+ @post = Post.create
19
+ end
20
+
21
+ describe 'without a slug' do
22
+ it 'should return the id' do
23
+ @post.slug.should == "@#{@post.id}"
24
+ end
25
+
26
+ it 'be able to be found by id' do
27
+ Post.by_slug("@#{@post.id}").should == @post
28
+ end
29
+ end
30
+
31
+ describe 'with a slug' do
32
+ before(:each) { @post.update_attributes(:slug => 'a-slug') }
33
+
34
+ it 'should return slug' do
35
+ @post.slug.should == 'a-slug'
36
+ end
37
+
38
+ it 'be able to be found by id' do
39
+ Post.by_slug('a-slug').should == @post
40
+ end
41
+ end
42
+
43
+ end
44
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --format specdoc
2
+ --colour
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ gem 'rspec', '>=1.1.3'
3
+ require 'spec'
4
+ require 'pathname'
5
+ require Pathname(__FILE__).dirname.expand_path.parent + 'lib/dm-is-sluggable'
6
+
7
+ def load_driver(name, default_uri)
8
+ return false if ENV['ADAPTER'] != name.to_s
9
+
10
+ lib = "do_#{name}"
11
+
12
+ begin
13
+ gem lib, '~>0.9.8'
14
+ require lib
15
+ DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
16
+ DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
17
+ true
18
+ rescue Gem::LoadError => e
19
+ warn "Could not load #{lib}: #{e}"
20
+ false
21
+ end
22
+ end
23
+
24
+ ENV['ADAPTER'] ||= 'sqlite3'
25
+
26
+ HAS_SQLITE3 = load_driver(:sqlite3, 'sqlite3::memory:')
27
+ HAS_MYSQL = load_driver(:mysql, 'mysql://localhost/dm_core_test')
28
+ HAS_POSTGRES = load_driver(:postgres, 'postgres://postgres@localhost/dm_core_test')
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jeremyboles-dm-is-sluggable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.8
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Boles
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-10 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: dm-core
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - "="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.8
23
+ version:
24
+ description: Simple Datamapper plugin for referencing Resources by 'URL slugs'
25
+ email:
26
+ - jeremy [a] jeremyboles [d] com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.txt
33
+ - LICENSE
34
+ - TODO
35
+ files:
36
+ - History.txt
37
+ - LICENSE
38
+ - Manifest.txt
39
+ - README.markdown
40
+ - Rakefile
41
+ - TODO
42
+ - lib/dm-is-sluggable.rb
43
+ - lib/dm-is-sluggable/is/sluggable.rb
44
+ - lib/dm-is-sluggable/is/version.rb
45
+ - spec/integration/sluggable_spec.rb
46
+ - spec/spec.opts
47
+ - spec/spec_helper.rb
48
+ - README.txt
49
+ has_rdoc: true
50
+ homepage: http://github.com/jeremyboles/dm-is-sluggable/tree/master
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --main
54
+ - README.txt
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project: datamapper
72
+ rubygems_version: 1.2.0
73
+ signing_key:
74
+ specification_version: 2
75
+ summary: Simple Datamapper plugin for referencing Resources by 'URL slugs'
76
+ test_files: []
77
+