Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

ruby - Import SASS file from database instead of filesystem

I don't have much experience with Ruby. I want to @import sass from database instead of filesystem. I did not find any examples online. How should I go about implementing this. I see that I have to extend an importer class but since I have no understanding of ruby I need to know where in my filesystem does this class reside (Just to check it out) and in general what are the basic steps involved.

More information

MySQL database contains sass content. So in my web application I accept sass (as string) from user which may contain an import statement for example :

@import test.scss
body  { color:red } 

Now in my MySQL DB I will have something like this

Table  sass_files
column_name             filename  |   content
example row             test.scss |   p {color:blue;}

I want to make this import work, sure I can just do a regular expression match to get the filename from the user input and then query the DB by that file name and get the content. But I read that there is a nice way to make ruby/sass use DB as a load-path instead of filesystem.

UPDATE

SO i created a dummy custom importer class with find method as

def find(name, options)
  Sass::Engine.new("p { color :blue; }", options)
end

How do i add this importer to sass load paths w/o using ruby, like can i make change to the source files in the sass gem folder and add this importer to lib/sass/importers?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Out of the box, sass will only import local files (anything else is compiled into a CSS @import statement) but as the sass docs explain you can write your own custom importers by extending the Sass::Importers::Base class.

This answer to another question gives an example of a custom HTTP importer. You could approach this in (at least) two ways: write a custom importer for your database or serve the sass file over HTTP from the database (via a simple PHP page or something)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...