Class: Shaf::Formable::Builder::FormWrapper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, ImmutableAttr
Defined in:
lib/shaf/formable/builder.rb

Constant Summary collapse

WRITER_DELEGATES =
DELEGATES.map { |method| :"#{method}=" }

Constants included from ImmutableAttr

ImmutableAttr::NON_DUPABLE_CLASSES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ImmutableAttr

dup, immutable_accessor, immutable_reader, immutable_writer

Constructor Details

#initialize(form, method_name: nil, instance_accessor: nil) ⇒ FormWrapper

Returns a new instance of FormWrapper.



20
21
22
23
24
25
# File 'lib/shaf/formable/builder.rb', line 20

def initialize(form, method_name: nil, instance_accessor: nil)
  @form = form&.dup || Formable::Form.new
  @method_name = method_name
  @form.action = action_from(method_name) unless @form.action
  @instance_accessor = instance_accessor
end

Instance Attribute Details

#instance_accessorObject

Returns the value of attribute instance_accessor.



14
15
16
# File 'lib/shaf/formable/builder.rb', line 14

def instance_accessor
  @instance_accessor
end

Instance Method Details

#action_from(method_name) ⇒ Object



27
28
29
30
31
# File 'lib/shaf/formable/builder.rb', line 27

def action_from(method_name)
  return unless method_name

  (method_name.to_s.delete_suffix('_form')).to_sym
end

#instance_accessor?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/shaf/formable/builder.rb', line 45

def instance_accessor?
  [:empty, :prefill].include? instance_accessor
end

#method_nameObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/shaf/formable/builder.rb', line 33

def method_name
  name = @method_name || form.action

  if name.nil?
    nil
  elsif name.to_s.end_with? '_form'
    name.to_sym
  else
    :"#{name}_form"
  end
end

#prefill?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/shaf/formable/builder.rb', line 49

def prefill?
  instance_accessor == :prefill
end