Class: Slotify::Value

Inherits:
Object
  • Object
show all
Includes:
InflectionHelper
Defined in:
lib/slotify/value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InflectionHelper

#plural?, #singular?, #singularize

Constructor Details

#initialize(view_context, slot_name, args = [], options = {}, block = nil, partial_path: nil) ⇒ Value

Returns a new instance of Value.



9
10
11
12
13
14
15
16
# File 'lib/slotify/value.rb', line 9

def initialize(view_context, slot_name, args = [], options = {}, block = nil, partial_path: nil)
  @view_context = view_context
  @slot_name = slot_name.to_sym
  @args = args
  @options = options.to_h
  @block = block
  @partial_path = partial_path
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **options) ⇒ Object



67
68
69
70
71
# File 'lib/slotify/value.rb', line 67

def method_missing(name, *args, **options)
  if name.start_with?("to_") && args.none?
    @args.first.public_send(name)
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



5
6
7
# File 'lib/slotify/value.rb', line 5

def args
  @args
end

#blockObject (readonly)

Returns the value of attribute block.



5
6
7
# File 'lib/slotify/value.rb', line 5

def block
  @block
end

#slot_nameObject (readonly)

Returns the value of attribute slot_name.



5
6
7
# File 'lib/slotify/value.rb', line 5

def slot_name
  @slot_name
end

Instance Method Details

#contentObject Also known as: to_s, to_str



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/slotify/value.rb', line 22

def content
  body = if @block && @block.arity == 0
    @view_context.capture(&@block)
  else
    begin
      args.first.to_str
    rescue NoMethodError
      ""
    end
  end
  ActiveSupport::SafeBuffer.new(body.presence || "")
end

#empty?Boolean Also known as: blank?

Returns:

  • (Boolean)


42
43
44
# File 'lib/slotify/value.rb', line 42

def empty?
  @args.empty? || @options.empty? || !@block
end

#optionsObject



18
19
20
# File 'lib/slotify/value.rb', line 18

def options
  ValueOptions.new(@view_context, @options)
end

#present?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/slotify/value.rb', line 38

def present?
  @args.present? || @options.present? || @block
end

#render_in(view_context, &block) ⇒ Object



73
74
75
# File 'lib/slotify/value.rb', line 73

def render_in(view_context, &block)
  view_context.render partial_path, **@options.to_h, &@block || block
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/slotify/value.rb', line 63

def respond_to_missing?(name, include_private = false)
  name.start_with?("to_") || super
end

#to_hObject Also known as: to_hash



48
49
50
# File 'lib/slotify/value.rb', line 48

def to_h
  @options
end

#with_default_options(default_options) ⇒ Object



58
59
60
61
# File 'lib/slotify/value.rb', line 58

def with_default_options(default_options)
  options = TagOptionsMerger.call(default_options, @options)
  Value.new(@view_context, @slot_name, @args, options, @block)
end

#with_partial_path(partial_path) ⇒ Object



54
55
56
# File 'lib/slotify/value.rb', line 54

def with_partial_path(partial_path)
  Value.new(@view_context, @slot_name, @args, options, @block, partial_path:)
end