Module: Reform::Form::Property

Included in:
Reform::Form
Defined in:
lib/reform/form.rb

Instance Method Summary collapse

Instance Method Details

#property(name, options = {}, &block) ⇒ Object

Add macro logic, e.g. for :populator.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/reform/form.rb', line 18

def property(name, options={}, &block)
  definition = super # let representable sort out inheriting of properties, and so on.
  definition.merge!(deserializer: {}) unless definition[:deserializer] # always keep :deserializer per property.

  deserializer_options = definition[:deserializer]

  # Populators
  internal_populator = Populator::Sync.new(nil)
  if block = definition[:populate_if_empty]
    internal_populator = Populator::IfEmpty.new(block)
  end
  if block = definition[:populator] # populator wins over populate_if_empty when :inherit
    internal_populator = Populator.new(block)
  end
  definition.merge!(internal_populator: internal_populator) unless options[:internal_populator]
  external_populator = Populator::External.new

  # always compute a parse_pipeline for each property of the deserializer and inject it via :parse_pipeline.
  # first, let representable compute the pipeline functions by invoking #parse_functions.
  if definition[:nested]
    parse_pipeline = ->(input, options) do
      functions = options[:binding].send(:parse_functions)
      pipeline  = Representable::Pipeline[*functions] # Pipeline[StopOnExcluded, AssignName, ReadFragment, StopOnNotFound, OverwriteOnNil, Collect[#<Representable::Function::CreateObject:0xa6148ec>, #<Representable::Function::Decorate:0xa6148b0>, Deserialize], Set]

      pipeline  = Representable::Pipeline::Insert.(pipeline, external_populator,            replace: Representable::CreateObject::Instance)
      pipeline  = Representable::Pipeline::Insert.(pipeline, Representable::Decorate,       delete: true)
      pipeline  = Representable::Pipeline::Insert.(pipeline, Deserialize,                   replace: Representable::Deserialize)
      pipeline  = Representable::Pipeline::Insert.(pipeline, Representable::SetValue,       delete: true) # FIXME: only diff to options without :populator
    end
  else
    parse_pipeline = ->(input, options) do
      functions = options[:binding].send(:parse_functions)
      pipeline  = Representable::Pipeline[*functions] # Pipeline[StopOnExcluded, AssignName, ReadFragment, StopOnNotFound, OverwriteOnNil, Collect[#<Representable::Function::CreateObject:0xa6148ec>, #<Representable::Function::Decorate:0xa6148b0>, Deserialize], Set]

      # FIXME: this won't work with property :name, inherit: true (where there is a populator set already).
      pipeline  = Representable::Pipeline::Insert.(pipeline, external_populator,            replace: Representable::SetValue) if definition[:populator] # FIXME: only diff to options without :populator
      pipeline
    end
  end

  deserializer_options[:parse_pipeline] ||= parse_pipeline

  if proc = definition[:skip_if]
    proc = Reform::Form::Validate::Skip::AllBlank.new if proc == :all_blank
    deserializer_options.merge!(skip_parse: proc) # TODO: same with skip_parse ==> External
  end


  # per default, everything should be writeable for the deserializer (we're only writing on the form). however, allow turning it off.
  deserializer_options.merge!(writeable: true) unless deserializer_options.has_key?(:writeable)

  definition
end