Sha256: 8f9266179a090e8838255f3657dfbe5941ee44cf71e4a10ec3e555d5e282d7de

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

module Inverter
  class Tags
    def self.update_html(html)
      tags = parse_tags(html)

      tags.each do |tag, partials|
        tag_name = partials[0]
        params   = partials[1]

        if helper_exists?(tag_name)
          helper_result = call_helper(tag_name, params)
          html.gsub!(tag, helper_result)
        end
      end

      return html
    end

    private

      def self.parse_tags(text)
        regex  = /\[[a-zA-Z0-9\-]{2,}#[a-zA-Z0-9\-,]{2,}\]/
        result = {}

        text.scan(regex).each do |tag|
          result[tag] = tag.gsub(/(\[|\])/, '').split('#')
        end

        return result
      end


      def self.call_helper(tag_name, params)
        name = helper_name(tag_name)
        ApplicationController.helpers.send(name, params)
      end


      def self.helper_exists?(tag_name)
        name = helper_name(tag_name)
        ApplicationController.helpers.respond_to? name
      end


      def self.helper_name(tag_name)
        name = tag_name.downcase.gsub('-', '_')
        return "inverter_#{ name }"
      end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
inverter-0.5.1 lib/inverter/tags.rb
inverter-0.5.0 lib/inverter/tags.rb
inverter-0.4.7 lib/inverter/tags.rb
inverter-0.4.6 lib/inverter/tags.rb
inverter-0.4.5 lib/inverter/tags.rb