Class: DataMapper::Adapters::XmlAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/dm-xml-adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ XmlAdapter

Returns a new instance of XmlAdapter.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dm-xml-adapter.rb', line 74

def initialize(name, options)
  super

  @options = Hash.new
  @options[:directory] = options[:directory]
  @options[:directory] ||= './db'         
   
  @last_used_id = Hash.new
  
  @cache = XmlAdapterCache.new
  
  #@@logger = Log4r::Logger.new 'adapter'
  #@@logger.outputters = Log4r::Outputter.stdout

end

Instance Method Details

#create(resources) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dm-xml-adapter.rb', line 98

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



94
95
96
# File 'lib/dm-xml-adapter.rb', line 94

def create_model_storage(repository, model)
 FileUtils.mkdir_p(classname_to_dir(model.to_s))
end

#delete(collection) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/dm-xml-adapter.rb', line 111

def delete(collection)
 collection.each do |result|
    @last_used_id[result.class.to_s] = result.key.first
  xml_destroy(result)
  # also remove from cache
  @cache.delete(result.model.to_s, result.key.first)
  # also remove from mtimes
  @cache.delete_mtime(class_name_to_file(result.model.to_s, result.key.first))
 end
 return collection.size
end

#destroy_model_storage(repository, model) ⇒ Object



90
91
92
# File 'lib/dm-xml-adapter.rb', line 90

def destroy_model_storage(repository, model)
 FileUtils.rm_rf(classname_to_dir(model.to_s))
end

#read(query) ⇒ Object



139
140
141
# File 'lib/dm-xml-adapter.rb', line 139

def read(query)
 return filter_result_set(get_all(query.model), query)
end

#update(attributes, collection) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/dm-xml-adapter.rb', line 123

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