Sha256: b7dbffe42ee592d4c0b3878e22f5f830caa0d32d2bfc1a3bf25646be898f499b

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

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

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

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

    def asset_uri(path)
      'file://' + File.expand_path('../../../assets/'+ path, __FILE__)
    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

    module ClassMethods
      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
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slippery-0.0.1 lib/slippery/processor_helpers.rb