创建自定义链轮处理器的文档? [英] Documentation for creating custom Sprockets processors?

查看:41
本文介绍了创建自定义链轮处理器的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Rails 创建一个链轮预处理器,它可以在资产管道中找到 .png.rb 文件,并使用它们生成我的应用程序中各个页面的 png 屏幕截图.

I'm trying to create a sprockets preprocessor for Rails that finds .png.rb files in the asset pipeline and uses them to generate png screenshots of various pages in my application.

我已经阅读了很多关于这个主题的内容,但我似乎找不到任何关于如何进行设置的简单文档.请帮忙?

I've read up on this topic quite a bit but I can't seem to find any straightforward documentation on how to get this set up. Help, please?

这是我目前所拥有的:

/initializers/sprockets.rb:

require 'screenshot_preprocessor'

Rails.application.assets.register_mime_type('screenshot/png', '.png.rb')
Rails.application.assets.register_preprocessor('screenshot/png', ScreenshotPreprocessor)

<小时>

/lib/screenshot_preprocessor.rb:

class ScreenshotPreprocessor
  # What API do I need to provide here?
  #   - What methods do I need to provide?
  #   - What parameters does Sprockets pass me?
  #   - What do I need to return to Sprockets?
end

推荐答案

好吧,我仍然不确定在哪里可以找到有关此的文档.但是,通过阅读 Sprockets 的源代码、使用 pry 调试器并阅读使用 Sprockets 做过类似事情的人的博客文章,我能够想出这个:

Okay, I'm still not sure where to find documentation on this. But, by reading Sprockets' source code, playing around with the pry debugger, and reading blog posts from people who have done similar things with Sprockets, I was able to come up with this:

/initializers/sprockets.rb:

require 'screenshot_generator'

Rails.application.assets.register_engine('.screenshot', ScreenshotGenerator)

<小时>

/lib/screenshot_generator.rb:

require_relative 'capybara_screenshot' # Don't worry about this, it's not
                                       # relevant to this question.

class ScreenshotGenerator < Sprockets::Processor
  def evaluate(context, locals)
    generator_class = ScreenshotGenerator.get_generator_class(context.pathname)

    return generator_class.new.generate
  end

  private

  def self.get_generator_class(generator_file)
    # This evaluates the Ruby code in the given file and returns a class that
    # can generate a binary string containing an image file.
    # (Code omitted for brevity)
  end
end

<小时>

这现在对我来说很好用,但我真的更喜欢看一些关于链轮预处理器、后处理器和引擎如何工作的真实文档.如果有人发现任何此类文档,请发布答案.


This works fine for me now, but I'd really prefer to see some real documentation on how Sprockets preprocessors, postprocessors, and engines work. If anyone finds any such documentation, please post an answer.

这篇关于创建自定义链轮处理器的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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