find_by_hash 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ryan Bigg
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/README.markdown ADDED
@@ -0,0 +1,29 @@
1
+ # Find By Hash
2
+
3
+ Find By Hash is a library that does exactly what it says on the box: it finds by a Hash. It will improve all aspects of your life.
4
+
5
+ All methods require a hash to be passed to them, otherwise you'll get a `NotAHash` error.
6
+
7
+ ## `find_by_hash`
8
+
9
+ Finds a single record based on the given hash.
10
+
11
+ Post.find_by_hash(:title => "Hey there!")
12
+
13
+ ## `find_all_by_hash`
14
+
15
+ Finds all records based on the given hash.
16
+
17
+ Post.find_all_by_hash(:title => "Hey there!")
18
+
19
+ ## `find_or_create_by_hash`
20
+
21
+ Finds or creates a record based on the given hash.
22
+
23
+ Post.find_or_create_by_hash(:title => "Hey there!")
24
+
25
+ ## `find_or_initialize_by_hash`
26
+
27
+ Finds or initializes a record based on the given hash.
28
+
29
+ Post.find_or_initialize_by_hash(:title => "Hey there!")
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "find_by_hash"
8
+ gem.summary = %Q{Extension to ActiveRecord to allow you to do findy-thingies with hashes}
9
+ gem.description = %Q{Extension to ActiveRecord to allow you to do findy-thingies with hashes}
10
+ gem.email = "radarlistener@gmail.com"
11
+ gem.homepage = "http://github.com/radar/find_by_hash"
12
+ gem.authors = ["Ryan Bigg"]
13
+ gem.add_development_dependency "rspec"
14
+ gem.add_dependency "activerecord"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ if File.exist?('VERSION')
41
+ version = File.read('VERSION')
42
+ else
43
+ version = ""
44
+ end
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "find_by_hash #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{find_by_hash}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ryan Bigg"]
12
+ s.date = %q{2010-07-03}
13
+ s.description = %q{Extension to ActiveRecord to allow you to do findy-thingies with hashes}
14
+ s.email = %q{radarlistener@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.markdown",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "find_by_hash.gemspec",
27
+ "find_by_hash.sqlite3",
28
+ "lib/find_by_hash.rb",
29
+ "rails/init.rb",
30
+ "spec/find_by_hash_spec.rb",
31
+ "spec/initializers/schema.rb",
32
+ "spec/spec_helper.rb",
33
+ "spec/support/models.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/radar/find_by_hash}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.6}
39
+ s.summary = %q{Extension to ActiveRecord to allow you to do findy-thingies with hashes}
40
+ s.test_files = [
41
+ "spec/find_by_hash_spec.rb",
42
+ "spec/initializers/schema.rb",
43
+ "spec/spec_helper.rb",
44
+ "spec/support/models.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<rspec>, [">= 0"])
53
+ s.add_runtime_dependency(%q<activerecord>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<rspec>, [">= 0"])
56
+ s.add_dependency(%q<activerecord>, [">= 0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<rspec>, [">= 0"])
60
+ s.add_dependency(%q<activerecord>, [">= 0"])
61
+ end
62
+ end
63
+
Binary file
@@ -0,0 +1,37 @@
1
+ module FindByHash
2
+
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+ module ClassMethods
7
+ def find_by_hash(hash)
8
+ hash_find("find_by", hash)
9
+ end
10
+
11
+ def find_all_by_hash(hash)
12
+ hash_find("find_all_by", hash)
13
+ end
14
+
15
+ def find_or_create_by_hash(hash)
16
+ hash_find("find_or_create_by", hash)
17
+ end
18
+
19
+ def find_or_initialize_by_hash(hash)
20
+ hash_find("find_or_initialize_by", hash)
21
+ end
22
+
23
+ private
24
+ def syntax(object)
25
+ raise NotAHash if !object.is_a?(Hash)
26
+ object.keys.join("_and_")
27
+ end
28
+
29
+ def hash_find(finder="find_by", hash={})
30
+ send(finder + "_" + syntax(hash), *hash.values)
31
+ end
32
+ end
33
+
34
+ class NotAHash < Exception; end
35
+ end
36
+
37
+
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'find_by_hash'
2
+ ActiveRecord::Base.send :include, FindByHash
@@ -0,0 +1,50 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "The whole thing" do
4
+ describe "find_by_hash" do
5
+ it "finds by a hash containing one key" do
6
+
7
+ Post.find_by_hash({ :title => "Hey there!" }).should_not be_nil
8
+ end
9
+
10
+ it "finds by a hash containing multiple keys" do
11
+ Post.find_by_hash({ :permalink => "hey-there", :title => "Hey there!" }).should_not be_nil
12
+ end
13
+
14
+ it "errors if not passed a hash" do
15
+ lambda { Post.find_by_hash([]) }.should raise_error(FindByHash::NotAHash)
16
+ end
17
+ end
18
+
19
+ describe "find_all_by_hash" do
20
+ it "finds all the posts" do
21
+ Post.find_all_by_hash({ :category => "Feeling good" }).size.should eql(2)
22
+ end
23
+ end
24
+
25
+ describe "find_or_create_by_hash" do
26
+ it "creates a post because it doesn't exist" do
27
+ Post.expects(:new).returns(post = stub)
28
+ post.expects(:save).returns(true)
29
+ Post.find_or_create_by_hash({ :title => "What's up dog?" })
30
+ end
31
+
32
+ it "finds a pre-existing record" do
33
+ Post.expects(:new).never
34
+ Post.find_or_create_by_hash({ :title => "Hey there!" })
35
+ end
36
+ end
37
+
38
+ describe "find_or_initialize_by_hash" do
39
+ it "initializes a new record" do
40
+ Post.expects(:new).returns(post = stub)
41
+ post.expects(:save).never
42
+ Post.find_or_initialize_by_hash({ :title => "What's up dog?" })
43
+ end
44
+
45
+ it "finds a pre-existing record" do
46
+ Post.expects(:new).never
47
+ Post.find_or_initialize_by_hash({ :title => "Hey there!" })
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :posts, :force => true do |t|
5
+ t.string :text, :permalink, :title, :category
6
+ t.timestamps
7
+ end
8
+
9
+ end
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ begin
4
+ require 'rubygems'
5
+ rescue LoadError
6
+ end
7
+
8
+ require 'spec'
9
+ require 'spec/autorun'
10
+ require 'active_record'
11
+
12
+ require 'find_by_hash'
13
+ # Just like a real rails app!
14
+ require 'rails/init'
15
+
16
+ ActiveRecord::Base.establish_connection(:database => "find_by_hash.sqlite3", :adapter => "sqlite3")
17
+ load 'initializers/schema.rb'
18
+
19
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f }
20
+
21
+ Spec::Runner.configure do |config|
22
+ config.mock_with :mocha
23
+ end
@@ -0,0 +1,12 @@
1
+ class Post < ActiveRecord::Base
2
+ before_create :generate_permalink
3
+
4
+ private
5
+
6
+ def generate_permalink
7
+ self.permalink = title.parameterize
8
+ end
9
+ end
10
+
11
+ Post.create(:title => "Hey there!", :text => "Thanks for using Find By Hash!", :category => "Feeling good")
12
+ Post.create(:title => "Sup, all?", :text => "How's things today?", :category => "Feeling good")
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: find_by_hash
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 0
9
+ version: 0.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Ryan Bigg
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-03 00:00:00 +10:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: activerecord
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :runtime
43
+ version_requirements: *id002
44
+ description: Extension to ActiveRecord to allow you to do findy-thingies with hashes
45
+ email: radarlistener@gmail.com
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ extra_rdoc_files:
51
+ - LICENSE
52
+ - README.markdown
53
+ files:
54
+ - .document
55
+ - .gitignore
56
+ - LICENSE
57
+ - README.markdown
58
+ - Rakefile
59
+ - VERSION
60
+ - find_by_hash.gemspec
61
+ - find_by_hash.sqlite3
62
+ - lib/find_by_hash.rb
63
+ - rails/init.rb
64
+ - spec/find_by_hash_spec.rb
65
+ - spec/initializers/schema.rb
66
+ - spec/spec_helper.rb
67
+ - spec/support/models.rb
68
+ has_rdoc: true
69
+ homepage: http://github.com/radar/find_by_hash
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --charset=UTF-8
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ requirements: []
92
+
93
+ rubyforge_project:
94
+ rubygems_version: 1.3.6
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Extension to ActiveRecord to allow you to do findy-thingies with hashes
98
+ test_files:
99
+ - spec/find_by_hash_spec.rb
100
+ - spec/initializers/schema.rb
101
+ - spec/spec_helper.rb
102
+ - spec/support/models.rb