Sha256: 00a75ec22c9dece08014f44d41f277076e0cc02a6d4770f321e3c0b3aefef7cd

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

module Slippery
  module ProcessorHelpers
    def self.included(klz)
      klz.extend ClassMethods
    end

    def asset_uri(path)
      "file://" + Slippery::ROOT.join('assets', path).to_s
    end
    module_function :asset_uri

    def include_local_javascript(element, path)
      element.add javascript_include_tag(asset_uri(path))
    end

    def include_local_css(element, path)
      element.add stylesheet_link_tag(asset_uri(path))
    end

    def javascript_include_tag(path)
      H[:script, {src: path, type: 'text/javascript'}]
    end

    def stylesheet_link_tag(path)
      H[:link, {href: path, rel: 'stylesheet'}]
    end

    def data_attributes(attrs)
      Hash[*attrs.flat_map { |k, v| ["data-#{k}", v] }]
    end

    def hash_to_js(hsh)
      hsh.map { |k, v| "#{k}:#{v.inspect}" }.join(',') #:(
    end

    def call(doc)
      doc.process(*self.class.processors.map {|name| send(name) })
    end

    module ClassMethods
      def processors
        @processors ||= []
      end

      def processor(name, selector = nil, &blk)
        if selector
          define_method name do
            ->(node) { node.replace(selector) { |node| instance_exec(node, &blk) } }
          end
        else
          define_method name { ->(node) { blk.call(node) } }
        end
        processors << name
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
slippery-0.4.2 lib/slippery/processor_helpers.rb
slippery-0.4.1 lib/slippery/processor_helpers.rb
slippery-0.4.0 lib/slippery/processor_helpers.rb