Sha256: adf60dcf58cf87149e754822bfe77e621dea91471b1a54185a7a2cc147dd67f7

Contents?: true

Size: 1.32 KB

Versions: 8

Compression:

Stored size: 1.32 KB

Contents

require "active_support/core_ext/string/output_safety"

module Serbea
  module Helpers
    def self.included(mod)
      Serbea::Pipeline.deny_value_method %i(escape h prepend append assign_to)
    end

    def capture(*args)
      previous_buffer_state = @_erbout
      @_erbout = Serbea::OutputBuffer.new
      result = yield(*args)
      @_erbout = previous_buffer_state

      result&.html_safe
    end

    def pipeline(context, value)
      Pipeline.new(context, value)
    end

    def helper(name, &helper_block)
      self.class.define_method(name) do |*args, &block|
        previous_buffer_state = @_erbout
        @_erbout = Serbea::OutputBuffer.new
        result = helper_block.call(*args, &block)
        @_erbout = previous_buffer_state

        result.is_a?(String) ? result.html_safe : result
      end
    end
    alias_method :macro, :helper

    def h(input)
      ERB::Util.h(input.to_s)
    end
    alias_method :escape, :h

    def safe(input)
      input.to_s.html_safe
    end
    alias_method :raw, :safe

    def prepend(old_string, new_string)
      "#{new_string}#{old_string}"
    end

    def append(old_string, new_string)
      "#{old_string}#{new_string}"
    end

    def assign_to(input, varname, preserve: false)
      self.instance_variable_set("@#{varname}", input)
      preserve ? input : nil
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
serbea-0.11.2 lib/serbea/helpers.rb
serbea-0.11.1 lib/serbea/helpers.rb
serbea-0.11 lib/serbea/helpers.rb
serbea-0.10.5 lib/serbea/helpers.rb
serbea-0.10.4 lib/serbea/helpers.rb
serbea-0.10.3 lib/serbea/helpers.rb
serbea-0.10.2 lib/serbea/helpers.rb
serbea-0.10.1 lib/serbea/helpers.rb