mongomodel 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,6 +17,7 @@ module MongoModel
17
17
  autoload :Attributes, 'mongomodel/concerns/attributes'
18
18
  autoload :AttributeMethods, 'mongomodel/concerns/attribute_methods'
19
19
  autoload :Associations, 'mongomodel/concerns/associations'
20
+ autoload :Translation, 'mongomodel/concerns/translation'
20
21
  autoload :Validations, 'mongomodel/concerns/validations'
21
22
  autoload :Callbacks, 'mongomodel/concerns/callbacks'
22
23
  autoload :Logging, 'mongomodel/concerns/logging'
@@ -1,3 +1,5 @@
1
+ require 'active_support/core_ext/module/introspection'
2
+
1
3
  module MongoModel
2
4
  module Associations
3
5
  module Base
@@ -69,12 +69,12 @@ module MongoModel
69
69
 
70
70
  private
71
71
  def class_for_type(type)
72
- type = type.constantize
72
+ klass = type.constantize
73
73
 
74
- if (subclasses + [name]).include?(type.to_s)
75
- type
74
+ if (subclasses + [name]).include?(type)
75
+ klass
76
76
  else
77
- raise DocumentNotFound, "Document not of the correct type (got #{type.to_s})"
77
+ raise DocumentNotFound, "Document not of the correct type (got #{type})"
78
78
  end
79
79
  rescue NameError
80
80
  self
@@ -0,0 +1,10 @@
1
+ module MongoModel
2
+ module Translation
3
+ extend ActiveModel::Translation
4
+
5
+ # Set the i18n scope to overwrite ActiveModel.
6
+ def i18n_scope #:nodoc:
7
+ :mongomodel
8
+ end
9
+ end
10
+ end
@@ -14,11 +14,6 @@ module MongoModel
14
14
 
15
15
  property
16
16
  end
17
-
18
- # Set the i18n scope to overwrite ActiveModel.
19
- def i18n_scope #:nodoc:
20
- :mongomodel
21
- end
22
17
  end
23
18
 
24
19
  def valid?
@@ -11,6 +11,8 @@ module MongoModel
11
11
  include Attributes
12
12
  include Properties
13
13
 
14
+ extend Translation
15
+
14
16
  include Validations
15
17
  include Callbacks
16
18
 
@@ -1,3 +1,5 @@
1
+ require 'active_support/core_ext/object/blank'
2
+
1
3
  class Boolean < TrueClass; end
2
4
 
3
5
  class Symbol
@@ -9,52 +11,62 @@ class Symbol
9
11
  define_method(:desc) { MongoModel::MongoOrder::Clause.new(self, :descending) }
10
12
  end
11
13
 
12
- class Object
13
- begin
14
- ObjectSpace.each_object(Class.new) {}
15
-
16
- # Exclude this class unless it's a subclass of our supers and is defined.
17
- # We check defined? in case we find a removed class that has yet to be
18
- # garbage collected. This also fails for anonymous classes -- please
19
- # submit a patch if you have a workaround.
20
- def subclasses_of(*superclasses) #:nodoc:
14
+ class Class
15
+ # Rubinius
16
+ if defined?(Class.__subclasses__)
17
+ def descendents
21
18
  subclasses = []
22
-
23
- superclasses.each do |sup|
24
- ObjectSpace.each_object(class << sup; self; end) do |k|
25
- if k != sup && (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id"))
26
- subclasses << k
27
- end
28
- end
29
- end
30
-
19
+ __subclasses__.each {|k| subclasses << k; subclasses.concat k.descendents }
31
20
  subclasses
32
21
  end
33
- rescue RuntimeError
34
- # JRuby and any implementations which cannot handle the objectspace traversal
35
- # above fall back to this implementation
36
- def subclasses_of(*superclasses) #:nodoc:
37
- subclasses = []
22
+ else
23
+ # MRI
24
+ begin
25
+ ObjectSpace.each_object(Class.new) {}
38
26
 
39
- superclasses.each do |sup|
27
+ def descendents
28
+ subclasses = []
29
+ ObjectSpace.each_object(class << self; self; end) do |k|
30
+ subclasses << k unless k == self
31
+ end
32
+ subclasses
33
+ end
34
+ # JRuby
35
+ rescue StandardError
36
+ def descendents
37
+ subclasses = []
40
38
  ObjectSpace.each_object(Class) do |k|
41
- if superclasses.any? { |superclass| k < superclass } &&
42
- (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id"))
43
- subclasses << k
44
- end
39
+ subclasses << k if k < self
45
40
  end
46
41
  subclasses.uniq!
42
+ subclasses
47
43
  end
48
- subclasses
49
44
  end
50
45
  end
51
- end
52
-
53
- class Class
54
- # Returns an array with the names of the subclasses of +self+ as strings.
55
- #
56
- # Integer.subclasses # => ["Bignum", "Fixnum"]
46
+
47
+ def reachable?
48
+ eval("defined?(::#{self}) && ::#{self}.equal?(self)")
49
+ end
50
+
57
51
  def subclasses
58
52
  Object.subclasses_of(self).map { |o| o.to_s }
59
53
  end
60
54
  end
55
+
56
+ class Object
57
+ def remove_subclasses_of(*superclasses) #:nodoc:
58
+ Class.remove_class(*subclasses_of(*superclasses))
59
+ end
60
+
61
+ # Exclude this class unless it's a subclass of our supers and is defined.
62
+ # We check defined? in case we find a removed class that has yet to be
63
+ # garbage collected. This also fails for anonymous classes -- please
64
+ # submit a patch if you have a workaround.
65
+ def subclasses_of(*superclasses) #:nodoc:
66
+ subclasses = []
67
+ superclasses.each do |klass|
68
+ subclasses.concat klass.descendents.select {|k| k.name.blank? || k.reachable?}
69
+ end
70
+ subclasses
71
+ end
72
+ end
@@ -1,3 +1,3 @@
1
1
  module MongoModel
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongomodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Pohlenz
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-28 00:00:00 +10:30
12
+ date: 2010-02-10 00:00:00 +10:30
13
13
  default_executable: console
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -83,6 +83,7 @@ files:
83
83
  - lib/mongomodel/concerns/properties.rb
84
84
  - lib/mongomodel/concerns/record_status.rb
85
85
  - lib/mongomodel/concerns/timestamps.rb
86
+ - lib/mongomodel/concerns/translation.rb
86
87
  - lib/mongomodel/concerns/validations.rb
87
88
  - lib/mongomodel/concerns/validations/associated.rb
88
89
  - lib/mongomodel/document.rb