Module: Serbea::Helpers

Included in:
Bridgetown::SerbeaView
Defined in:
lib/serbea/helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



5
6
7
# File 'lib/serbea/helpers.rb', line 5

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

Instance Method Details

#append(old_string, new_string) ⇒ Object



49
50
51
# File 'lib/serbea/helpers.rb', line 49

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

#assign_to(input, varname, preserve: false) ⇒ Object



53
54
55
56
# File 'lib/serbea/helpers.rb', line 53

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

#capture(*args) {|args| ... } ⇒ Object

Yields:

  • (args)


9
10
11
12
13
14
15
16
17
# File 'lib/serbea/helpers.rb', line 9

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

  result&.html_safe
end

#h(input) ⇒ Object Also known as: escape



35
36
37
# File 'lib/serbea/helpers.rb', line 35

def h(input)
  ERB::Util.h(input.to_s)
end

#helper(name, &helper_block) ⇒ Object Also known as: macro



23
24
25
26
27
28
29
30
31
32
# File 'lib/serbea/helpers.rb', line 23

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

    result.is_a?(String) ? result.html_safe : result
  end
end

#pipeline(context, value) ⇒ Object



19
20
21
# File 'lib/serbea/helpers.rb', line 19

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

#prepend(old_string, new_string) ⇒ Object



45
46
47
# File 'lib/serbea/helpers.rb', line 45

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

#safe(input) ⇒ Object Also known as: raw



40
41
42
# File 'lib/serbea/helpers.rb', line 40

def safe(input)
  input.to_s.html_safe
end