Class: DataMapper::Resource::VeneerInterface::ClassWrapper

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

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, subclasses

Constructor Details

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

Class Method Details

.model_classesObject



5
6
7
# File 'lib/veneer/adapters/datamapper/class_wrapper.rb', line 5

def self.model_classes
  ::DataMapper::Model.descendants
end

Instance Method Details

#collection_associationsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/veneer/adapters/datamapper/class_wrapper.rb', line 13

def collection_associations
  @collection_associations ||= begin
    result = klass.relationships.inject([]) do |ary, rel|
      if rel.max > 1
        ary << {
          :name  => rel.name,
          :class => rel.child_model
        }
      end
      ary
    end
    result
  end
end

#count(opts = {}) ⇒ Object



80
81
82
# File 'lib/veneer/adapters/datamapper/class_wrapper.rb', line 80

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

#destroy_allObject



68
69
70
# File 'lib/veneer/adapters/datamapper/class_wrapper.rb', line 68

def destroy_all
  klass.all.destroy
end

#find_first(opts) ⇒ Object



72
73
74
# File 'lib/veneer/adapters/datamapper/class_wrapper.rb', line 72

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

#find_many(opts) ⇒ Object



76
77
78
# File 'lib/veneer/adapters/datamapper/class_wrapper.rb', line 76

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

#max(field, opts = {}) ⇒ Object



94
95
96
97
# File 'lib/veneer/adapters/datamapper/class_wrapper.rb', line 94

def max(field, opts={})
  opts = ::Hashie::Mash.new(opts)
  klass.all(dm_conditions_from_opts(opts)).max(field)
end

#member_associationsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/veneer/adapters/datamapper/class_wrapper.rb', line 28

def member_associations
  @member_associations ||= begin
    result = klass.relationships.inject([]) do |ary, rel|
      if rel.max == 1
        ary << {
          :name  => rel.name,
          :class => rel.parent_model
        }
      end
      ary
    end
    result
  end
end

#min(field, opts = {}) ⇒ Object



89
90
91
92
# File 'lib/veneer/adapters/datamapper/class_wrapper.rb', line 89

def min(field, opts={})
  opts = ::Hashie::Mash.new(opts)
  klass.all(dm_conditions_from_opts(opts)).min(field)
end

#new(opts = {}) ⇒ Object



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

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

#primary_keysObject



64
65
66
# File 'lib/veneer/adapters/datamapper/class_wrapper.rb', line 64

def primary_keys
  @primary_keys ||= klass.key.map { |key| key.name }
end

#propertiesObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/veneer/adapters/datamapper/class_wrapper.rb', line 43

def properties
  @properties ||= begin
    klass.properties.map do |property|
      ::DataMapper::Resource::VeneerInterface::Property.new(
        self,
        {
          :name => property.name,
          :type => property,
          :constraints => {
            :length => property.options[:length],
            :nullable? => property.options[:allow_nil],
            :precision => property.options[:precision],
            :scale => property.options[:scale],
          },
          :primary => primary_keys.include?(property.name),
        }
      )
    end
  end
end

#sum(field, opts = {}) ⇒ Object



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

def sum(field, opts={})
  opts = ::Hashie::Mash.new(opts)
  klass.all(dm_conditions_from_opts(opts)).sum(field)
end

#validators_on(name) ⇒ Object

Delegate to validators_on if ActiveModel::Validations has been included in the model



101
102
103
# File 'lib/veneer/adapters/datamapper/class_wrapper.rb', line 101

def validators_on(name)
  klass <=> ::ActiveModel::Validations ? klass.validators_on(name) : []
end