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, args = [], options = {}, block = nil, slot_name: nil, partial_path: nil) ⇒ Value



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

def initialize(view_context, args = [], options = {}, block = nil, slot_name: 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) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/slotify/value.rb', line 62

def method_missing(name, ...)
  if name.start_with?("to_")
    @args.first.public_send(name, ...)
  else
    super
  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

#[](key) ⇒ Object



70
71
72
# File 'lib/slotify/value.rb', line 70

def [](key)
  key.is_a?(Integer) ? @args[key] : super
end

#contentObject



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

def content
  if @block && @block.arity == 0
    body = @view_context.capture(&@block)
    ActiveSupport::SafeBuffer.new(body.presence || "")
  elsif args.first.is_a?(String)
    ActiveSupport::SafeBuffer.new(args.first)
  else
    args.first
  end
end

#empty?Boolean Also known as: blank?



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

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



33
34
35
# File 'lib/slotify/value.rb', line 33

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

#render_in(view_context, &block) ⇒ Object



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

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



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

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

#to_hObject Also known as: to_hash



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

def to_h
  @options
end

#with_default_options(default_options) ⇒ Object



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

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

#with_partial_path(partial_path) ⇒ Object



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

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