carpeliam-dm-slug 0.9.8

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/History.txt ADDED
@@ -0,0 +1 @@
1
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Liam Morley
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,13 @@
1
+ History.txt
2
+ LICENSE
3
+ Manifest.txt
4
+ README.markdown
5
+ Rakefile
6
+ TODO
7
+ lib/dm-slug.rb
8
+ lib/dm-slug/has/slug.rb
9
+ lib/dm-slug/has/version.rb
10
+ spec/integration/slug_spec.rb
11
+ spec/spec.opts
12
+ spec/spec_helper.rb
13
+ tasks/hoe.rb
data/README.markdown ADDED
@@ -0,0 +1,29 @@
1
+ dm-slug
2
+ =======
3
+
4
+ Manages your permalinks so you don't have to.
5
+
6
+ Internally, makes use of `DataMapper::Types::Slug`, although I'm not terribly
7
+ excited about its implementation at the moment. (After you save a model, you
8
+ have to reload it to be able to see a proper value for the slug field.)
9
+
10
+ To use, add the following to your model:
11
+
12
+ has_slug # uses defaults of :on => name, :called => :slug
13
+
14
+ or
15
+
16
+ has_slug :on => title, :called => permalink
17
+
18
+ You can add a `:length` option as well, though if you place your `has_slug`
19
+ anywhere below the property given in `:on`, it will pick up the length of that
20
+ field and use that instead. It will also add a `unique_index` option if the
21
+ original field has `unique_index` set to true, or if it's a key.
22
+
23
+ As shown above, if your field to create a slug on is called `:name` or if you
24
+ don't mind your slug field being called `slug`, then you can leave that
25
+ corresponding option out, as those are the defaults. If you'd like something
26
+ different, as in the second example, you can specify them and they'll get used
27
+ instead of the defaults.
28
+
29
+ Let me know if there's anything additional you'd like to see here.
data/Rakefile ADDED
@@ -0,0 +1,55 @@
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-slug/has/version'
8
+
9
+ AUTHOR = "Liam Morley"
10
+ EMAIL = "liam@carpeliam.com"
11
+ GEM_NAME = "dm-slug"
12
+ GEM_VERSION = DataMapper::Has::Slug::VERSION
13
+ GEM_DEPENDENCIES = [
14
+ ["dm-core", GEM_VERSION],
15
+ ["dm-validations", GEM_VERSION],
16
+ ["dm-types", GEM_VERSION]
17
+ ]
18
+ GEM_CLEAN = ["log", "pkg", "coverage"]
19
+ GEM_EXTRAS = { :has_rdoc => true, :extra_rdoc_files => %w[ README.markdown LICENSE TODO ] }
20
+
21
+ PROJECT_NAME = "dm-slug"
22
+ PROJECT_URL = "http://www.github.com/carpeliam/dm-slug/tree/master"
23
+ PROJECT_DESCRIPTION = PROJECT_SUMMARY = "Manages your slug so you don't have to."
24
+
25
+ require ROOT + 'tasks/hoe'
26
+
27
+ task :default => [ :spec ]
28
+
29
+ WIN32 = (RUBY_PLATFORM =~ /win32|mingw|cygwin/) rescue nil
30
+ SUDO = WIN32 ? '' : ('sudo' unless ENV['SUDOLESS'])
31
+
32
+ desc "Install #{GEM_NAME} #{GEM_VERSION}"
33
+ task :install => [ :package ] do
34
+ sh "#{SUDO} gem install --local pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources", :verbose => false
35
+ end
36
+
37
+ desc "Uninstall #{GEM_NAME} #{GEM_VERSION} (default ruby)"
38
+ task :uninstall => [ :clobber ] do
39
+ sh "#{SUDO} gem uninstall #{GEM_NAME} -v#{GEM_VERSION} -I -x", :verbose => false
40
+ end
41
+
42
+ desc 'Run specifications'
43
+ Spec::Rake::SpecTask.new(:spec) do |t|
44
+ t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
45
+ t.spec_files = Pathname.glob((ROOT + 'spec/**/*_spec.rb').to_s)
46
+
47
+ begin
48
+ t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true
49
+ t.rcov_opts << '--exclude' << 'spec'
50
+ t.rcov_opts << '--text-summary'
51
+ t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
52
+ rescue Exception
53
+ # rcov not installed
54
+ end
55
+ end
data/TODO ADDED
@@ -0,0 +1,2 @@
1
+ TODO
2
+ ====
@@ -0,0 +1,30 @@
1
+ module DataMapper
2
+ module Has
3
+ module Slug
4
+
5
+ ##
6
+ # Methods that should be included in DataMapper::Model.
7
+ # Normally this should just be your generator, so that the namespace
8
+ # does not get cluttered. ClassMethods and InstanceMethods gets added
9
+ # in the specific resources when you fire is :example
10
+ ##
11
+
12
+ def has_slug(options = {})
13
+ options = { :on => :name, :called => :slug, :length => 50 }.merge(options)
14
+
15
+ if properties.has_property? options[:on]
16
+ p = properties[options[:on]]
17
+ p_opts = {:length => p.length, :lazy => p.lazy?, :nullable => p.nullable?,
18
+ :unique_index => (p.unique or p.unique_index)}
19
+ else
20
+ p_opts = {:length => options[:length]}
21
+ end
22
+ property options[:called], DataMapper::Types::Slug, p_opts
23
+
24
+ before :valid? do
25
+ attribute_set(options[:called], self.send(options[:on]))
26
+ end
27
+ end
28
+ end # Slug
29
+ end # Has
30
+ end # DataMapper
@@ -0,0 +1,7 @@
1
+ module DataMapper
2
+ module Has
3
+ module Slug
4
+ VERSION = "0.9.8"
5
+ end
6
+ end
7
+ end
data/lib/dm-slug.rb ADDED
@@ -0,0 +1,24 @@
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.8'
7
+ require 'dm-core'
8
+ #gem 'dm-validations', '~>0.9.8'
9
+ require 'dm-validations'
10
+ #gem 'dm-types', '~>0.9.8'
11
+ require 'dm-types'
12
+
13
+ # Require plugin-files
14
+ require Pathname(__FILE__).dirname.expand_path / 'dm-slug' / 'has' / 'slug.rb'
15
+
16
+
17
+ # Include the plugin in Resource
18
+ module DataMapper
19
+ module Resource
20
+ module ClassMethods
21
+ include DataMapper::Has::Slug
22
+ end # module ClassMethods
23
+ end # module Resource
24
+ end # module DataMapper
@@ -0,0 +1,56 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
+
4
+ class NameExample
5
+ include DataMapper::Resource
6
+ has_slug
7
+ property :id, Serial
8
+ property :name, String
9
+ end
10
+
11
+ class TitleExample
12
+ include DataMapper::Resource
13
+ property :id, Serial
14
+ property :title, String, :length => 75, :unique_index => true
15
+ has_slug :on => :title
16
+ end
17
+
18
+ class AlternateFieldExample
19
+ include DataMapper::Resource
20
+ property :id, Serial
21
+ property :title, String
22
+ has_slug :on => :title, :called => :permalink
23
+ end
24
+
25
+ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
26
+
27
+ describe 'DataMapper::Has::Slug' do
28
+ before do
29
+ DataMapper.auto_migrate!
30
+ end
31
+ it "should create a slug field" do
32
+ ne = NameExample.create :name => 'foo bar'
33
+ ne.respond_to? :slug
34
+ end
35
+
36
+ it "should have a slug field that matches the given name" do
37
+ ne = NameExample.create :name => 'foo bar'
38
+ NameExample.first.slug.should == 'foo-bar'
39
+ end
40
+
41
+ it "should accept an :on option" do
42
+ te = TitleExample.create :title => 'foo bar'
43
+ TitleExample.first.slug.should == 'foo-bar'
44
+ end
45
+
46
+ it "should respect the length of the :on field if known" do
47
+ TitleExample.properties[:slug].length.should == 75 # TitleExample.properties[:title].length
48
+ TitleExample.properties[:slug].unique_index.should == true # TitleExample.properties[:title].unique_index
49
+ end
50
+
51
+ it "should accept a :named option" do
52
+ afe = AlternateFieldExample.create :title => 'foo bar'
53
+ AlternateFieldExample.first.permalink.should == 'foo-bar'
54
+ end
55
+ end
56
+ 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-slug'
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')
data/tasks/hoe.rb ADDED
@@ -0,0 +1,49 @@
1
+ require 'hoe'
2
+
3
+ @config_file = "~/.rubyforge/user-config.yml"
4
+ @config = nil
5
+ RUBYFORGE_USERNAME = "unknown"
6
+ def rubyforge_username
7
+ unless @config
8
+ begin
9
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
10
+ rescue
11
+ puts <<-EOS
12
+ ERROR: No rubyforge config file found: #{@config_file}
13
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
14
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
15
+ EOS
16
+ exit
17
+ end
18
+ end
19
+ RUBYFORGE_USERNAME.replace @config["username"]
20
+ end
21
+
22
+ # Remove hoe dependency
23
+ class Hoe
24
+ def extra_dev_deps
25
+ @extra_dev_deps.reject! { |dep| dep[0] == "hoe" }
26
+ @extra_dev_deps
27
+ end
28
+ end
29
+
30
+ hoe = Hoe.new(GEM_NAME, GEM_VERSION) do |p|
31
+
32
+ p.developer(AUTHOR, EMAIL)
33
+
34
+ p.description = PROJECT_DESCRIPTION
35
+ p.summary = PROJECT_SUMMARY
36
+ p.url = PROJECT_URL
37
+
38
+ p.rubyforge_name = PROJECT_NAME if PROJECT_NAME
39
+
40
+ p.clean_globs |= GEM_CLEAN
41
+ p.spec_extras = GEM_EXTRAS if GEM_EXTRAS
42
+
43
+ GEM_DEPENDENCIES.each do |dep|
44
+ p.extra_deps << dep
45
+ end
46
+
47
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
48
+
49
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carpeliam-dm-slug
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.8
5
+ platform: ruby
6
+ authors:
7
+ - Liam Morley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-14 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
+ - !ruby/object:Gem::Dependency
25
+ name: dm-validations
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - "="
30
+ - !ruby/object:Gem::Version
31
+ version: 0.9.8
32
+ version:
33
+ - !ruby/object:Gem::Dependency
34
+ name: dm-types
35
+ version_requirement:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.8
41
+ version:
42
+ description: Manages your slug so you don't have to.
43
+ email:
44
+ - liam@carpeliam.com
45
+ executables: []
46
+
47
+ extensions: []
48
+
49
+ extra_rdoc_files:
50
+ - README.markdown
51
+ - LICENSE
52
+ - TODO
53
+ files:
54
+ - History.txt
55
+ - LICENSE
56
+ - Manifest.txt
57
+ - README.markdown
58
+ - Rakefile
59
+ - TODO
60
+ - lib/dm-slug.rb
61
+ - lib/dm-slug/has/slug.rb
62
+ - lib/dm-slug/has/version.rb
63
+ - spec/integration/slug_spec.rb
64
+ - spec/spec.opts
65
+ - spec/spec_helper.rb
66
+ - tasks/hoe.rb
67
+ has_rdoc: true
68
+ homepage: http://www.github.com/carpeliam/dm-slug/tree/master
69
+ post_install_message:
70
+ rdoc_options:
71
+ - --main
72
+ - README.markdown
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ version:
87
+ requirements: []
88
+
89
+ rubyforge_project: dm-slug
90
+ rubygems_version: 1.2.0
91
+ signing_key:
92
+ specification_version: 2
93
+ summary: Manages your slug so you don't have to.
94
+ test_files: []
95
+