Class: MongoMapper::Document::VeneerInterface::ClassWrapper

Inherits:
Veneer::Base::ClassWrapper show all
Defined in:
lib/veneer/adapters/mongomapper/class_wrapper.rb

Constant Summary collapse

PRIMARY_KEYS =
[:_id]

Instance Attribute Summary

Attributes inherited from Veneer::Base::ClassWrapper

#klass, #opts

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Veneer::Base::ClassWrapper

#all, #create, #create!, #first, inherited, #initialize, #max, #min, subclasses, #sum, #validators_on

Constructor Details

This class inherits a constructor from Veneer::Base::ClassWrapper

Class Method Details

.model_classesObject



9
10
11
# File 'lib/veneer/adapters/mongomapper/class_wrapper.rb', line 9

def self.model_classes
  ::MongoMapper::Document.descendants
end

Instance Method Details

#collection_associationsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/veneer/adapters/mongomapper/class_wrapper.rb', line 17

def collection_associations
  @collection_associations ||= begin
    klass.associations.inject([]) do |ary, (name, assoc)|
      if assoc.is_a? ::MongoMapper::Plugins::Associations::ManyAssociation
        ary << {
          :name => name,
          :class => assoc.class_name.constantize
        }
      end
      ary
    end
  end
end

#count(opts = {}) ⇒ Object



77
78
79
# File 'lib/veneer/adapters/mongomapper/class_wrapper.rb', line 77

def count(opts={})
  opts[:conditions].nil? ? klass.count : klass.count(opts[:conditions])
end

#destroy_allObject



73
74
75
# File 'lib/veneer/adapters/mongomapper/class_wrapper.rb', line 73

def destroy_all
  klass.destroy_all
end

#find_first(opts) ⇒ Object



81
82
83
# File 'lib/veneer/adapters/mongomapper/class_wrapper.rb', line 81

def find_first(opts)
  klass.first(opts)
end

#find_many(opts) ⇒ Object



85
86
87
# File 'lib/veneer/adapters/mongomapper/class_wrapper.rb', line 85

def find_many(opts)
  klass.all(opts)
end

#member_associationsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/veneer/adapters/mongomapper/class_wrapper.rb', line 31

def member_associations
  @member_associations ||= begin
    types = [::MongoMapper::Plugins::Associations::BelongsToAssociation,
             ::MongoMapper::Plugins::Associations::ManyAssociation]
    klass.associations.inject([]) do |ary, (name, assoc)|
      if types.include?(assoc.class)
        ary << {
          :name => name,
          :class => assoc.class_name.constantize
        }
      end
      ary
    end
  end
end

#new(opts = {}) ⇒ Object



13
14
15
# File 'lib/veneer/adapters/mongomapper/class_wrapper.rb', line 13

def new(opts = {})
  ::Kernel::Veneer(klass.new(opts))
end

#primary_keysObject



69
70
71
# File 'lib/veneer/adapters/mongomapper/class_wrapper.rb', line 69

def primary_keys
  ::MongoMapper::Document::VeneerInterface::ClassWrapper::PRIMARY_KEYS
end

#propertiesObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/veneer/adapters/mongomapper/class_wrapper.rb', line 47

def properties
  @properties ||= begin
    klass.keys.map do |property|
      property = property[1]
      name = property.name.to_sym
      ::MongoMapper::Document::VeneerInterface::Property.new(
        self,
        {
          :name => name,
          :type => property.type,
          :constraints => {
            :length => property.options[:length],
            :nullable? => nil,
            :precision => nil,
          },
          :primary => primary_keys.include?(name),
        }
      )
    end
  end
end