Skip to content

Instantly share code, notes, and snippets.

@stopthink
Created April 19, 2010 20:04
Show Gist options
  • Select an option

  • Save stopthink/371522 to your computer and use it in GitHub Desktop.

Select an option

Save stopthink/371522 to your computer and use it in GitHub Desktop.
# Load format-specific drivers in the following order:
$SourceParserImplementations = []
SourceParser::Upcoming
SourceParser::Ical
SourceParser::Hcal
@stopthink

Copy link
Copy Markdown
Author

Throws the following error at line 3:

NameError in SourcesController#import

uninitialized constant SourceParser::Upcoming

@radar

radar commented Apr 19, 2010

Copy link
Copy Markdown

Line 1: A comment
Line 2: Setting a global variable called "$SourceParserImplementations" to a blank Array.
Line 3: Attempting to access the constant "Upcoming" under the namespace of "SourceParser". SourceParser here could either be a module or a class.
Line 4 & 5: Ditto.

Diagnosis: Whatever is defining your SourceParser module/class is not being required.

@nickstamas

Copy link
Copy Markdown

I agree with radar, you're attempting to reference something (impossible to tell from the snippet) with the SourceParser namespace that is not defined anywhere.

@stopthink

Copy link
Copy Markdown
Author

This snippet is at the end of source_parser.rb inside of app/models. There is a subfolder at app/models/source_parser/ with the class files, ical.rb, upcoming.rb, etc. These files are subclasses of source_parser. Is there a typical way of requiring these classes? Perhaps Rails/Ruby does it automagically? I can't find any trace of including them manually. Running 2.3.5, if it helps.

@nickstamas

Copy link
Copy Markdown

Does SourceParser inherit from ActiveRecord::Base? I like to keep non-ActiveRecord objects out of app/models and put them in the lib folder. Rails will include all class files in this directory by default.

Assuming SourceParser does not inherit from ActiveRecord::Base, I would put all your class files in lib and llose the folder hierarchy. So you'd have lib/source_parser.rb, lib/upcoming.rb, etc. If Upcoming, Ical and Hcal are subclasses of SourceParser, their class signature needs to look like "class Upcoming < SourceParser".

Does that help?

@radar

radar commented Apr 19, 2010

Copy link
Copy Markdown

Nstamas you're incorrect about the signature of the class.

It needs to be defined as either:

class SourceParser::Upcoming

or

class SourceParser
  class Upcoming
  ...
  end
end

Or

module SourceParser
  class Upcoming
  ...
  end
end

@nickstamas

Copy link
Copy Markdown

Is SourceParser a class or a module? If it's a class and he's inheriting from SourceParser, he would use the "<" sig, if it's a module that the Upcoming, Ical, Hcal classes are included in, he'd use the "::" sig. Right?

@stopthink

Copy link
Copy Markdown
Author

for reference, I'm pulling code from http://github.com/calagator/calagator

I have cloned a copy of the Calagator project and am able to run it successfully on my machine. I've attempted to copy over just the components required for importing events, but clearly I've missed something, because it does not work in my project.

It appears that source_parser, and its siblings ical, upcoming, etc are classes.

source_parser.rb: http://github.com/calagator/calagator/blob/master/app/models/source_parser.rb

upcoming.rb: http://github.com/calagator/calagator/blob/master/app/models/source_parser/upcoming.rb

@nickstamas

Copy link
Copy Markdown

Try just moving source_parser.rb and the source_parser folder into the lib folder. The classes should then be included automatically by Rails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment