Created
May 8, 2014 23:28
-
-
Save yaauie/80770b162ad095a6e1e0 to your computer and use it in GitHub Desktop.
DataMapper InMemoryAddapter that respects your keys.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DataMapper | |
module Adapters | |
# DataMapper.setup(:default, 'in_memory_with_keys::') | |
class InMemoryWithKeysAdapter < InMemoryAdapter | |
def create(resources) | |
records = records_for(resources.first.model) | |
resources.each do |resource| | |
key_names = resource.model.properties(self.name).select(&:key?).map(&:name).map(&:to_s) | |
record_as_fields = attributes_as_fields(resource.attributes(nil)) | |
keys = extract_key_parts(key_names, record_as_fields) | |
collision = records.any? do |record| | |
extract_key_parts(key_names, record) == keys | |
end | |
raise 'DUPLICATE!' if collision | |
initialize_serial(resource, records.size.succ) | |
records << record_as_fields | |
end | |
end | |
private | |
def extract_key_parts(key_names, hash) | |
hash.values_at(*key_names) | |
end | |
end | |
const_added(:InMemoryWithKeysAdapter) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment