Class: DataMapper::Adapters::XmlAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- DataMapper::Adapters::XmlAdapter
- Defined in:
- lib/dm-xml-adapter.rb
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.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/dm-xml-adapter.rb', line 61 def initialize(name, ) super = Hash.new [:directory] = [:directory] [:directory] ||= './db' @last_used_id = Hash.new @cache = XmlAdapterCache.new end |
Instance Method Details
#create(resources) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/dm-xml-adapter.rb', line 82 def create(resources) resources.each do |resource| model = resource.model id = find_free_id_for(resource.class.to_s) # find name of key attribute key = model.key.first.name resource.attributes[key] = id resource.instance_variable_set("@" + key.to_s, id) save(resource) end return resources.size end |
#create_model_storage(repository, model) ⇒ Object
78 79 80 |
# File 'lib/dm-xml-adapter.rb', line 78 def create_model_storage(repository, model) FileUtils.mkdir_p(classname_to_dir(model.to_s)) end |
#delete(collection) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/dm-xml-adapter.rb', line 95 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
74 75 76 |
# File 'lib/dm-xml-adapter.rb', line 74 def destroy_model_storage(repository, model) FileUtils.rm_rf(classname_to_dir(model.to_s)) end |
#read(query) ⇒ Object
125 126 127 |
# File 'lib/dm-xml-adapter.rb', line 125 def read(query) return filter_result_set(get_all(query.model), query) end |
#update(attributes, collection) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/dm-xml-adapter.rb', line 109 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 |