dm-xml-adapter 0.584 → 0.587
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/dm-xml-adapter.rb +22 -24
- data/spec/dm-xml-adapter_spec.rb +14 -2
- metadata +8 -38
    
        data/lib/dm-xml-adapter.rb
    CHANGED
    
    | @@ -7,8 +7,7 @@ require 'pp' | |
| 7 7 | 
             
            gem 'libxml-ruby', '>= 0.8.3'
         | 
| 8 8 | 
             
            require 'xml'
         | 
| 9 9 | 
             
            require 'tempfile'
         | 
| 10 | 
            -
            require ' | 
| 11 | 
            -
            require 'log4r'
         | 
| 10 | 
            +
            require 'logger'
         | 
| 12 11 |  | 
| 13 12 | 
             
            # http://redcorundum.blogspot.com/2006/05/kernelqualifiedconstget.html
         | 
| 14 13 | 
             
            module Kernel
         | 
| @@ -56,7 +55,6 @@ module DataMapper::Adapters | |
| 56 55 |  | 
| 57 56 | 
             
                def put(classname, id, object)
         | 
| 58 57 | 
             
                  # check to see if we have a classname entry
         | 
| 59 | 
            -
                  #puts "putting a cache entry of id: #{id}"
         | 
| 60 58 | 
             
                  if (@classes[classname] == nil)
         | 
| 61 59 | 
             
                    @classes[classname] = Hash.new
         | 
| 62 60 | 
             
                  end
         | 
| @@ -73,25 +71,33 @@ module DataMapper::Adapters | |
| 73 71 | 
             
                end
         | 
| 74 72 |  | 
| 75 73 | 
             
                def get(classname, id)
         | 
| 76 | 
            -
                  #puts "getting a cache entry for id: #{id}"
         | 
| 77 74 | 
             
                  hash = @classes[classname]
         | 
| 78 75 | 
             
                  return hash[id]
         | 
| 79 76 | 
             
                end
         | 
| 80 77 | 
             
              end
         | 
| 81 78 |  | 
| 82 79 | 
             
              class XmlAdapter < AbstractAdapter
         | 
| 83 | 
            -
                 | 
| 84 | 
            -
             | 
| 80 | 
            +
                
         | 
| 81 | 
            +
                def loglevel=(loglevel)
         | 
| 82 | 
            +
                  @options[:loglevel] = loglevel
         | 
| 83 | 
            +
                  @logger.level = loglevel
         | 
| 84 | 
            +
                end
         | 
| 85 | 
            +
                
         | 
| 86 | 
            +
                def logger=(logger)
         | 
| 87 | 
            +
                  @options[:logger] = logger
         | 
| 88 | 
            +
                  @logger = logger
         | 
| 85 89 | 
             
                end
         | 
| 86 90 |  | 
| 87 91 | 
             
                def initialize(name, options)
         | 
| 88 92 | 
             
                  super
         | 
| 89 93 |  | 
| 90 | 
            -
                   | 
| 91 | 
            -
                  @options = Hash.new
         | 
| 92 | 
            -
                  @options[:directory] = options[:directory]
         | 
| 94 | 
            +
                  @options = options
         | 
| 93 95 | 
             
                  @options[:directory] ||= './db'
         | 
| 94 | 
            -
             | 
| 96 | 
            +
                  @options[:loglevel] ||= Logger::FATAL
         | 
| 97 | 
            +
                  @options[:logger] ||= Logger.new(STDERR)
         | 
| 98 | 
            +
                  @logger = @options[:logger]
         | 
| 99 | 
            +
                  @logger.level = @options[:loglevel]
         | 
| 100 | 
            +
                  @logger.debug("DM-XML-Adapter Initialized!")
         | 
| 95 101 | 
             
                  @last_used_id = Hash.new
         | 
| 96 102 |  | 
| 97 103 | 
             
                  @cache = XmlAdapterCache.new
         | 
| @@ -100,10 +106,12 @@ module DataMapper::Adapters | |
| 100 106 |  | 
| 101 107 | 
             
                def destroy_model_storage(model)
         | 
| 102 108 | 
             
                  FileUtils.rm_rf(classname_to_dir(model.to_s))
         | 
| 109 | 
            +
                  @logger.debug("Removing model storage for #{model}")
         | 
| 103 110 | 
             
                end
         | 
| 104 111 |  | 
| 105 112 | 
             
                def create_model_storage(model)
         | 
| 106 113 | 
             
                  FileUtils.mkdir_p(classname_to_dir(model.to_s))
         | 
| 114 | 
            +
                  @logger.debug("Creating model storage for: #{model}")
         | 
| 107 115 | 
             
                end
         | 
| 108 116 |  | 
| 109 117 | 
             
                def create(resources)
         | 
| @@ -111,7 +119,6 @@ module DataMapper::Adapters | |
| 111 119 | 
             
                  resources.each do |resource|
         | 
| 112 120 | 
             
                    id = find_free_id_for(resources.first.class.to_s)
         | 
| 113 121 | 
             
                    # find name of key attribute
         | 
| 114 | 
            -
                    #puts "we are creating a new resource with id: #{id}"
         | 
| 115 122 | 
             
                    resource.attributes[key] = id
         | 
| 116 123 | 
             
                    resource.instance_variable_set("@" + key.to_s, id)
         | 
| 117 124 | 
             
                    save(resource)
         | 
| @@ -124,6 +131,7 @@ module DataMapper::Adapters | |
| 124 131 | 
             
                    class_name = result.model.to_s
         | 
| 125 132 | 
             
                    @last_used_id[class_name] = key
         | 
| 126 133 | 
             
                    xml_destroy(result)
         | 
| 134 | 
            +
                    @logger.debug("Destroying #{result}")
         | 
| 127 135 | 
             
                    # also remove from cache
         | 
| 128 136 | 
             
                    @cache.delete(class_name, key)
         | 
| 129 137 | 
             
                    # also remove from mtimes
         | 
| @@ -142,6 +150,7 @@ module DataMapper::Adapters | |
| 142 150 | 
             
                      # first member is Property object
         | 
| 143 151 | 
             
                      # second member is the value
         | 
| 144 152 | 
             
                      obj.instance_variable_set("@" + attrib[0].name.to_s, attrib[1])
         | 
| 153 | 
            +
                      @logger.debug("Updating instance variable #{attrib[0].name.to_s} to #{attrib[1]}")
         | 
| 145 154 | 
             
                    end
         | 
| 146 155 | 
             
                    save(obj)
         | 
| 147 156 | 
             
                  end
         | 
| @@ -168,6 +177,7 @@ module DataMapper::Adapters | |
| 168 177 |  | 
| 169 178 | 
             
                def get_all(model)
         | 
| 170 179 | 
             
                  model_name = model.to_s
         | 
| 180 | 
            +
                  @logger.debug("Getting all for model #{model_name}")
         | 
| 171 181 | 
             
                  directory = classname_to_dir(model_name)
         | 
| 172 182 | 
             
                  if ! File.exists?(directory)
         | 
| 173 183 | 
             
                    return []
         | 
| @@ -250,9 +260,7 @@ module DataMapper::Adapters | |
| 250 260 | 
             
                  # if there are no entries in the directory
         | 
| 251 261 | 
             
                  # or the directory doesn't exist
         | 
| 252 262 | 
             
                  # we need to create it...
         | 
| 253 | 
            -
                  #puts "trying to find a free id for #{class_name}"
         | 
| 254 263 | 
             
                  if ! File.exists?(classname_to_dir(class_name))
         | 
| 255 | 
            -
                    #puts "the directory doesn't exist"
         | 
| 256 264 | 
             
                    # default ID
         | 
| 257 265 | 
             
                    return 1
         | 
| 258 266 | 
             
                  end
         | 
| @@ -263,13 +271,10 @@ module DataMapper::Adapters | |
| 263 271 | 
             
                  id = @last_used_id[class_name] || 1
         | 
| 264 272 | 
             
                  while true do
         | 
| 265 273 | 
             
                    filename = File.join(directory.path, id.to_s + ".xml")
         | 
| 266 | 
            -
                    #puts "checking for the existence of #{filename}"
         | 
| 267 274 | 
             
                    if ! File.exists?(filename)
         | 
| 268 275 | 
             
                      @last_used_id[class_name] = id
         | 
| 269 | 
            -
                      #puts "found free id: #{id}"
         | 
| 270 276 | 
             
                      return id
         | 
| 271 277 | 
             
                    end
         | 
| 272 | 
            -
                    #puts "the file existed..."
         | 
| 273 278 | 
             
                    id += 1
         | 
| 274 279 | 
             
                  end
         | 
| 275 280 | 
             
                end
         | 
| @@ -278,7 +283,6 @@ module DataMapper::Adapters | |
| 278 283 | 
             
                  # since we're saving, purge the cache
         | 
| 279 284 | 
             
                  resource_class = resource.class.to_s
         | 
| 280 285 | 
             
                  resource_key = resource.key.first
         | 
| 281 | 
            -
                  #puts "the key of the oject we are saving: #{resource_key}"
         | 
| 282 286 | 
             
                  @cache.delete(resource_class, resource_key)
         | 
| 283 287 | 
             
                  @cache.delete_mtime(class_name_to_file(resource_class, resource_key))
         | 
| 284 288 |  | 
| @@ -307,14 +311,12 @@ module DataMapper::Adapters | |
| 307 311 | 
             
                    end
         | 
| 308 312 | 
             
                  end
         | 
| 309 313 |  | 
| 310 | 
            -
                  #puts "the key is now: #{resource_key}"
         | 
| 311 314 | 
             
                  xmlfile = File.new(class_name_to_file(resource_class, resource_key), "w")
         | 
| 312 | 
            -
                  #puts "saving to #{xmlfile.path}"
         | 
| 313 315 | 
             
                  tempfile = Tempfile.new("dm-xml-adapter", Dir.tmpdir())
         | 
| 314 316 | 
             
                  tempfile.puts out_string
         | 
| 315 | 
            -
                  #puts "the content is: #{out_string}"
         | 
| 316 317 | 
             
                  tempfile.close
         | 
| 317 318 | 
             
                  FileUtils.mv(tempfile.path,xmlfile.path)
         | 
| 319 | 
            +
                  @logger.debug("Saved to: #{xmlfile.path}")
         | 
| 318 320 | 
             
                  return xmlfile
         | 
| 319 321 |  | 
| 320 322 | 
             
                end
         | 
| @@ -328,10 +330,6 @@ module DataMapper::Adapters | |
| 328 330 | 
             
                  return File.join(classname_to_dir(class_name), id.to_s + ".xml")
         | 
| 329 331 | 
             
                end
         | 
| 330 332 |  | 
| 331 | 
            -
                #memoize(:file_to_id)
         | 
| 332 | 
            -
                #memoize(:classname_to_dir)
         | 
| 333 | 
            -
                #memoize(:class_name_to_file)
         | 
| 334 | 
            -
             | 
| 335 333 | 
             
              end
         | 
| 336 334 | 
             
            end
         | 
| 337 335 |  | 
    
        data/spec/dm-xml-adapter_spec.rb
    CHANGED
    
    | @@ -4,8 +4,6 @@ require 'pathname' | |
| 4 4 | 
             
            require 'dm-migrations'
         | 
| 5 5 | 
             
            require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
         | 
| 6 6 |  | 
| 7 | 
            -
            DataMapper::Adapters::XmlAdapter.threads = 2
         | 
| 8 | 
            -
             | 
| 9 7 | 
             
            describe DataMapper::Adapters::XmlAdapter do
         | 
| 10 8 | 
             
              before(:each) do
         | 
| 11 9 | 
             
                @adapter = DataMapper.setup(:default, {:adapter => 'xml', :directory => 'db'})
         | 
| @@ -174,6 +172,20 @@ describe DataMapper::Adapters::XmlAdapter do | |
| 174 172 | 
             
                  end
         | 
| 175 173 | 
             
                end
         | 
| 176 174 |  | 
| 175 | 
            +
                describe "logging support" do      
         | 
| 176 | 
            +
                  it "should work with logging" do
         | 
| 177 | 
            +
                    @adapter.loglevel = Logger::DEBUG
         | 
| 178 | 
            +
                    @adapter.logger = Logger.new("/dev/null")
         | 
| 179 | 
            +
                    XMLTest::User.create(:name => "contentking", :content => "cool")
         | 
| 180 | 
            +
                    XMLTest::User.all.size.should == 1
         | 
| 181 | 
            +
                    DataMapper.repository.adapter.loglevel = Logger::DEBUG
         | 
| 182 | 
            +
                    DataMapper.repository.adapter.logger = Logger.new(STDOUT)
         | 
| 183 | 
            +
                    XMLTest::User.create(:name => "contentking", :content => "cool")
         | 
| 184 | 
            +
                    XMLTest::User.all.size.should == 2
         | 
| 185 | 
            +
                  end
         | 
| 186 | 
            +
             | 
| 187 | 
            +
                end
         | 
| 188 | 
            +
             | 
| 177 189 | 
             
              end
         | 
| 178 190 |  | 
| 179 191 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,12 +1,12 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: dm-xml-adapter
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            -
              hash:  | 
| 5 | 
            -
              prerelease:  | 
| 4 | 
            +
              hash: 1181
         | 
| 5 | 
            +
              prerelease: 
         | 
| 6 6 | 
             
              segments: 
         | 
| 7 7 | 
             
              - 0
         | 
| 8 | 
            -
              -  | 
| 9 | 
            -
              version: "0. | 
| 8 | 
            +
              - 587
         | 
| 9 | 
            +
              version: "0.587"
         | 
| 10 10 | 
             
            platform: ruby
         | 
| 11 11 | 
             
            authors: 
         | 
| 12 12 | 
             
            - Joshua Harding
         | 
| @@ -14,8 +14,7 @@ autorequire: dm-xml-adapter | |
| 14 14 | 
             
            bindir: bin
         | 
| 15 15 | 
             
            cert_chain: []
         | 
| 16 16 |  | 
| 17 | 
            -
            date:  | 
| 18 | 
            -
            default_executable: 
         | 
| 17 | 
            +
            date: 2012-02-12 00:00:00 Z
         | 
| 19 18 | 
             
            dependencies: 
         | 
| 20 19 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| 21 20 | 
             
              name: libxml-ruby
         | 
| @@ -46,7 +45,7 @@ dependencies: | |
| 46 45 | 
             
              type: :runtime
         | 
| 47 46 | 
             
              version_requirements: *id002
         | 
| 48 47 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| 49 | 
            -
              name:  | 
| 48 | 
            +
              name: dm-core
         | 
| 50 49 | 
             
              prerelease: false
         | 
| 51 50 | 
             
              requirement: &id003 !ruby/object:Gem::Requirement 
         | 
| 52 51 | 
             
                none: false
         | 
| @@ -60,7 +59,7 @@ dependencies: | |
| 60 59 | 
             
              type: :runtime
         | 
| 61 60 | 
             
              version_requirements: *id003
         | 
| 62 61 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| 63 | 
            -
              name:  | 
| 62 | 
            +
              name: dm-migrations
         | 
| 64 63 | 
             
              prerelease: false
         | 
| 65 64 | 
             
              requirement: &id004 !ruby/object:Gem::Requirement 
         | 
| 66 65 | 
             
                none: false
         | 
| @@ -73,34 +72,6 @@ dependencies: | |
| 73 72 | 
             
                    version: "0"
         | 
| 74 73 | 
             
              type: :runtime
         | 
| 75 74 | 
             
              version_requirements: *id004
         | 
| 76 | 
            -
            - !ruby/object:Gem::Dependency 
         | 
| 77 | 
            -
              name: dm-core
         | 
| 78 | 
            -
              prerelease: false
         | 
| 79 | 
            -
              requirement: &id005 !ruby/object:Gem::Requirement 
         | 
| 80 | 
            -
                none: false
         | 
| 81 | 
            -
                requirements: 
         | 
| 82 | 
            -
                - - ">="
         | 
| 83 | 
            -
                  - !ruby/object:Gem::Version 
         | 
| 84 | 
            -
                    hash: 3
         | 
| 85 | 
            -
                    segments: 
         | 
| 86 | 
            -
                    - 0
         | 
| 87 | 
            -
                    version: "0"
         | 
| 88 | 
            -
              type: :runtime
         | 
| 89 | 
            -
              version_requirements: *id005
         | 
| 90 | 
            -
            - !ruby/object:Gem::Dependency 
         | 
| 91 | 
            -
              name: dm-migrations
         | 
| 92 | 
            -
              prerelease: false
         | 
| 93 | 
            -
              requirement: &id006 !ruby/object:Gem::Requirement 
         | 
| 94 | 
            -
                none: false
         | 
| 95 | 
            -
                requirements: 
         | 
| 96 | 
            -
                - - ">="
         | 
| 97 | 
            -
                  - !ruby/object:Gem::Version 
         | 
| 98 | 
            -
                    hash: 3
         | 
| 99 | 
            -
                    segments: 
         | 
| 100 | 
            -
                    - 0
         | 
| 101 | 
            -
                    version: "0"
         | 
| 102 | 
            -
              type: :runtime
         | 
| 103 | 
            -
              version_requirements: *id006
         | 
| 104 75 | 
             
            description: 
         | 
| 105 76 | 
             
            email: josh@statewidesoftware.com
         | 
| 106 77 | 
             
            executables: []
         | 
| @@ -115,7 +86,6 @@ files: | |
| 115 86 | 
             
            - spec/dm-xml-adapter_spec.rb
         | 
| 116 87 | 
             
            - spec/spec_helper.rb
         | 
| 117 88 | 
             
            - README
         | 
| 118 | 
            -
            has_rdoc: true
         | 
| 119 89 | 
             
            homepage: 
         | 
| 120 90 | 
             
            licenses: []
         | 
| 121 91 |  | 
| @@ -145,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 145 115 | 
             
            requirements: []
         | 
| 146 116 |  | 
| 147 117 | 
             
            rubyforge_project: 
         | 
| 148 | 
            -
            rubygems_version: 1. | 
| 118 | 
            +
            rubygems_version: 1.8.6
         | 
| 149 119 | 
             
            signing_key: 
         | 
| 150 120 | 
             
            specification_version: 3
         | 
| 151 121 | 
             
            summary: a XML adapter for DataMapper. this adapter allows you to use DataMapper with XML files as a backing store.
         |