Class: DataMapper::Adapters::XmlAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- DataMapper::Adapters::XmlAdapter
- Defined in:
- lib/dm-xml-adapter.rb
Class Method Summary collapse
Instance Method Summary collapse
- #create(resources) ⇒ Object
- #create_model_storage(repository, model) ⇒ Object
- #delete(collection) ⇒ Object
- #destroy_model_storage(repository, model) ⇒ Object
-
#initialize(name, options) ⇒ XmlAdapter
constructor
A new instance of XmlAdapter.
- #read(query) ⇒ Object
- #update(attributes, collection) ⇒ Object
Constructor Details
#initialize(name, options) ⇒ XmlAdapter
Returns a new instance of XmlAdapter.
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/dm-xml-adapter.rb', line 64 def initialize(name, ) super @@threadcount = 1 @options = Hash.new @options[:directory] = [:directory] @options[:directory] ||= './db' @last_used_id = Hash.new @cache = XmlAdapterCache.new end |
Class Method Details
.threads=(count) ⇒ Object
60 61 62 |
# File 'lib/dm-xml-adapter.rb', line 60 def self.threads=(count) @@threadcount = count end |
Instance Method Details
#create(resources) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/dm-xml-adapter.rb', line 86 def create(resources) key = resources.first.model.key.first.name resources.each do |resource| id = find_free_id_for(resources.first.class.to_s) # find name of key attribute resource.attributes[key] = id resource.instance_variable_set("@" + key.to_s, id) save(resource) end.size end |
#create_model_storage(repository, model) ⇒ Object
82 83 84 |
# File 'lib/dm-xml-adapter.rb', line 82 def create_model_storage(repository, model) FileUtils.mkdir_p(classname_to_dir(model.to_s)) end |
#delete(collection) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/dm-xml-adapter.rb', line 97 def delete(collection) collection.each do |result| key = result.key.first class_name = result.model.to_s @last_used_id[class_name] = key xml_destroy(result) # also remove from cache @cache.delete(class_name, key) # also remove from mtimes @cache.delete_mtime(class_name_to_file(class_name, key)) end return collection.size end |
#destroy_model_storage(repository, model) ⇒ Object
78 79 80 |
# File 'lib/dm-xml-adapter.rb', line 78 def destroy_model_storage(repository, model) FileUtils.rm_rf(classname_to_dir(model.to_s)) end |
#read(query) ⇒ Object
127 128 129 |
# File 'lib/dm-xml-adapter.rb', line 127 def read(query) return filter_result_set(get_all(query.model), query) end |
#update(attributes, collection) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/dm-xml-adapter.rb', line 111 def update(attributes, collection) # ok, for each object found we have to update the attribs we found # first thing is figure out what class we are dealing with # iterate over every object in this set and set the given attributes collection.each do |obj| attributes.each do |attrib| # attrib is an array # first member is Property object # second member is the value obj.instance_variable_set("@" + attrib[0].name.to_s, attrib[1]) end save(obj) end return collection end |