Module: DataMapper::Support::ActiveRecordImpersonation::ClassMethods

Defined in:
lib/data_mapper/support/active_record_impersonation.rb

Instance Method Summary collapse

Instance Method Details

#[](*keys) ⇒ Object

Raises:

  • (ArgumentError)


82
83
84
85
# File 'lib/data_mapper/support/active_record_impersonation.rb', line 82

def [](*keys)
  raise ArgumentError.new('Hash is not a valid key') if keys.size == 1 && keys.first.is_a?(Hash)
  database.get(self, keys)
end

#all(options = {}) ⇒ Object



34
35
36
# File 'lib/data_mapper/support/active_record_impersonation.rb', line 34

def all(options = {})
  database.all(self, options)
end

#count(*args) ⇒ Object



54
55
56
# File 'lib/data_mapper/support/active_record_impersonation.rb', line 54

def count(*args)
  database.count(self, *args)
end

#create(attributes) ⇒ Object



87
88
89
90
91
# File 'lib/data_mapper/support/active_record_impersonation.rb', line 87

def create(attributes)
  instance = self.new(attributes)
  instance.save
  instance
end

#create!(attributes) ⇒ Object

Raises:



93
94
95
96
97
98
# File 'lib/data_mapper/support/active_record_impersonation.rb', line 93

def create!(attributes)
  instance = self.new(attributes)
  instance.save
  raise InvalidRecord.new(instance) if instance.new_record?
  instance
end

#delete_allObject



58
59
60
# File 'lib/data_mapper/support/active_record_impersonation.rb', line 58

def delete_all
  database.delete_all(self)
end

#each(options = {}, &b) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/data_mapper/support/active_record_impersonation.rb', line 38

def each(options = {}, &b)
  raise ArgumentError.new(":offset is not supported with the #each method") if options.has_key?(:offset)

  offset = 0
  limit = options[:limit] || (self::const_defined?('DEFAULT_LIMIT') ? self::DEFAULT_LIMIT : 500)

  until (results = all(options.merge(:limit => limit, :offset => offset))).empty?
    results.each(&b)
    offset += limit
  end
end

#find(type_or_id, options = {}) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/data_mapper/support/active_record_impersonation.rb', line 66

def find(type_or_id, options = {})
  case type_or_id
    when :first then first(options)
    when :all then all(options)
    else first(type_or_id, options)
  end
end

#find_by_sql(*args) ⇒ Object



74
75
76
# File 'lib/data_mapper/support/active_record_impersonation.rb', line 74

def find_by_sql(*args)
  DataMapper::database.query(*args)
end

#find_or_create(search_attributes, create_attributes = {}) ⇒ Object



30
31
32
# File 'lib/data_mapper/support/active_record_impersonation.rb', line 30

def find_or_create(search_attributes, create_attributes = {})
  first(search_attributes) || create(search_attributes.merge(create_attributes))
end

#first(*args) ⇒ Object



50
51
52
# File 'lib/data_mapper/support/active_record_impersonation.rb', line 50

def first(*args)
  database.first(self, *args)
end

#get(*keys) ⇒ Object



78
79
80
# File 'lib/data_mapper/support/active_record_impersonation.rb', line 78

def get(*keys)
  database.get(self, keys)
end

#truncate!Object



62
63
64
# File 'lib/data_mapper/support/active_record_impersonation.rb', line 62

def truncate!
  database.truncate(self)
end