Airtable / airtable-ruby

Access Airtable data sheets with ease using ruby
MIT License
163 stars 72 forks source link

uninitialized constant Airtable::Record::HashWithIndifferentAccess #5

Open dmerand opened 8 years ago

dmerand commented 8 years ago

Hi,

I've been working on getting this gem to work for some time, and I'm stuck. I'm wondering if the issue is my setup or the gem.

Here's my setup code:

#!/usr/bin/env ruby

require 'airtable'

at_key = 'KEY_REDACTED'
at_base = 'BASE_REDACTED'

@client = Airtable::Client.new(at_key)
@table = @client.table(at_base, "Videos")
@records = @table.records
p @records.inspect

...pretty much straight from the README. When I run that code, I get this error:

/Users/donald/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/airtable-0.0.8/lib/airtable/record.rb:33:in override_attributes!': uninitialized constant Airtable::Record::HashWithIndifferentAccess (NameError) from /Users/donald/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/airtable-0.0.8/lib/airtable/record.rb:5:ininitialize' from /Users/donald/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/airtable-0.0.8/lib/airtable/record_set.rb:13:in new' from /Users/donald/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/airtable-0.0.8/lib/airtable/record_set.rb:13:inblock in initialize' from /Users/donald/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/airtable-0.0.8/lib/airtable/record_set.rb:13:in map' from /Users/donald/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/airtable-0.0.8/lib/airtable/record_set.rb:13:ininitialize' from /Users/donald/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/airtable-0.0.8/lib/airtable/table.rb:27:in new' from /Users/donald/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/airtable-0.0.8/lib/airtable/table.rb:27:inrecords' from ./gem_test.rb:10:in `<

As you can see, I'm using rbenv. I've run this same code on Ruby 1.9.1, 2.2.4, and 2.3.0 and I can't get it to do anything but give me the error you see above. Am I missing something? Or this this a bug?

I downloaded the gem source to my machine and ran the tests, which ran just fine.

nesquena commented 8 years ago

There's an unstated dependency on http://apidock.com/rails/ActiveSupport/HashWithIndifferentAccess which is causing this issue. If you add in the activesupport library to your project or otherwise define HashWithIndifferentAccess, this error will go away

dmerand commented 8 years ago

Thanks, that fixes it indeed! For anybody else with this problem, the require line is:

require 'active_support/all'

yuuki1224 commented 8 years ago

I've got same trouble and wondering why don't this add require 'active_support/all' into here? Any reason?

syrnick commented 8 years ago

I'll address that. Is there any reason to not depend on active_support explicitly?

nesquena commented 8 years ago

active_support is a pretty expensive dependency to bring in to a project that doesn't already have it loaded. I wonder if we'd be better off finding an alternate way to make it so we can do @record[:foo] and @record['foo'] produce the same response. That said, easiest solution is to depend explicitly on active_support

syrnick commented 8 years ago

Handling @record[:foo] and @record['foo'] is pretty easy. I'd just store data keyed by the string k, a map from to_key(k) to k and lookup the key if the argument to @record[] is a symbol. I think it'll actually be better, because you'd only be exposed to naming collisions created by to_key when you use an ambiguous symbol.

@record.fields[:foo] and @record.fields['foo'] is a bit more complicated as we'd need an equivalent replacement for HashWithIndifferentAccess. I'm leaning towards checking if it exists and returning one if it does. If you don't have active_support, it'd just work until you try to use @record.fields with a symbol.