I am wondering if a ruby hash can be shared between Sass and CoffeeScript with a minimum of fuss.
I've looked around for an answer to this but found nothing conclusive. Sources, such as the documentation for Sass, talk about how to link files of the same type and how to manipulate data structures within the language, but don't touch on whether data can be imported from elsewhere, or whether ruby code can be interpreted in someway - the only similar thing I can think of is compass using a .rb file for it's config.
My instinct suggests this is (or should) be possible, both languages are Ruby-like and capable of interpreting hashes.
As this is a practical problem I've faced several times (DRYing up pre-processed front end code, but also providing the same values for back-end processing, such as generating an SVG in an HTML template) but never solved in a really clean way I would accept a solution that involved using Rails.
Please note, I am quite specific about this relating to the pre-compilation stage of front end asset production, namely Sass and CoffeeScript. Answers which involve CSS, JavaScript or require a browser are not what I'm looking for.
Extra bits
I've decided to add an example, here are three definitions of the same data as key value pairs:
colours = { brandBackground: '#f00', brandForeground: '#00f', brandText: '#0f0' }
colours[:brandBackground]
$colours: ( brandBackground: '#f00', brandForeground: '#00f', brandText: '#0f0' )
map-get($colours, brandBackground)
colours = { brandBackground: '#f00', brandForeground: '#00f', brandText: '#0f0' }
colours.brandBackground
Coffeescript and Ruby hash definitions are identical, Sass is very close ... if this can work then colours (or any other variable) could be defined in one place then used throughout the front and back end by any code.
You can turn your CoffeeScript and SASS into ERB templates which will be preprocessed (and thus you can use all awesome Ruby's possibilities in them). There is a little post that describes this: https://robots.thoughtbot.com/dont-repeat-your-ruby-constants-in-javascript.
Update
Add this code into
lib/shared_assets_data.rb
:Then to enable autoloading
lib
directory add this line toconfig/application.rb
:After that you can do following: