dm-ar-finders 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 John W Higgins
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 ADDED
@@ -0,0 +1,4 @@
1
+ dm-ar-finders
2
+ =============
3
+
4
+ DataMapper plugin providing support for ActiveRecord-style finders.
data/Rakefile ADDED
@@ -0,0 +1,65 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'rake/clean'
4
+ require 'rake/gempackagetask'
5
+ require 'spec/rake/spectask'
6
+ require 'pathname'
7
+
8
+ CLEAN.include '{log,pkg}/'
9
+
10
+ spec = Gem::Specification.new do |s|
11
+ s.name = 'dm-ar-finders'
12
+ s.version = '0.9.2'
13
+ s.platform = Gem::Platform::RUBY
14
+ s.has_rdoc = true
15
+ s.extra_rdoc_files = %w[ README LICENSE TODO ]
16
+ s.summary = 'DataMapper plugin providing ActiveRecord-style finders'
17
+ s.description = s.summary
18
+ s.author = 'John W Higgins'
19
+ s.email = 'john@wishVPS.com'
20
+ s.homepage = 'http://github.com/sam/dm-more/tree/master/dm-ar-finders'
21
+ s.require_path = 'lib'
22
+ s.files = FileList[ '{lib,spec}/**/*.rb', 'spec/spec.opts', 'Rakefile', *s.extra_rdoc_files ]
23
+ s.add_dependency('dm-core', "=#{s.version}")
24
+ end
25
+
26
+ task :default => [ :spec ]
27
+
28
+ WIN32 = (RUBY_PLATFORM =~ /win32|mingw|cygwin/) rescue nil
29
+ SUDO = WIN32 ? '' : ('sudo' unless ENV['SUDOLESS'])
30
+
31
+ Rake::GemPackageTask.new(spec) do |pkg|
32
+ pkg.gem_spec = spec
33
+ end
34
+
35
+ desc "Install #{spec.name} #{spec.version} (default ruby)"
36
+ task :install => [ :package ] do
37
+ sh "#{SUDO} gem install --local pkg/#{spec.name}-#{spec.version} --no-update-sources", :verbose => false
38
+ end
39
+
40
+ desc "Uninstall #{spec.name} #{spec.version} (default ruby)"
41
+ task :uninstall => [ :clobber ] do
42
+ sh "#{SUDO} gem uninstall #{spec.name} -v#{spec.version} -I -x", :verbose => false
43
+ end
44
+
45
+ namespace :jruby do
46
+ desc "Install #{spec.name} #{spec.version} with JRuby"
47
+ task :install => [ :package ] do
48
+ sh %{#{SUDO} jruby -S gem install --local pkg/#{spec.name}-#{spec.version} --no-update-sources}, :verbose => false
49
+ end
50
+ end
51
+
52
+ desc 'Run specifications'
53
+ Spec::Rake::SpecTask.new(:spec) do |t|
54
+ t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
55
+ t.spec_files = Pathname.glob(Pathname.new(__FILE__).dirname + 'spec/**/*_spec.rb')
56
+
57
+ begin
58
+ t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true
59
+ t.rcov_opts << '--exclude' << 'spec'
60
+ t.rcov_opts << '--text-summary'
61
+ t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
62
+ rescue Exception
63
+ # rcov not installed
64
+ end
65
+ end
data/TODO ADDED
@@ -0,0 +1,8 @@
1
+ TODO
2
+ ====
3
+
4
+
5
+
6
+ ---
7
+ TODO tickets may also be found in the DataMapper Issue Tracker:
8
+ http://wm.lighthouseapp.com/projects/4819-datamapper/overview
@@ -0,0 +1,42 @@
1
+ require 'rubygems'
2
+ gem 'dm-core', '=0.9.2'
3
+ require 'dm-core'
4
+
5
+ module DataMapper
6
+ module Model
7
+ def find_or_create(search_attributes, create_attributes = {})
8
+ first(search_attributes) || create(search_attributes.merge(create_attributes))
9
+ end
10
+
11
+ private
12
+
13
+ def method_missing_with_find_by(method, *args, &block)
14
+ if match = matches_dynamic_finder?(method)
15
+ finder = determine_finder(match)
16
+ attribute_names = extract_attribute_names_from_match(match)
17
+
18
+ conditions = {}
19
+ attribute_names.each {|key| conditions[key] = args.shift}
20
+
21
+ send(finder, conditions)
22
+ else
23
+ method_missing_without_find_by(method, *args, &block)
24
+ end
25
+ end
26
+
27
+ alias_method :method_missing_without_find_by, :method_missing
28
+ alias_method :method_missing, :method_missing_with_find_by
29
+
30
+ def matches_dynamic_finder?(method_id)
31
+ /^find_(all_by|by)_([_a-zA-Z]\w*)$/.match(method_id.to_s)
32
+ end
33
+
34
+ def determine_finder(match)
35
+ match.captures.first == 'all_by' ? :all : :first
36
+ end
37
+
38
+ def extract_attribute_names_from_match(match)
39
+ match.captures.last.split('_and_')
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,48 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
+
4
+ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
5
+ describe "DataMapper::Resource" do
6
+ after do
7
+ repository(:default).adapter.execute('DELETE from green_smoothies');
8
+ end
9
+
10
+ before(:all) do
11
+ class GreenSmoothie
12
+ include DataMapper::Resource
13
+ property :id, Integer, :serial => true
14
+ property :name, String
15
+
16
+ auto_migrate!(:default)
17
+ end
18
+ end
19
+
20
+ it "should find/create using find_or_create" do
21
+ repository(:default) do
22
+ green_smoothie = GreenSmoothie.new(:name => 'Banana')
23
+ green_smoothie.save
24
+ GreenSmoothie.find_or_create({:name => 'Banana'}).id.should eql(green_smoothie.id)
25
+ GreenSmoothie.find_or_create({:name => 'Strawberry'}).id.should eql(2)
26
+ end
27
+ end
28
+
29
+ it "should use find_by and use the name attribute to find a record" do
30
+ repository(:default) do
31
+ green_smoothie = GreenSmoothie.create({:name => 'Banana'})
32
+ green_smoothie.should == GreenSmoothie.find_by_name('Banana')
33
+ end
34
+ end
35
+
36
+ it "should use find_all_by to find records using an attribute" do
37
+ repository(:default) do
38
+ green_smoothie = GreenSmoothie.create({:name => 'Banana'})
39
+ green_smoothie2 = GreenSmoothie.create({:name => 'Banana'})
40
+ found_records = GreenSmoothie.find_all_by_name('Banana')
41
+ found_records.length.should == 2
42
+ found_records.each do |found_record|
43
+ [green_smoothie, green_smoothie2].include?(found_record).should be_true
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --format specdoc
2
+ --colour
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'pathname'
3
+ require Pathname(__FILE__).dirname.expand_path.parent + 'lib/dm-ar-finders'
4
+
5
+ def load_driver(name, default_uri)
6
+ return false if ENV['ADAPTER'] != name.to_s
7
+
8
+ lib = "do_#{name}"
9
+
10
+ begin
11
+ gem lib, '=0.9.2'
12
+ require lib
13
+ DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
14
+ DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
15
+
16
+ FileUtils.touch LOG_PATH
17
+ DataMapper::Logger.new(LOG_PATH, 0)
18
+ at_exit { DataMapper.logger.close }
19
+ true
20
+ rescue Gem::LoadError => e
21
+ warn "Could not load #{lib}: #{e}"
22
+ false
23
+ end
24
+ end
25
+
26
+ ENV['ADAPTER'] ||= 'sqlite3'
27
+ LOG_PATH = Pathname(__FILE__).dirname.expand_path.to_s + '/sql.log'
28
+ HAS_SQLITE3 = load_driver(:sqlite3, 'sqlite3::memory:')
29
+ HAS_MYSQL = load_driver(:mysql, 'mysql://localhost/dm_core_test')
30
+ HAS_POSTGRES = load_driver(:postgres, 'postgres://postgres@localhost/dm_core_test')
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-ar-finders
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.2
5
+ platform: ruby
6
+ authors:
7
+ - John W Higgins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-25 00:00:00 -05: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.2
23
+ version:
24
+ description: DataMapper plugin providing ActiveRecord-style finders
25
+ email: john@wishVPS.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ - LICENSE
33
+ - TODO
34
+ files:
35
+ - lib/dm-ar-finders.rb
36
+ - spec/integration/ar-finders_spec.rb
37
+ - spec/spec_helper.rb
38
+ - spec/spec.opts
39
+ - Rakefile
40
+ - README
41
+ - LICENSE
42
+ - TODO
43
+ has_rdoc: true
44
+ homepage: http://github.com/sam/dm-more/tree/master/dm-ar-finders
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.0.1
66
+ signing_key:
67
+ specification_version: 2
68
+ summary: DataMapper plugin providing ActiveRecord-style finders
69
+ test_files: []
70
+