Module: Reform::Contract::PropertyMethods

Included in:
Reform::Contract
Defined in:
lib/reform/contract.rb

Instance Method Summary collapse

Instance Method Details

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



45
46
47
48
49
# File 'lib/reform/contract.rb', line 45

def collection(name, options={}, &block)
  options[:collection] = true

  property(name, options, &block)
end

#properties(names, *args) ⇒ Object



51
52
53
# File 'lib/reform/contract.rb', line 51

def properties(names, *args)
  names.each { |name| property(name, *args) }
end

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/reform/contract.rb', line 30

def property(name, options={}, &block)
  options[:private_name] = options.delete(:as)

  options[:coercion_type] = options.delete(:type)

  options[:features] ||= []
  options[:features] += features.keys if block_given?

  definition = representer_class.property(name, options, &block)
  setup_form_definition(definition) if block_given? or options[:form]

  create_accessor(name)
  definition
end

#setup_form_definition(definition) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/reform/contract.rb', line 55

def setup_form_definition(definition)
  options = {
    # TODO: make this a bit nicer. why do we need :form at all?
    :form         => (definition[:extend] and definition[:extend].evaluate(nil)) || definition[:form], # :form is always just a Form class name.
    :pass_options => true, # new style of passing args
    :prepare      => lambda { |form, args| form }, # always just return the form without decorating.
    :representable => true, # form: Class must be treated as a typed property.
  }

  definition.merge!(options)
end