如何将扩展加载到Sass :: Script :: Functions模块? [英] How do I load extensions to the Sass::Script::Functions module?

查看:105
本文介绍了如何将扩展加载到Sass :: Script :: Functions模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此建议,我正在尝试扩展Sass:Script :: Functions模块: https:/ /gist.github.com/481261/dd07a52829886ab1ad0875a8895f0100c4b925ab 。问题是,我该在哪里放置sass-hex.rb文件,我是否需要做任何事情来加载模块扩展?我尝试将文件放置在config /中,但似乎未加载。当我进入Rails控制台并键入Sass :: Script :: Functions :: hex时,我得到: NoMethodError:Sass :: Script :: Functions:Module的未定义方法'hex'。

I'm trying to extend the Sass:Script::Functions module, per this recommendation: https://gist.github.com/481261/dd07a52829886ab1ad0875a8895f0100c4b925ab. The question is, where do I place the sass-hex.rb file and do I have to do anything to "load" the module extension? I tried placing the file in config/, but it doesn't seem to be loaded. When I go to the rails console and type Sass::Script::Functions::hex, I get: "NoMethodError: undefined method `hex' for Sass::Script::Functions:Module".

我是Rails的新手,所以答案可能是非常明显和琐碎的。也许这就是为什么没有一个站点在讨论扩展Sass :: Script :: Function的原因,其中没有提及如何将扩展实际连接到代码中。这是官方文档,但在这方面也没有帮助: http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#adding_custom_functions

I'm new to Rails, so the answer may be something super obvious and trivial. Maybe that's why none of the sites that are talking about extending Sass::Script::Functions have any mention on how to actually hook up the extension into your code. This is the official documentation, but it's not helpful in this regard either: http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#adding_custom_functions.

更新:

因此,我将方法的定义从 def hex ...更改为 def self.hex ...,现在Sass :: Script :: Functions.hex可以使用了。我仍然觉得自己很想念东西,因为NOBODY建议自我的任何地方。是必需的...还是在上面的示例中我不正确地调用该方法?

So, I changed the definition of the method from "def hex ..." to "def self.hex ..." and now Sass::Script::Functions.hex works. I still feel I'm missing something, as NOBODY suggested anywhere that the "self." is needed... Or am I invoking the method incorrectly in the example above?

此外,问题是根本没有从十六进制方法中调用我使用它的CSS文件。

Also, the problem is that the "hex" method is not invoked at all from the CSS file where I use it.

最后更新:

解决方案是实际执行我最初所做的工作:将代码放置在config /目录中(在我的情况下,位于指南针.rb文件中)。

The solution was to actually do what I did initially: Place the code in the config/ directory (in my case, inside the compass.rb file).

有两个原因使我卡住,并认为没有为我工作:

There are two reasons I got stuck and thought it didn't work for me:


  • 无法从控制台调用Sass :: Script :: Functions :: hex ...不知道为什么,但是我以为如果我能正确组装东西,我会以这种方式进行测试,那是错误的。

  • 由于我来回尝试了很多东西,所以我可能从未有过正确的组合:在config / compass.rb文件中包含十六进制功能,从.css文件中调用它,然后重新启动Rails服务器。

真正浪费时间-我希望这可以帮助其他人避免浪费时间...

A real waste of time - I hope this helps others avoid it...

推荐答案

我刚刚解决了这个问题,可以使用 compact Compass的功能。这是整个消息:

I just solved this to be able to use the compact function from Compass. Here's the whole scoop:

lib / sass.rb(创建一个新文件)

lib/sass.rb (created a new file)

# Compact function pulled from compass
module Sass::Script::Functions

  module CustomSassExtensions
    def compact(*args)
      sep = :comma
      if args.size == 1 && args.first.is_a?(Sass::Script::List)
        args = args.first.value
        sep = args.first.separator
      end
      Sass::Script::List.new(args.reject{|a| !a.to_bool}, sep)
    end
  end

  include CustomSassExtensions

end

config / application.rb(将其放在类应用程序内在紧跟 config.autoload_paths )的行之后

config/application.rb (place this inside inside class Application right after the lines with config.autoload_paths)

if config.respond_to?(:sass)
  require "#{config.root}/lib/sass.rb"
end

让我知道它是否对您有用。

Let me know if it worked for you.

这篇关于如何将扩展加载到Sass :: Script :: Functions模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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