hashrocket-mongomapper 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. data/.gitignore +7 -0
  2. data/History +70 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +39 -0
  5. data/Rakefile +73 -0
  6. data/VERSION +1 -0
  7. data/bin/mmconsole +56 -0
  8. data/lib/mongomapper.rb +70 -0
  9. data/lib/mongomapper/associations.rb +84 -0
  10. data/lib/mongomapper/associations/base.rb +69 -0
  11. data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +34 -0
  12. data/lib/mongomapper/associations/belongs_to_proxy.rb +22 -0
  13. data/lib/mongomapper/associations/many_documents_proxy.rb +103 -0
  14. data/lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb +33 -0
  15. data/lib/mongomapper/associations/many_embedded_proxy.rb +17 -0
  16. data/lib/mongomapper/associations/many_polymorphic_proxy.rb +11 -0
  17. data/lib/mongomapper/associations/many_proxy.rb +6 -0
  18. data/lib/mongomapper/associations/proxy.rb +63 -0
  19. data/lib/mongomapper/callbacks.rb +106 -0
  20. data/lib/mongomapper/document.rb +337 -0
  21. data/lib/mongomapper/dynamic_finder.rb +38 -0
  22. data/lib/mongomapper/embedded_document.rb +267 -0
  23. data/lib/mongomapper/finder_options.rb +85 -0
  24. data/lib/mongomapper/key.rb +76 -0
  25. data/lib/mongomapper/observing.rb +50 -0
  26. data/lib/mongomapper/pagination.rb +52 -0
  27. data/lib/mongomapper/rails_compatibility/document.rb +15 -0
  28. data/lib/mongomapper/rails_compatibility/embedded_document.rb +25 -0
  29. data/lib/mongomapper/save_with_validation.rb +19 -0
  30. data/lib/mongomapper/serialization.rb +55 -0
  31. data/lib/mongomapper/serializers/json_serializer.rb +92 -0
  32. data/lib/mongomapper/support.rb +30 -0
  33. data/lib/mongomapper/validations.rb +61 -0
  34. data/mongomapper.gemspec +142 -0
  35. data/test/NOTE_ON_TESTING +1 -0
  36. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +53 -0
  37. data/test/functional/associations/test_belongs_to_proxy.rb +45 -0
  38. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +131 -0
  39. data/test/functional/associations/test_many_embedded_proxy.rb +106 -0
  40. data/test/functional/associations/test_many_polymorphic_proxy.rb +261 -0
  41. data/test/functional/associations/test_many_proxy.rb +295 -0
  42. data/test/functional/test_associations.rb +47 -0
  43. data/test/functional/test_callbacks.rb +85 -0
  44. data/test/functional/test_document.rb +952 -0
  45. data/test/functional/test_pagination.rb +81 -0
  46. data/test/functional/test_rails_compatibility.rb +30 -0
  47. data/test/functional/test_validations.rb +172 -0
  48. data/test/models.rb +139 -0
  49. data/test/test_helper.rb +67 -0
  50. data/test/unit/serializers/test_json_serializer.rb +157 -0
  51. data/test/unit/test_association_base.rb +144 -0
  52. data/test/unit/test_document.rb +123 -0
  53. data/test/unit/test_embedded_document.rb +526 -0
  54. data/test/unit/test_finder_options.rb +183 -0
  55. data/test/unit/test_key.rb +247 -0
  56. data/test/unit/test_mongomapper.rb +28 -0
  57. data/test/unit/test_observing.rb +101 -0
  58. data/test/unit/test_pagination.rb +113 -0
  59. data/test/unit/test_rails_compatibility.rb +34 -0
  60. data/test/unit/test_serializations.rb +52 -0
  61. data/test/unit/test_validations.rb +500 -0
  62. metadata +189 -0
@@ -0,0 +1,50 @@
1
+ require 'observer'
2
+ require 'singleton'
3
+ require 'set'
4
+
5
+ module MongoMapper
6
+ module Observing #:nodoc:
7
+ def self.included(model)
8
+ model.class_eval do
9
+ extend Observable
10
+ end
11
+ end
12
+ end
13
+
14
+ class Observer
15
+ include Singleton
16
+
17
+ class << self
18
+ def observe(*models)
19
+ models.flatten!
20
+ models.collect! { |model| model.is_a?(Symbol) ? model.to_s.camelize.constantize : model }
21
+ define_method(:observed_classes) { Set.new(models) }
22
+ end
23
+
24
+ def observed_class
25
+ if observed_class_name = name[/(.*)Observer/, 1]
26
+ observed_class_name.constantize
27
+ else
28
+ nil
29
+ end
30
+ end
31
+ end
32
+
33
+ def initialize
34
+ Set.new(observed_classes).each { |klass| add_observer! klass }
35
+ end
36
+
37
+ def update(observed_method, object) #:nodoc:
38
+ send(observed_method, object) if respond_to?(observed_method)
39
+ end
40
+
41
+ protected
42
+ def observed_classes
43
+ Set.new([self.class.observed_class].compact.flatten)
44
+ end
45
+
46
+ def add_observer!(klass)
47
+ klass.add_observer(self)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,52 @@
1
+ module MongoMapper
2
+ module Pagination
3
+ class PaginationProxy < BasicObject
4
+ attr_accessor :subject
5
+ attr_reader :total_entries, :per_page, :current_page
6
+ alias limit per_page
7
+
8
+ def initialize(total_entries, current_page, per_page=nil)
9
+ @total_entries = total_entries.to_i
10
+ self.per_page = per_page
11
+ self.current_page = current_page
12
+ end
13
+
14
+ def total_pages
15
+ (total_entries / per_page.to_f).ceil
16
+ end
17
+
18
+ def out_of_bounds?
19
+ current_page > total_pages
20
+ end
21
+
22
+ def previous_page
23
+ current_page > 1 ? (current_page - 1) : nil
24
+ end
25
+
26
+ def next_page
27
+ current_page < total_pages ? (current_page + 1) : nil
28
+ end
29
+
30
+ def skip
31
+ (current_page - 1) * per_page
32
+ end
33
+ alias offset skip
34
+
35
+ def method_missing(name, *args, &block)
36
+ @subject.send(name, *args, &block)
37
+ end
38
+
39
+ private
40
+ def per_page=(value)
41
+ value = 25 if value.blank?
42
+ @per_page = value.to_i
43
+ end
44
+
45
+ def current_page=(value)
46
+ value = value.to_i
47
+ value = 1 if value < 1
48
+ @current_page = value
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,15 @@
1
+ module MongoMapper
2
+ module RailsCompatibility
3
+ module Document
4
+ def self.included(model)
5
+ model.class_eval do
6
+ alias_method :new_record?, :new?
7
+ end
8
+ end
9
+
10
+ def to_param
11
+ id
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ module MongoMapper
2
+ module RailsCompatibility
3
+ module EmbeddedDocument
4
+ def self.included(model)
5
+ model.class_eval do
6
+ extend ClassMethods
7
+ end
8
+
9
+ class << model
10
+ alias has_many many
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ def column_names
16
+ keys.keys
17
+ end
18
+ end
19
+
20
+ def to_param
21
+ raise "Missing to_param method in #{self.class}. You should implement it to return the unique identifier of this embedded document within a document."
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,19 @@
1
+ module MongoMapper
2
+ module SaveWithValidation
3
+ def self.included(base)
4
+ base.class_eval do
5
+ alias_method_chain :save, :validation
6
+ alias_method_chain :save!, :validation
7
+ end
8
+ end
9
+
10
+ private
11
+ def save_with_validation
12
+ valid? ? save_without_validation : false
13
+ end
14
+
15
+ def save_with_validation!
16
+ valid? ? save_without_validation! : raise(DocumentNotValid.new(self))
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,55 @@
1
+ require 'active_support/json'
2
+
3
+ module MongoMapper #:nodoc:
4
+ module Serialization
5
+ class Serializer #:nodoc:
6
+ attr_reader :options
7
+
8
+ def initialize(record, options = {})
9
+ @record, @options = record, options.dup
10
+ end
11
+
12
+ def serializable_key_names
13
+ key_names = @record.class.keys.keys
14
+
15
+ if options[:only]
16
+ options.delete(:except)
17
+ key_names = key_names & Array(options[:only]).collect { |n| n.to_s }
18
+ else
19
+ options[:except] = Array(options[:except])
20
+ key_names = key_names - options[:except].collect { |n| n.to_s }
21
+ end
22
+
23
+ key_names
24
+ end
25
+
26
+ def serializable_method_names
27
+ Array(options[:methods]).inject([]) do |method_attributes, name|
28
+ method_attributes << name if @record.respond_to?(name.to_s)
29
+ method_attributes
30
+ end
31
+ end
32
+
33
+ def serializable_names
34
+ serializable_key_names + serializable_method_names
35
+ end
36
+
37
+ def serializable_record
38
+ returning(serializable_record = {}) do
39
+ serializable_names.each { |name| serializable_record[name] = @record.send(name) }
40
+ end
41
+ end
42
+
43
+ def serialize
44
+ # overwrite to implement
45
+ end
46
+
47
+ def to_s(&block)
48
+ serialize(&block)
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ dir = Pathname(__FILE__).dirname.expand_path + 'serializers'
55
+ require dir + 'json_serializer'
@@ -0,0 +1,92 @@
1
+ module MongoMapper #:nodoc:
2
+ module Serialization
3
+ def self.included(base)
4
+ base.cattr_accessor :include_root_in_json, :instance_writer => false
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ # Returns a JSON string representing the model. Some configuration is
9
+ # available through +options+.
10
+ #
11
+ # The option <tt>include_root_in_json</tt> controls the top-level behavior of
12
+ # to_json. When it is <tt>true</tt>, to_json will emit a single root node named
13
+ # after the object's type. For example:
14
+ #
15
+ # konata = User.find(1)
16
+ # User.include_root_in_json = true
17
+ # konata.to_json
18
+ # # => { "user": {"id": 1, "name": "Konata Izumi", "age": 16,
19
+ # "created_at": "2006/08/01", "awesome": true} }
20
+ #
21
+ # User.include_root_in_json = false
22
+ # konata.to_json
23
+ # # => {"id": 1, "name": "Konata Izumi", "age": 16,
24
+ # "created_at": "2006/08/01", "awesome": true}
25
+ #
26
+ # The remainder of the examples in this section assume include_root_in_json is set to
27
+ # <tt>false</tt>.
28
+ #
29
+ # Without any +options+, the returned JSON string will include all
30
+ # the model's attributes. For example:
31
+ #
32
+ # konata = User.find(1)
33
+ # konata.to_json
34
+ # # => {"id": 1, "name": "Konata Izumi", "age": 16,
35
+ # "created_at": "2006/08/01", "awesome": true}
36
+ #
37
+ # The <tt>:only</tt> and <tt>:except</tt> options can be used to limit the attributes
38
+ # included, and work similar to the +attributes+ method. For example:
39
+ #
40
+ # konata.to_json(:only => [ :id, :name ])
41
+ # # => {"id": 1, "name": "Konata Izumi"}
42
+ #
43
+ # konata.to_json(:except => [ :id, :created_at, :age ])
44
+ # # => {"name": "Konata Izumi", "awesome": true}
45
+ #
46
+ # To include any methods on the model, use <tt>:methods</tt>.
47
+ #
48
+ # konata.to_json(:methods => :permalink)
49
+ # # => {"id": 1, "name": "Konata Izumi", "age": 16,
50
+ # "created_at": "2006/08/01", "awesome": true,
51
+ # "permalink": "1-konata-izumi"}
52
+ def to_json(options = {})
53
+ apply_to_json_defaults(options)
54
+
55
+ if include_root_in_json
56
+ "{#{self.class.json_class_name}: #{JsonSerializer.new(self, options).to_s}}"
57
+ else
58
+ JsonSerializer.new(self, options).to_s
59
+ end
60
+ end
61
+
62
+ def from_json(json)
63
+ self.attributes = ActiveSupport::JSON.decode(json)
64
+ self
65
+ end
66
+
67
+ class JsonSerializer < MongoMapper::Serialization::Serializer #:nodoc:
68
+ def serialize
69
+ serializable_record.to_json
70
+ end
71
+ end
72
+
73
+ module ClassMethods
74
+ def json_class_name
75
+ @json_class_name ||= name.demodulize.underscore.inspect
76
+ end
77
+ end
78
+
79
+ private
80
+ def apply_to_json_defaults(options)
81
+ unless options[:only]
82
+ methods = [options.delete(:methods)].flatten.compact
83
+ methods << :id
84
+ options[:methods] = methods.uniq
85
+ end
86
+
87
+ except = [options.delete(:except)].flatten.compact
88
+ except << :_id
89
+ options[:except] = except
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,30 @@
1
+ class BasicObject #:nodoc:
2
+ instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|instance_eval|proxy_|^object_id$)/ }
3
+ end unless defined?(BasicObject)
4
+
5
+ class Boolean
6
+ def self.mm_typecast(value)
7
+ ['true', 't', '1'].include?(value.to_s.downcase)
8
+ end
9
+ end
10
+
11
+ class Object
12
+ # The hidden singleton lurks behind everyone
13
+ def metaclass
14
+ class << self; self end
15
+ end
16
+
17
+ def meta_eval(&blk)
18
+ metaclass.instance_eval(&blk)
19
+ end
20
+
21
+ # Adds methods to a metaclass
22
+ def meta_def(name, &blk)
23
+ meta_eval { define_method(name, &blk) }
24
+ end
25
+
26
+ # Defines an instance method within a class
27
+ def class_def(name, &blk)
28
+ class_eval { define_method(name, &blk) }
29
+ end
30
+ end
@@ -0,0 +1,61 @@
1
+ module MongoMapper
2
+ module Validations
3
+ module Macros
4
+ def validates_uniqueness_of(*args)
5
+ add_validations(args, MongoMapper::Validations::ValidatesUniquenessOf)
6
+ end
7
+
8
+ def validates_exclusion_of(*args)
9
+ add_validations(args, MongoMapper::Validations::ValidatesExclusionOf)
10
+ end
11
+
12
+ def validates_inclusion_of(*args)
13
+ add_validations(args, MongoMapper::Validations::ValidatesInclusionOf)
14
+ end
15
+ end
16
+
17
+ class ValidatesUniquenessOf < Validatable::ValidationBase
18
+ def valid?(instance)
19
+ # TODO: scope
20
+ doc = instance.class.find(:first, :conditions => {self.attribute => instance[attribute]}, :limit => 1)
21
+ doc.nil? || instance.id == doc.id
22
+ end
23
+
24
+ def message(instance)
25
+ super || "has already been taken"
26
+ end
27
+ end
28
+
29
+ class ValidatesExclusionOf < Validatable::ValidationBase
30
+ required_option :within
31
+
32
+ def valid?(instance)
33
+ value = instance[attribute]
34
+ return true if allow_nil && value.nil?
35
+ return true if allow_blank && value.blank?
36
+
37
+ !within.include?(instance[attribute])
38
+ end
39
+
40
+ def message(instance)
41
+ super || "is reserved"
42
+ end
43
+ end
44
+
45
+ class ValidatesInclusionOf < Validatable::ValidationBase
46
+ required_option :within
47
+
48
+ def valid?(instance)
49
+ value = instance[attribute]
50
+ return true if allow_nil && value.nil?
51
+ return true if allow_blank && value.blank?
52
+
53
+ within.include?(value)
54
+ end
55
+
56
+ def message(instance)
57
+ super || "is not in the list"
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,142 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mongomapper}
8
+ s.version = "0.3.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["John Nunemaker"]
12
+ s.date = %q{2009-08-25}
13
+ s.default_executable = %q{mmconsole}
14
+ s.email = %q{nunemaker@gmail.com}
15
+ s.executables = ["mmconsole"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".gitignore",
22
+ "History",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "bin/mmconsole",
28
+ "lib/mongomapper.rb",
29
+ "lib/mongomapper/associations.rb",
30
+ "lib/mongomapper/associations/base.rb",
31
+ "lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb",
32
+ "lib/mongomapper/associations/belongs_to_proxy.rb",
33
+ "lib/mongomapper/associations/many_documents_proxy.rb",
34
+ "lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb",
35
+ "lib/mongomapper/associations/many_embedded_proxy.rb",
36
+ "lib/mongomapper/associations/many_polymorphic_proxy.rb",
37
+ "lib/mongomapper/associations/many_proxy.rb",
38
+ "lib/mongomapper/associations/proxy.rb",
39
+ "lib/mongomapper/callbacks.rb",
40
+ "lib/mongomapper/document.rb",
41
+ "lib/mongomapper/dynamic_finder.rb",
42
+ "lib/mongomapper/embedded_document.rb",
43
+ "lib/mongomapper/finder_options.rb",
44
+ "lib/mongomapper/key.rb",
45
+ "lib/mongomapper/observing.rb",
46
+ "lib/mongomapper/pagination.rb",
47
+ "lib/mongomapper/rails_compatibility/document.rb",
48
+ "lib/mongomapper/rails_compatibility/embedded_document.rb",
49
+ "lib/mongomapper/save_with_validation.rb",
50
+ "lib/mongomapper/serialization.rb",
51
+ "lib/mongomapper/serializers/json_serializer.rb",
52
+ "lib/mongomapper/support.rb",
53
+ "lib/mongomapper/validations.rb",
54
+ "mongomapper.gemspec",
55
+ "test/NOTE_ON_TESTING",
56
+ "test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
57
+ "test/functional/associations/test_belongs_to_proxy.rb",
58
+ "test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
59
+ "test/functional/associations/test_many_embedded_proxy.rb",
60
+ "test/functional/associations/test_many_polymorphic_proxy.rb",
61
+ "test/functional/associations/test_many_proxy.rb",
62
+ "test/functional/test_associations.rb",
63
+ "test/functional/test_callbacks.rb",
64
+ "test/functional/test_document.rb",
65
+ "test/functional/test_pagination.rb",
66
+ "test/functional/test_rails_compatibility.rb",
67
+ "test/functional/test_validations.rb",
68
+ "test/models.rb",
69
+ "test/test_helper.rb",
70
+ "test/unit/serializers/test_json_serializer.rb",
71
+ "test/unit/test_association_base.rb",
72
+ "test/unit/test_document.rb",
73
+ "test/unit/test_embedded_document.rb",
74
+ "test/unit/test_finder_options.rb",
75
+ "test/unit/test_key.rb",
76
+ "test/unit/test_mongomapper.rb",
77
+ "test/unit/test_observing.rb",
78
+ "test/unit/test_pagination.rb",
79
+ "test/unit/test_rails_compatibility.rb",
80
+ "test/unit/test_serializations.rb",
81
+ "test/unit/test_validations.rb"
82
+ ]
83
+ s.homepage = %q{http://github.com/jnunemaker/mongomapper}
84
+ s.rdoc_options = ["--charset=UTF-8"]
85
+ s.require_paths = ["lib"]
86
+ s.rubyforge_project = %q{mongomapper}
87
+ s.rubygems_version = %q{1.3.5}
88
+ s.summary = %q{Awesome gem for modeling your domain and storing it in mongo}
89
+ s.test_files = [
90
+ "test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
91
+ "test/functional/associations/test_belongs_to_proxy.rb",
92
+ "test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
93
+ "test/functional/associations/test_many_embedded_proxy.rb",
94
+ "test/functional/associations/test_many_polymorphic_proxy.rb",
95
+ "test/functional/associations/test_many_proxy.rb",
96
+ "test/functional/test_associations.rb",
97
+ "test/functional/test_callbacks.rb",
98
+ "test/functional/test_document.rb",
99
+ "test/functional/test_pagination.rb",
100
+ "test/functional/test_rails_compatibility.rb",
101
+ "test/functional/test_validations.rb",
102
+ "test/models.rb",
103
+ "test/test_helper.rb",
104
+ "test/unit/serializers/test_json_serializer.rb",
105
+ "test/unit/test_association_base.rb",
106
+ "test/unit/test_document.rb",
107
+ "test/unit/test_embedded_document.rb",
108
+ "test/unit/test_finder_options.rb",
109
+ "test/unit/test_key.rb",
110
+ "test/unit/test_mongomapper.rb",
111
+ "test/unit/test_observing.rb",
112
+ "test/unit/test_pagination.rb",
113
+ "test/unit/test_rails_compatibility.rb",
114
+ "test/unit/test_serializations.rb",
115
+ "test/unit/test_validations.rb"
116
+ ]
117
+
118
+ if s.respond_to? :specification_version then
119
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
120
+ s.specification_version = 3
121
+
122
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
123
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
124
+ s.add_runtime_dependency(%q<mongodb-mongo>, ["= 0.11.1"])
125
+ s.add_runtime_dependency(%q<validatable>, ["= 1.7.2"])
126
+ s.add_development_dependency(%q<mocha>, ["= 0.9.4"])
127
+ s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
128
+ else
129
+ s.add_dependency(%q<activesupport>, [">= 0"])
130
+ s.add_dependency(%q<mongodb-mongo>, ["= 0.11.1"])
131
+ s.add_dependency(%q<validatable>, ["= 1.7.2"])
132
+ s.add_dependency(%q<mocha>, ["= 0.9.4"])
133
+ s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
134
+ end
135
+ else
136
+ s.add_dependency(%q<activesupport>, [">= 0"])
137
+ s.add_dependency(%q<mongodb-mongo>, ["= 0.11.1"])
138
+ s.add_dependency(%q<validatable>, ["= 1.7.2"])
139
+ s.add_dependency(%q<mocha>, ["= 0.9.4"])
140
+ s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
141
+ end
142
+ end