Module: DataMapper::Support::ActiveRecordImpersonation::ClassMethods
- Defined in:
- lib/data_mapper/support/active_record_impersonation.rb
Instance Method Summary collapse
- #[](*keys) ⇒ Object
- #all(options = {}) ⇒ Object
- #count(*args) ⇒ Object
- #create(attributes) ⇒ Object
- #create!(attributes) ⇒ Object
- #delete_all ⇒ Object
- #each(options = {}, &b) ⇒ Object
- #find(type_or_id, options = {}) ⇒ Object
- #find_by_sql(*args) ⇒ Object
- #find_or_create(search_attributes, create_attributes = {}) ⇒ Object
- #first(*args) ⇒ Object
- #get(*keys) ⇒ Object
- #truncate! ⇒ Object
Instance Method Details
#[](*keys) ⇒ Object
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( = {}) database.all(self, ) 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
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_all ⇒ Object
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
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/data_mapper/support/active_record_impersonation.rb', line 38 def each( = {}, &b) raise ArgumentError.new(":offset is not supported with the #each method") if .has_key?(:offset) offset = 0 limit = [:limit] || (self::const_defined?('DEFAULT_LIMIT') ? self::DEFAULT_LIMIT : 500) until (results = all(.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, = {}) case type_or_id when :first then first() when :all then all() else first(type_or_id, ) 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 |