How to have config.rb require other files?

732 views
Skip to first unread message

axyl

unread,
May 10, 2013, 7:37:15 PM5/10/13
to compas...@googlegroups.com
We're planning on splitting our Sass code base into multiple projects, so I'd like to create some shared logic that the config files can reference. However, I tried just putting some shared code into a different file, config_base.rb, at the same level as config.rb, and got this error:

C:\Users\aliu\workspace\>compass compile --trace
NameError on line ["5"] of C: uninitialized constant Compass::Configuration::Ser
ialization::CssSplitter
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/configuration/serialization.rb:24:in `eval'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/configuration/serialization.rb:24:in `parse_string'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/configuration/serialization.rb:15:in `block in _parse'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/configuration/serialization.rb:14:in `open'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/configuration/serialization.rb:14:in `_parse'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/configuration/file_data.rb:7:in `block in new_from_file'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/configuration/inheritance.rb:204:in `with_defaults'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/configuration/file_data.rb:6:in `new_from_file'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/configuration/helpers.rb:42:in `configuration_for'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/configuration/helpers.rb:97:in `add_project_configuration'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/commands/project_base.rb:31:in `add_project_configuration'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/commands/project_base.rb:25:in `configure!'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/commands/project_base.rb:15:in `initialize'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/commands/update_project.rb:37:in `initialize'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/exec/sub_command_ui.rb:42:in `new'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/exec/sub_command_ui.rb:42:in `perform!'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/lib/compass/exec/sub_command_ui.rb:15:in `run!'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/bin/compass:30:in `block in <top (required)>'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/bin/compass:44:in `call'
  C:/Dev/Ruby193/lib/ruby/gems/1.9.1/gems/compass-0.12.2/bin/compass:44:in `<top (required)>'
  C:/Dev/Ruby193/bin/compass:19:in `load'
  C:/Dev/Ruby193/bin/compass:19:in `<main>'

I'm fairly new at ruby, so not sure how to resolve this error. Perhaps it's not possible for the config.rb to reference other files? Attached my files for your reference. If anyone could give me some suggestions and pointers, I'd really appreciate them. 
config.rb
config_base.rb

Bill Burton

unread,
May 11, 2013, 1:01:47 AM5/11/13
to compas...@googlegroups.com
Hello,

At the top of your config.rb put the following two lines:

# Add the directory this script resides in to the front of the LOAD_PATH
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
require 'config_base'

Then delete the load 'config_base.rb' line.

Normally, you would run compass in the same folder containing the config.rb but if you didn't and specified the path to the config.rb, it would still be able to load config_base.rb.

Hope this helps!
-Bill

--
You received this message because you are subscribed to the Google Groups "Compass" group.
To unsubscribe from this group and stop receiving emails from it, send an email to compass-user...@googlegroups.com.
To post to this group, send email to compas...@googlegroups.com.
Visit this group at http://groups.google.com/group/compass-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Bill Burton <bbu...@mail.com>

axyl

unread,
May 13, 2013, 5:09:11 PM5/13/13
to compas...@googlegroups.com, bbu...@mail.com
Hi Bill, 

Thanks so much, that worked! 

Chris Eppstein

unread,
May 16, 2013, 12:29:48 PM5/16/13
to compas...@googlegroups.com, bbu...@mail.com
I would have done it like this:

require File.join(File.dirname(__FILE__), 'config_base.rb')
extend CssSplitter

No need to add the project location to the global path in this case.

Also, include will not work because the context this code is evaluated in is an instance, not a class. So you need to use extend instead.

Chris
Reply all
Reply to author
Forward
0 new messages