Sluggish 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/README.rdoc +20 -0
- data/Rakefile +8 -0
- data/lib/sluggish.rb +23 -0
- data/lib/sluggish/version.rb +3 -0
- data/sluggish.gemspec +22 -0
- data/test/test_sluggish.rb +45 -0
- metadata +102 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
= Sluggable
|
2
|
+
|
3
|
+
Creates a shortcut alias for find_by_(name|slug|key) as [] so that you can call MyModel[:foo] and it will find it.
|
4
|
+
A possible usage would be for common queries like User.find_by_email.
|
5
|
+
The setup would be
|
6
|
+
|
7
|
+
Class User < ActiveRecord::Base
|
8
|
+
include Sluggable
|
9
|
+
sluggit! :email
|
10
|
+
end
|
11
|
+
|
12
|
+
User['foo@example.com'] #=> User.find_by_email('foo@example.com')
|
13
|
+
|
14
|
+
== Installation
|
15
|
+
|
16
|
+
gem install sluggable
|
17
|
+
|
18
|
+
== Notes
|
19
|
+
|
20
|
+
Relies on the class responding to find_by_(foo)
|
data/Rakefile
ADDED
data/lib/sluggish.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require "sluggish/version"
|
2
|
+
|
3
|
+
module Sluggish
|
4
|
+
def self.included(klass)
|
5
|
+
klass.class_eval { @sluggable_finder_key = nil }
|
6
|
+
klass.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def [](slug)
|
11
|
+
send(:"find_by_#{sluggable_finder}", slug.to_s)
|
12
|
+
end
|
13
|
+
|
14
|
+
def sluggit!(finder)
|
15
|
+
@sluggable_finder_key = finder
|
16
|
+
end
|
17
|
+
|
18
|
+
def sluggable_finder
|
19
|
+
@sluggable_finder_key || [:name, :slug, :key].detect{|k| self.respond_to?("find_by_#{k}")}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
data/sluggish.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "sluggish/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "Sluggish"
|
7
|
+
s.version = Sluggish::VERSION
|
8
|
+
s.authors = ["Diego Scataglini"]
|
9
|
+
s.email = ["diego@junivi.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Writes a quick [] finder}
|
12
|
+
s.description = %q{aliases [] to find_by_(name|slug|key) or your own preference}
|
13
|
+
|
14
|
+
s.rubyforge_project = "sluggable"
|
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
|
+
s.add_development_dependency('shoulda')
|
21
|
+
s.add_development_dependency('mocha')
|
22
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "sluggish"
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class Mock1
|
5
|
+
include Sluggish
|
6
|
+
sluggit! :foo
|
7
|
+
def self.find_by_foo(msg)
|
8
|
+
msg
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
%w{name slug key}.each do |k|
|
13
|
+
eval <<-EOF
|
14
|
+
class Mock#{k}
|
15
|
+
include Sluggish
|
16
|
+
def self.find_by_#{k}(msg)
|
17
|
+
msg
|
18
|
+
end
|
19
|
+
end
|
20
|
+
EOF
|
21
|
+
end
|
22
|
+
|
23
|
+
class TestSluggish < Test::Unit::TestCase
|
24
|
+
def test_should_look_for_name
|
25
|
+
assert_equal "bar", Mockname["bar"]
|
26
|
+
end
|
27
|
+
def test_should_look_for_slug
|
28
|
+
assert_equal "bar", Mockslug["bar"]
|
29
|
+
end
|
30
|
+
def test_should_look_for_key
|
31
|
+
assert_equal "bar", Mockkey["bar"]
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_sluggit_should_override_defaults
|
35
|
+
assert_equal "bar", Mock1["bar"]
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_sluggit_variable_is_available_to_class
|
39
|
+
assert_equal :foo, Mock1.instance_variable_get('@sluggable_finder_key')
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_sluggit_variable_is_not_available_to_instance
|
43
|
+
assert_equal nil, Mock1.new.instance_variable_get('@sluggable_finder_key')
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: Sluggish
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Diego Scataglini
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-30 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: shoulda
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: mocha
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id002
|
49
|
+
description: aliases [] to find_by_(name|slug|key) or your own preference
|
50
|
+
email:
|
51
|
+
- diego@junivi.com
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files: []
|
57
|
+
|
58
|
+
files:
|
59
|
+
- .gitignore
|
60
|
+
- Gemfile
|
61
|
+
- README.rdoc
|
62
|
+
- Rakefile
|
63
|
+
- lib/sluggish.rb
|
64
|
+
- lib/sluggish/version.rb
|
65
|
+
- sluggish.gemspec
|
66
|
+
- test/test_sluggish.rb
|
67
|
+
has_rdoc: true
|
68
|
+
homepage: ""
|
69
|
+
licenses: []
|
70
|
+
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
hash: 3
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: "0"
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
hash: 3
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
requirements: []
|
95
|
+
|
96
|
+
rubyforge_project: sluggable
|
97
|
+
rubygems_version: 1.3.7
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: Writes a quick [] finder
|
101
|
+
test_files:
|
102
|
+
- test/test_sluggish.rb
|