从数据库而不是文件系统导入SASS文件 [英] Import SASS file from database instead of filesystem

查看:86
本文介绍了从数据库而不是文件系统导入SASS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Ruby没有太多经验。我想从数据库而不是文件系统@import sass。我没有在线找到任何示例。我应该如何实施。我看到我必须扩展一个importer类,但是由于我不了解ruby,所以我需要知道该类在文件系统中的位置(只需检查一下即可),并且通常涉及哪些基本步骤。

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.

更多信息

MySQL数据库包含sass内容。
因此,在我的Web应用程序中,我接受用户的sass(作为字符串),其中可能包含导入语句,例如:

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 } 

现在在我的MySQL数据库中我将有这样的东西

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;}

我想执行此导入操作,请确保我可以执行正则表达式匹配从用户输入中获取文件名,然后通过该文件名查询数据库并获取内容。
但是我读到有一种很好的方法可以使ruby / sass使用DB作为装载路径而不是文件系统。

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.

更新

我创建了一个虚拟的自定义进口商类,其查找方法为

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

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

我如何将此导入器添加到不使用ruby的sass加载路径中,我可以吗更改sass gem文件夹中的源文件,然后将此导入器添加到lib / sass / importers吗?

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?

谢谢

推荐答案

由于使用的是Compass进行编译,因此可以在Compass配置文件中添加自定义Sass导入器。例如,使用 compass compile -c config.rb 进行编译,您将在 config.rb 中包含类似内容。文件:

Since you're using Compass to compile, you can add a custom Sass importers in the Compass config file. For example, compiling using compass compile -c config.rb, you would include something like this in your config.rb file:

require File.join(File.dirname(__FILE__), 'importer.rb')
Sass.load_paths << Sass::Importers::Custom.new()

然后在进口商中使用。 rb 在同一目录中,您将包括进口商定义:

Then in importer.rb in the same directory, you would include your importer definition:

module Sass
    module Importers
        class Custom < Base
            def find(name, options)
                if name == '[globals]'
                    options[:syntax] = :scss
                    options[:filename] = 'globals'
                    options[:importer] = self
                    return Sass::Engine.new("$imported-variable: blue;", options)
                else
                    return nil
                end
            end

            def find_relative(uri, base, options)
                nil
            end

            def key(uri, options)
                [self.class.name + ":" + uri, uri]
            end

            def mtime(uri, options)
                nil
            end

            def to_s
                '[custom]'
            end
        end
    end
end

然后在您的Sass文件中,您可以使用导入程序:

Then in your Sass file you can use the importer:

@import '[globals]';
p {
    color: $imported-variable;
}

当然,这只是一个虚拟实现,仅接受与 [全局变量] 。您需要提供自己的访问MySQL数据库的实现,因为我没有使用Ruby进行数据库访问的经验。希望除了@Sean提供的链接之外,它还可以使您更加接近。

Of course, this is just a dummy implementation that only accepts a URI matching "[globals]". You'll need to supply your own implementation that accesses your MySQL database, as I don't have any experience with database access in Ruby. Hopefully this should get you a little closer, though, in addition to the links that @Sean has provided.

这篇关于从数据库而不是文件系统导入SASS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆