How do I load extensions to the Sass::Script::Functions module?

情到浓时终转凉″ 提交于 2019-12-01 05:34:33

问题


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".

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.

UPDATE:

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?

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

LAST UPDATE:

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 cannot be invoked from the console... Not sure why, but I was wrong in assuming that if I rig things up correctly, I'll be able to test it that way.
  • Since I was going back and forth trying a bunch of things, I probably never had the right combination of having the hex function inside the config/compass.rb file, invoking it from the .css file, AND restarting the rails server.

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


回答1:


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

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 (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.




回答2:


I usually make a folder in lib named "sass" and in that folder create a sass-hex.rb (make sure this folder is on the load path)

module Sass::Script::Functions
  module SassHex
    def hex(decimal)
      Sass::Script::String.new("%02x" % decimal)
    end
  end
  include SassHex
end

All you should have todo is require the sass-hex.rb file I use this trick a lot in compass when extending sass.



来源:https://stackoverflow.com/questions/8690372/how-do-i-load-extensions-to-the-sassscriptfunctions-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!