mongoid_ext 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/.document +5 -0
  2. data/Gemfile +20 -0
  3. data/Gemfile.lock +50 -0
  4. data/LICENSE +20 -0
  5. data/README.rdoc +17 -0
  6. data/Rakefile +44 -0
  7. data/VERSION +1 -0
  8. data/bin/mongoid_console +85 -0
  9. data/lib/mongoid_ext.rb +71 -0
  10. data/lib/mongoid_ext/criteria_ext.rb +15 -0
  11. data/lib/mongoid_ext/document_ext.rb +29 -0
  12. data/lib/mongoid_ext/file.rb +86 -0
  13. data/lib/mongoid_ext/file_list.rb +74 -0
  14. data/lib/mongoid_ext/file_server.rb +69 -0
  15. data/lib/mongoid_ext/filter.rb +266 -0
  16. data/lib/mongoid_ext/filter/parser.rb +71 -0
  17. data/lib/mongoid_ext/filter/result_set.rb +75 -0
  18. data/lib/mongoid_ext/js/filter.js +41 -0
  19. data/lib/mongoid_ext/js/find_tags.js +26 -0
  20. data/lib/mongoid_ext/js/tag_cloud.js +28 -0
  21. data/lib/mongoid_ext/modifiers.rb +93 -0
  22. data/lib/mongoid_ext/mongo_mapper.rb +63 -0
  23. data/lib/mongoid_ext/paranoia.rb +100 -0
  24. data/lib/mongoid_ext/patches.rb +17 -0
  25. data/lib/mongoid_ext/random.rb +23 -0
  26. data/lib/mongoid_ext/slugizer.rb +84 -0
  27. data/lib/mongoid_ext/storage.rb +110 -0
  28. data/lib/mongoid_ext/tags.rb +26 -0
  29. data/lib/mongoid_ext/types/embedded_hash.rb +25 -0
  30. data/lib/mongoid_ext/types/open_struct.rb +15 -0
  31. data/lib/mongoid_ext/types/timestamp.rb +15 -0
  32. data/lib/mongoid_ext/types/translation.rb +51 -0
  33. data/lib/mongoid_ext/update.rb +11 -0
  34. data/lib/mongoid_ext/versioning.rb +189 -0
  35. data/lib/mongoid_ext/voteable.rb +104 -0
  36. data/mongoid_ext.gemspec +129 -0
  37. data/test/helper.rb +30 -0
  38. data/test/models.rb +80 -0
  39. data/test/support/custom_matchers.rb +55 -0
  40. data/test/test_filter.rb +51 -0
  41. data/test/test_modifiers.rb +65 -0
  42. data/test/test_paranoia.rb +40 -0
  43. data/test/test_random.rb +57 -0
  44. data/test/test_slugizer.rb +66 -0
  45. data/test/test_storage.rb +110 -0
  46. data/test/test_tags.rb +47 -0
  47. data/test/test_update.rb +16 -0
  48. data/test/test_versioning.rb +55 -0
  49. data/test/test_voteable.rb +77 -0
  50. data/test/types/test_open_struct.rb +22 -0
  51. data/test/types/test_set.rb +26 -0
  52. data/test/types/test_timestamp.rb +40 -0
  53. metadata +301 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ source :gemcutter
2
+
3
+ gem 'mongoid', '~> 2'
4
+ gem 'uuidtools', '>= 2.1.1'
5
+ gem 'i18n'
6
+ gem 'tzinfo'
7
+ gem 'differ', '>= 0.1.2'
8
+
9
+ group :development do
10
+ gem "yard", "~> 0.6.0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.5.2"
13
+
14
+ gem "shoulda", "~> 2.11.3"
15
+ gem "jnunemaker-matchy", "~> 0.4"
16
+ gem 'shoulda', '~> 2.11.3'
17
+ gem 'mocha', '~> 0.9.4'
18
+ gem 'timecop'
19
+ end
20
+
data/Gemfile.lock ADDED
@@ -0,0 +1,50 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.6)
5
+ activesupport (= 3.0.6)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.5.0)
8
+ activesupport (3.0.6)
9
+ bson (1.3.0)
10
+ builder (2.1.2)
11
+ differ (0.1.2)
12
+ git (1.2.5)
13
+ i18n (0.5.0)
14
+ jeweler (1.5.2)
15
+ bundler (~> 1.0.0)
16
+ git (>= 1.2.5)
17
+ rake
18
+ jnunemaker-matchy (0.4.0)
19
+ mocha (0.9.12)
20
+ mongo (1.3.0)
21
+ bson (>= 1.3.0)
22
+ mongoid (2.0.1)
23
+ activemodel (~> 3.0)
24
+ mongo (~> 1.3)
25
+ tzinfo (~> 0.3.22)
26
+ will_paginate (~> 3.0.pre)
27
+ rake (0.8.7)
28
+ shoulda (2.11.3)
29
+ timecop (0.3.5)
30
+ tzinfo (0.3.26)
31
+ uuidtools (2.1.2)
32
+ will_paginate (3.0.pre2)
33
+ yard (0.6.7)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ bundler (~> 1.0.0)
40
+ differ (>= 0.1.2)
41
+ i18n
42
+ jeweler (~> 1.5.2)
43
+ jnunemaker-matchy (~> 0.4)
44
+ mocha (~> 0.9.4)
45
+ mongoid (~> 2.0.0)
46
+ shoulda (~> 2.11.3)
47
+ timecop
48
+ tzinfo
49
+ uuidtools (>= 2.1.1)
50
+ yard (~> 0.6.0)
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 David A. Cuadrado
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.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = mongoid_ext
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 David A. Cuadrado. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "mongoid_ext"
8
+ gem.summary = %Q{mongoid plugins}
9
+ gem.description = %Q{mongoid plugins}
10
+ gem.email = "krawek@gmail.com"
11
+ gem.homepage = "http://github.com/dcu/mongoid_ext"
12
+ gem.authors = ["David A. Cuadrado"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :default => :test
41
+
42
+ require 'yard'
43
+ YARD::Rake::YardocTask.new
44
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.6.1
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
3
+
4
+ begin
5
+ require 'mongoid_ext'
6
+ require 'irb'
7
+ rescue LoadError
8
+ require 'rubygems'
9
+ retry
10
+ end
11
+
12
+ IRB.setup(nil)
13
+ irb = IRB::Irb.new
14
+
15
+ IRB.conf[:MAIN_CONTEXT] = irb.context
16
+
17
+ irb.context.evaluate("require 'irb/completion'", 0)
18
+ irb.context.evaluate(%@
19
+ include Mongoid
20
+
21
+ Mongoid.configure do |config|
22
+ config.master = Mongo::Connection.new.db("test")
23
+ end
24
+ $db = Mongoid.config.database
25
+
26
+ @, 0)
27
+
28
+ puts %@
29
+ Welcome to the Mongoid Console!
30
+
31
+ Example 1: Using the driver
32
+ things = $db.collection("things")
33
+ things.insert("name" => "Raw Thing")
34
+ things.insert("name" => "Another Thing", "date" => Time.now)
35
+
36
+ cursor = things.find("name" => "Raw Thing")
37
+ puts cursor.next_object.inspect
38
+
39
+ Example 2: Defining documents
40
+ class Person
41
+ include Mongoid::Document
42
+ field :first_name
43
+ field :middle_initial
44
+ field :last_name
45
+ end
46
+
47
+ Example 3: Associations
48
+ class Person
49
+ include Mongoid::Document
50
+ references_one :policy
51
+ references_many :prescriptions
52
+ references_many :preferences, :stored_as => :array, :inverse_of => :people
53
+ end
54
+
55
+ class Policy
56
+ include Mongoid::Document
57
+ referenced_in :person
58
+ end
59
+
60
+ class Prescription
61
+ include Mongoid::Document
62
+ referenced_in :person
63
+ end
64
+
65
+ class Preference
66
+ include Mongoid::Document
67
+ references_many :people, :stored_as => :array, :inverse_of => :preferences
68
+ end
69
+
70
+ person = Person.create
71
+ policy = Policy.create
72
+ prescription = Prescription.create
73
+
74
+ person.policy = policy
75
+ person.prescriptions = [prescription]
76
+ @
77
+
78
+ trap("SIGINT") do
79
+ irb.signal_handle
80
+ end
81
+
82
+ catch(:IRB_EXIT) do
83
+ irb.eval_input
84
+ end
85
+
@@ -0,0 +1,71 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ if RUBY_VERSION =~ /^1\.8/
4
+ $KCODE = 'u'
5
+ end
6
+
7
+ require 'mongoid'
8
+ require 'uuidtools'
9
+ require 'differ'
10
+ require 'active_support/inflector'
11
+
12
+ begin
13
+ require 'magic'
14
+ rescue LoadError
15
+ $stderr.puts "disabling `magic` support. use 'gem install magic' to enable it"
16
+ end
17
+
18
+ require 'mongoid_ext/patches'
19
+
20
+ # types
21
+ require 'mongoid_ext/types/open_struct'
22
+ require 'mongoid_ext/types/timestamp'
23
+ require 'mongoid_ext/types/translation'
24
+ require 'mongoid_ext/types/embedded_hash'
25
+
26
+ # storage
27
+ require 'mongoid_ext/file_list'
28
+ require 'mongoid_ext/file'
29
+ require 'mongoid_ext/storage'
30
+ require 'mongoid_ext/file_server'
31
+
32
+ # update
33
+ require 'mongoid_ext/update'
34
+
35
+ # filter
36
+ require 'mongoid_ext/filter'
37
+ require 'mongoid_ext/filter/parser'
38
+ require 'mongoid_ext/filter/result_set'
39
+
40
+ # slug
41
+ require 'mongoid_ext/slugizer'
42
+
43
+ # tags
44
+ require 'mongoid_ext/tags'
45
+
46
+ require 'mongoid_ext/versioning'
47
+ require 'mongoid_ext/voteable'
48
+ require 'mongoid_ext/paranoia'
49
+
50
+ require 'mongoid_ext/random'
51
+ require 'mongoid_ext/mongo_mapper'
52
+ require 'mongoid_ext/document_ext'
53
+ require 'mongoid_ext/criteria_ext'
54
+ require 'mongoid_ext/modifiers'
55
+
56
+ module MongoidExt
57
+ def self.init
58
+ load_jsfiles(::File.dirname(__FILE__)+"/mongoid_ext/js")
59
+ end
60
+
61
+ def self.load_jsfiles(path)
62
+ Dir.glob(::File.join(path, "*.js")) do |js_path|
63
+ code = ::File.read(js_path)
64
+ name = ::File.basename(js_path, ".js")
65
+
66
+ # HACK: looks like ruby driver doesn't support this
67
+ Mongoid.master.eval("db.system.js.save({_id: '#{name}', value: #{code}})")
68
+ end
69
+ end
70
+ end
71
+
@@ -0,0 +1,15 @@
1
+ module MongoidExt
2
+ module CriteriaExt
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ module ClassMethods
9
+ end
10
+
11
+ module InstanceMethods
12
+ end
13
+ end
14
+ end
15
+ Mongoid::Criteria.send(:include, MongoidExt::CriteriaExt)
@@ -0,0 +1,29 @@
1
+ module MongoidExt
2
+ module DocumentExt
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ module ClassMethods
9
+ def find!(*args)
10
+ find(*args) || raise(Mongoid::Errors::DocumentNotFound.new(self, args))
11
+ end
12
+
13
+ end
14
+
15
+ module InstanceMethods
16
+ def raw_save(opts = {})
17
+ return true if !changed? && !opts.delete(:force)
18
+
19
+ if (opts.delete(:validate) != false || valid?)
20
+ self.collection.save(raw_attributes, opts)
21
+ true
22
+ else
23
+ false
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ Mongoid::Document.send(:include, MongoidExt::DocumentExt)
@@ -0,0 +1,86 @@
1
+ module MongoidExt
2
+ class File < EmbeddedHash
3
+ attr_accessor :_root_document
4
+
5
+ field :name, :type => String
6
+ field :extension, :type => String
7
+ field :content_type, :type => String
8
+
9
+ alias :filename :name
10
+
11
+ def put(filename, io, options = {})
12
+ options[:_id] = grid_filename
13
+
14
+ options[:metadata] ||= {}
15
+ options[:metadata][:collection] = _root_document.collection.name
16
+
17
+ self["name"] = filename
18
+ if filename =~ /\.([\w]{2,4})$/
19
+ self["extension"] = $1
20
+ end
21
+
22
+ if io.kind_of?(String)
23
+ io = StringIO.new(io)
24
+ end
25
+
26
+ if defined?(Magic) && Magic.respond_to?(:guess_string_mime_type)
27
+ data = io.read(256) # be nice with memory usage
28
+ self["content_type"] = options[:content_type] = Magic.guess_string_mime_type(data.to_s)
29
+ self["extension"] ||= options[:content_type].to_s.split("/").last.split("-").last
30
+
31
+ if io.respond_to?(:rewind)
32
+ io.rewind
33
+ else
34
+ io.seek(0)
35
+ end
36
+ end
37
+
38
+ options[:filename] = grid_filename
39
+ gridfs.delete(grid_filename)
40
+ gridfs.put(io, options)
41
+ end
42
+
43
+ def get
44
+ @io ||= gridfs.get(grid_filename)
45
+ end
46
+
47
+ def reset
48
+ @io = nil
49
+ end
50
+
51
+ def grid_filename
52
+ @grid_filename ||= "#{_root_document.collection.name}/#{self.id}"
53
+ end
54
+
55
+ def mime_type
56
+ self.content_type || get.content_type
57
+ end
58
+
59
+ def size
60
+ get.file_length
61
+ end
62
+
63
+ def read(size = nil)
64
+ self.get.read(size)
65
+ end
66
+
67
+ def delete
68
+ @io = nil
69
+ gridfs.delete(grid_filename)
70
+ end
71
+
72
+ def method_missing(name, *args, &block)
73
+ f = self.get rescue nil
74
+ if f && f.respond_to?(name)
75
+ f.send(name, *args, &block)
76
+ else
77
+ super(name, *args, &block)
78
+ end
79
+ end
80
+
81
+ protected
82
+ def gridfs
83
+ _root_document.class.gridfs
84
+ end
85
+ end
86
+ end