dm-is-indexed 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,3 @@
1
+ -
2
+ ChangeLog.*
3
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --markup markdown --title "dm-is-indexed Documentation" --protected
data/ChangeLog.md ADDED
@@ -0,0 +1,4 @@
1
+ ### 0.1.0 / 2011-03-21
2
+
3
+ * Initial release.
4
+
data/Gemfile ADDED
@@ -0,0 +1,142 @@
1
+ # If you're working on more than one datamapper gem at a time, then it's
2
+ # recommended to create a local Gemfile and use this instead of the git
3
+ # sources. This will make sure that you are developing against your
4
+ # other local datamapper sources that you currently work on. Gemfile.local
5
+ # will behave identically to the standard Gemfile apart from the fact that
6
+ # it fetches the datamapper gems from local paths. This means that you can
7
+ # use the same environment variables, like ADAPTER(S) or PLUGIN(S) when
8
+ # running
9
+ # bundle commands. Gemfile.local is added to .gitignore, so you don't need
10
+ # to worry about accidentally checking local development paths into git.
11
+ # In order to create a local Gemfile, all you need to do is run:
12
+ #
13
+ # bundle exec rake local_gemfile
14
+ #
15
+ # This will give you a Gemfile.local file that points to your local clones
16
+ # of the various datamapper gems. It's assumed that all datamapper repo
17
+ # clones reside in the same directory. You can use the Gemfile.local like
18
+ # so for running any bundle command:
19
+ #
20
+ # BUNDLE_GEMFILE=Gemfile.local bundle foo
21
+ #
22
+ # You can also specify which adapter(s) should be part of the bundle by
23
+ # setting an environment variable. This of course also works when using the
24
+ # Gemfile.local
25
+ #
26
+ # bundle foo # dm-sqlite-adapter
27
+ #
28
+ # ADAPTER=mysql bundle foo # dm-mysql-adapter
29
+ #
30
+ # ADAPTERS=sqlite,mysql bundle foo # dm-sqlite-adapter, dm-mysql-adapter
31
+ #
32
+ # Of course you can also use the ADAPTER(S) variable when using the
33
+ # Gemfile.local and running specs against selected adapters.
34
+ #
35
+ # For easily working with adapters supported on your machine, it's
36
+ # recommended that you first install all adapters that you are planning to
37
+ # use or work on by doing something like
38
+ #
39
+ # ADAPTERS=sqlite,mysql,postgres bundle install
40
+ #
41
+ # This will clone the various repositories and make them available to
42
+ # bundler. Once you have them installed you can easily switch between
43
+ # adapters for the various development tasks. Running something like
44
+ #
45
+ # ADAPTER=mysql bundle exec rake spec
46
+ #
47
+ # will make sure that the dm-mysql-adapter is part of the bundle, and will
48
+ # be used when running the specs.
49
+ #
50
+ # You can also specify which plugin(s) should be part of the bundle by
51
+ # setting an environment variable. This also works when using the
52
+ # Gemfile.local
53
+ #
54
+ # bundle foo # dm-migrations
55
+ #
56
+ # PLUGINS=dm-validations bundle foo # dm-migrations,
57
+ # # dm-validations
58
+ #
59
+ # PLUGINS=dm-validations,dm-types bundle foo # dm-migrations,
60
+ # # dm-validations,
61
+ # # dm-types
62
+ #
63
+ # Of course you can combine the PLUGIN(S) and ADAPTER(S) env vars to run
64
+ # specs for certain adapter/plugin combinations.
65
+ #
66
+ # Finally, to speed up running specs and other tasks, it's recommended to
67
+ # run
68
+ #
69
+ # bundle lock
70
+ #
71
+ # after running 'bundle install' for the first time. This will make
72
+ # 'bundle exec' run a lot faster compared to the unlocked version. With an
73
+ # unlocked bundle you would typically just run 'bundle install' from time
74
+ # to time to fetch the latest sources from upstream. When you locked your
75
+ # bundle, you need to run
76
+ #
77
+ # bundle install --relock
78
+ #
79
+ # to make sure to fetch the latest updates and then lock the bundle again.
80
+ # Gemfile.lock is added to the .gitignore file, so you don't need to worry
81
+ # about accidentally checking it into version control.
82
+
83
+ source :rubygems
84
+
85
+ DATAMAPPER = 'http://github.com/datamapper'
86
+ DM_VERSION = '~> 1.0'
87
+ DO_VERSION = '~> 0.10.2'
88
+ DM_DO_ADAPTERS = %w[sqlite postgres mysql oracle sqlserver]
89
+ RAILS = 'http://github.com/rails/rails.git'
90
+
91
+ if ENV['EXTLIB']
92
+ gem 'extlib', '~> 0.9.15', :git => "#{DATAMAPPER}/extlib.git",
93
+ :require => nil
94
+ else
95
+ gem 'activesupport', '~> 3.0.4', :require => nil
96
+ gem 'i18n', '~> 0.5.0'
97
+ end
98
+
99
+ gem 'dm-core', DM_VERSION, :git => "#{DATAMAPPER}/dm-core.git"
100
+
101
+ group :development do
102
+ gem 'rake', '~> 0.8.7'
103
+
104
+ gem 'ore-tasks', '~> 0.4'
105
+ gem 'rspec', '~> 2.4'
106
+
107
+ gem 'kramdown', '~> 0.12.0'
108
+ gem 'yard', '~> 0.6.0'
109
+ end
110
+
111
+ group :datamapper do
112
+ # We need this because we want to pin these dependencies to their git
113
+ # master sources
114
+
115
+ adapters = ENV['ADAPTER'] || ENV['ADAPTERS']
116
+ adapters = adapters.to_s.tr(',', ' ').split.uniq - %w[ in_memory ]
117
+
118
+ if (do_adapters = DM_DO_ADAPTERS & adapters).any?
119
+ options = {}
120
+ options[:git] = "#{DATAMAPPER}/do.git" if ENV['DO_GIT'] == 'true'
121
+
122
+ gem 'data_objects', DO_VERSION, options.dup
123
+
124
+ do_adapters.each do |adapter|
125
+ adapter = 'sqlite3' if adapter == 'sqlite'
126
+ gem "do_#{adapter}", DO_VERSION, options.dup
127
+ end
128
+
129
+ gem 'dm-do-adapter', DM_VERSION, :git => "#{DATAMAPPER}/dm-do-adapter.git"
130
+ end
131
+
132
+ adapters.each do |adapter|
133
+ gem "dm-#{adapter}-adapter", DM_VERSION, :git => "#{DATAMAPPER}/dm-#{adapter}-adapter.git"
134
+ end
135
+
136
+ plugins = ENV['PLUGINS'] || ENV['PLUGIN']
137
+ plugins = plugins.to_s.tr(',', ' ').split.push('dm-migrations').uniq
138
+
139
+ plugins.each do |plugin|
140
+ gem plugin, DM_VERSION, :git => "#{DATAMAPPER}/#{plugin}.git"
141
+ end
142
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Hal Brodigan
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.md ADDED
@@ -0,0 +1,65 @@
1
+ # dm-is-indexed
2
+
3
+ * [Homepage](http://github.com/postmodern/dm-is-indexed#readme)
4
+ * [Issues](http://github.com/postmodern/dm-is-indexed/issues)
5
+ * [Documentation](http://rubydoc.info/gems/dm-is-indexed/frames)
6
+ * [Email](mailto:postmodern.mod3 at gmail.com)
7
+
8
+ ## Description
9
+
10
+ dm-is-indexed overloads the `Model[]` method, and allows querying Resources
11
+ using their indexed properties.
12
+
13
+ ## Examples
14
+
15
+ require 'dm-is-indexed'
16
+
17
+ class Atom
18
+
19
+ include DataMapper::Resource
20
+
21
+ is :indexed
22
+
23
+ property :id, Serial
24
+
25
+ property :symbol, String, :unique => true
26
+
27
+ property :name, String, :unique => true
28
+
29
+ property :atomic_weight, Float, :index => true
30
+
31
+ end
32
+
33
+ Traditional Array style access is preserved:
34
+
35
+ Atom[0]
36
+ # => #<Atom: @id=1 @symbol="He" @name="Helium" @atomic_weight=4.002602>
37
+
38
+ Atom[0,1]
39
+ # => [#<Atom: @id=1 @symbol="He" @name="Helium" @atomic_weight=4.002602>,
40
+ #<Atom: @id=2 @symbol="Ne" @name="Neon" @atomic_weight=20.1797>]
41
+
42
+ Query Resources based on their unique indexed properties:
43
+
44
+ Atom["Kr"]
45
+ # => #<Atom: @id=4 @symbol="Kr" @name="Krpton" @atomic_weight=83.798>
46
+
47
+ Atom["Krpton"]
48
+ # => #<Atom: @id=4 @symbol="Kr" @name="Krpton" @atomic_weight=83.798>
49
+
50
+ Atom[83.798]
51
+ # => #<Atom: @id=4 @symbol="Kr" @name="Krpton" @atomic_weight=83.798>
52
+
53
+ ## Requirements
54
+
55
+ * [dm-core](http://github.com/datamapper/dm-core#readme) ~> 1.0
56
+
57
+ ## Install
58
+
59
+ $ gem install dm-is-indexed
60
+
61
+ ## Copyright
62
+
63
+ Copyright (c) 2011 Hal Brodigan
64
+
65
+ See {file:LICENSE.txt} for details.
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+
3
+ begin
4
+ require 'bundler'
5
+ rescue LoadError => e
6
+ STDERR.puts e.message
7
+ STDERR.puts "Run `gem install bundler` to install Bundler."
8
+ exit e.status_code
9
+ end
10
+
11
+ begin
12
+ Bundler.setup(:development)
13
+ rescue Bundler::BundlerError => e
14
+ STDERR.puts e.message
15
+ STDERR.puts "Run `bundle install` to install missing gems."
16
+ exit e.status_code
17
+ end
18
+
19
+ require 'rake'
20
+
21
+ require 'ore/tasks'
22
+ Ore::Tasks.new
23
+
24
+ require 'rspec/core/rake_task'
25
+ RSpec::Core::RakeTask.new
26
+
27
+ task :test => :spec
28
+ task :default => :spec
29
+
30
+
31
+ require 'yard'
32
+ YARD::Rake::YardocTask.new
33
+ task :doc => :yard
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ begin
4
+ Ore::Specification.new do |gemspec|
5
+ # custom logic here
6
+ end
7
+ rescue NameError
8
+ begin
9
+ require 'ore/specification'
10
+ retry
11
+ rescue LoadError
12
+ STDERR.puts "The '#{__FILE__}' file requires Ore."
13
+ STDERR.puts "Run `gem install ore-core` to install Ore."
14
+ end
15
+ end
data/gemspec.yml ADDED
@@ -0,0 +1,19 @@
1
+ name: dm-is-indexed
2
+ version: 0.1.0
3
+ summary: Query DataMapper Resources via their indexed properties.
4
+ description:
5
+ dm-is-indexed overloads the Model[] method, and allows querying Resources
6
+ using their indexed properties.
7
+
8
+ license: MIT
9
+ authors: Postmodern
10
+ email: postmodern.mod3@gmail.com
11
+ homepage: http://github.com/postmodern/dm-is-indexed
12
+ has_yard: true
13
+
14
+ dependencies:
15
+ dm-core: ~> 1.0
16
+
17
+ development_dependencies:
18
+ bundler: ~> 1.0.0
19
+ yard: ~> 0.6.0
@@ -0,0 +1,51 @@
1
+ module DataMapper
2
+ module Is
3
+ module Indexed
4
+ #
5
+ # Fired when your plugin gets included into a Model.
6
+ #
7
+ def is_indexed
8
+ extend DataMapper::Is::Indexed::ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+ #
13
+ # Allows querying the indexed properties of a Model.
14
+ #
15
+ # @param [Array, Range, Integer, Object] keys
16
+ # The key to use when querying the Model. If the key is an Integer
17
+ # or a Range, it will select multiple Resources of the Model.
18
+ #
19
+ # @return [DataMapper::Collection, DataMapper::Resource, nil]
20
+ # The selected Resource or Resources.
21
+ #
22
+ def [](*keys)
23
+ if keys.length > 1
24
+ super(*keys)
25
+ else
26
+ key = keys[0]
27
+
28
+ case key
29
+ when Integer, Range
30
+ super(*keys)
31
+ else
32
+ resource = nil
33
+
34
+ properties.each do |field|
35
+ if (field.index || field.unique?)
36
+ if field.primitive?(key)
37
+ resource = first(field => key)
38
+
39
+ break if resource
40
+ end
41
+ end
42
+ end
43
+
44
+ return resource
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,4 @@
1
+ require 'dm-core'
2
+ require 'dm-is-indexed/is/indexed'
3
+
4
+ DataMapper::Model.append_extensions DataMapper::Is::Indexed
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+ require 'integration/models/atom'
3
+
4
+ describe DataMapper::Is::Indexed do
5
+ before(:all) do
6
+ Atom.auto_migrate!
7
+
8
+ Atom.create(
9
+ :symbol => 'He',
10
+ :name => 'Helium',
11
+ :atomic_weight => 4.002602
12
+ )
13
+
14
+ Atom.create(
15
+ :symbol => 'Ne',
16
+ :name => 'Neon',
17
+ :atomic_weight => 20.1797
18
+ )
19
+
20
+ Atom.create(
21
+ :symbol => 'Ar',
22
+ :name => 'Argon',
23
+ :atomic_weight => 39.948
24
+ )
25
+
26
+ Atom.create(
27
+ :symbol => 'Kr',
28
+ :name => 'Krypton',
29
+ :atomic_weight => 83.798
30
+ )
31
+
32
+ Atom.create(
33
+ :symbol => 'Xe',
34
+ :name => 'Xeon',
35
+ :atomic_weight => 131.293
36
+ )
37
+
38
+ Atom.create(
39
+ :symbol => 'Rn',
40
+ :name => 'Radon',
41
+ :atomic_weight => 222.0
42
+ )
43
+
44
+ Atom.create(
45
+ :symbol => 'Uuo',
46
+ :name => 'Ununoctium',
47
+ :atomic_weight => 294.0
48
+ )
49
+ end
50
+
51
+ describe "Model.[]" do
52
+ subject { Atom }
53
+
54
+ it "should still allow selecting the nth resource" do
55
+ subject[0].symbol.should == 'He'
56
+ end
57
+
58
+ it "should still allowing selecting multiple resources" do
59
+ resources = subject[0,1]
60
+
61
+ resources[0].symbol.should == 'He'
62
+ resources[1].symbol.should == 'Ne'
63
+ end
64
+
65
+ it "should still allowing selecting ranges of resources" do
66
+ resources = subject[0..1]
67
+
68
+ resources[0].symbol.should == 'He'
69
+ resources[1].symbol.should == 'Ne'
70
+ end
71
+
72
+ it "should allow querying properties with unique indexes" do
73
+ subject['Ne'].name.should == 'Neon'
74
+ end
75
+
76
+ it "should query every property with an index" do
77
+ subject['Neon'].symbol.should == 'Ne'
78
+ end
79
+
80
+ it "should query properties with a non-unique index" do
81
+ subject[83.798].symbol.should == 'Kr'
82
+ end
83
+
84
+ it "should select the properties to query based on the key type" do
85
+ subject[83.798].symbol.should == 'Kr'
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,18 @@
1
+ require 'dm-core'
2
+ require 'dm-is-indexed'
3
+
4
+ class Atom
5
+
6
+ include DataMapper::Resource
7
+
8
+ is :indexed
9
+
10
+ property :id, Serial
11
+
12
+ property :symbol, String, :unique => true
13
+
14
+ property :name, String, :unique => true
15
+
16
+ property :atomic_weight, Float, :index => true
17
+
18
+ end
@@ -0,0 +1,12 @@
1
+ require 'rspec'
2
+ require 'dm-core/spec/setup'
3
+ require 'dm-core/spec/lib/adapter_helpers'
4
+
5
+ require 'dm-is-indexed'
6
+
7
+ DataMapper::Logger.new(STDERR, :debug) if ENV['DEBUG']
8
+ DataMapper::Spec.setup
9
+
10
+ RSpec.configure do |config|
11
+ config.extend(DataMapper::Spec::Adapters::Helpers)
12
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-is-indexed
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Postmodern
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-21 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: dm-core
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: "1.0"
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 1.0.0
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: yard
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.6.0
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ description: dm-is-indexed overloads the Model[] method, and allows querying Resources using their indexed properties.
50
+ email:
51
+ - postmodern.mod3@gmail.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files:
57
+ - README.md
58
+ - ChangeLog.md
59
+ - LICENSE.txt
60
+ files:
61
+ - .document
62
+ - .rspec
63
+ - .yardopts
64
+ - ChangeLog.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - dm-is-indexed.gemspec
70
+ - gemspec.yml
71
+ - lib/dm-is-indexed.rb
72
+ - lib/dm-is-indexed/is/indexed.rb
73
+ - spec/integration/indexed_spec.rb
74
+ - spec/integration/models/atom.rb
75
+ - spec/spec_helper.rb
76
+ has_rdoc: yard
77
+ homepage: http://github.com/postmodern/dm-is-indexed
78
+ licenses:
79
+ - MIT
80
+ post_install_message:
81
+ rdoc_options: []
82
+
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 1.3.6
97
+ requirements: []
98
+
99
+ rubyforge_project: dm-is-indexed
100
+ rubygems_version: 1.6.2
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Query DataMapper Resources via their indexed properties.
104
+ test_files:
105
+ - spec/integration/indexed_spec.rb