Veraticus / Dynamoid

Ruby ORM for Amazon's DynamoDB
http://joshsymonds.com/Dynamoid/
247 stars 83 forks source link

Default Adapter #70

Open luxx opened 12 years ago

luxx commented 12 years ago

I may be missing something, but there is a problem with the default configuration: the default value in lib/dynamoid/config.rb sets Dynamoid::Config.adapter to be 'aws-sdk', but this causes a problem with how the reconnect! method is currently implemented:

# lib\dynamoid\adapter.rb
module Dynamoid
  module Adapter
    def reconnect!
      require "dynamoid/adapter/#{Dynamoid::Config.adapter}" unless Dynamoid::Adapter.const_defined?(Dynamoid::Config.adapter.camelcase)
      @adapter = Dynamoid::Adapter.const_get(Dynamoid::Config.adapter.camelcase)
      @adapter.connect! if @adapter.respond_to?(:connect!)
      self.tables = benchmark('Cache Tables') {list_tables}
    end
  end
end

require "dynamoid/adapter/aws-sdk" will fail, the file name is actually aws_sdk. Also on the 2nd line Dynamoid::Config.adapter.camelcase gives 'Aws-sdk' which I think is supposed to be Dynamoid::Adapter::AwsSdk.

I found this method to be too complex and monkey patched it for my application to something more simple:

# lib\dynamoid\adapter.rb
module Dynamoid
  module Adapter
    def reconnect!
      @adapter = Dynamoid::Adapter::AwsSdk
      @adapter.connect!
      self.tables = benchmark('Cache Tables') {list_tables}
    end
  end
end

For a more general solution I would say, if you want to use the adapter in reconnect, leave the responsibility of the caller to require the file. I did not test this, but perhaps something like might do the trick:


# initializer
require 'some/custom_adapter'
Dynamoid.configure do |config|
  config.adapter = Some::CustomAdapter
end

# lib\dynamoid\adapter.rb
module Dynamoid
  module Adapter
    def reconnect!
      @adapter = Dynamoid::Config.adapter
      @adapter.connect!
      self.tables = benchmark('Cache Tables') {list_tables}
    end
  end
end

Even if you do not push the responsibility of requiring it to the caller, it should work with the defaults.

lisad commented 11 years ago

I too had the error with the bad constant "aws-sdk". It turns out my aws.yml file -- the file I added to include the AWS key and secret access key and dynamo-db-endpoint -- had not been checked in and included with the push.

scorcho commented 9 years ago

I keep getting the same flavor of errors. Standalone, inside of rails, using AWS sdk < v2 and >= v2. Does anyone have a working example of rails 4 working with Dynamoid, or even a standalone project?

.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/dynamoid-0.7.1/lib/dynamoid/adapter.rb:22:in `const_defined?': wrong constant name Aws-sdk (NameError)