Class: Cave::Form

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, Virtus
Defined in:
lib/cave/form.rb

Direct Known Subclasses

ModelForm

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Form

Returns a new instance of Form.



36
37
38
39
# File 'lib/cave/form.rb', line 36

def initialize attrs={}
  super
  bind attrs unless attrs.empty?
end

Class Method Details

.bind(*args) ⇒ Object



26
27
28
29
30
# File 'lib/cave/form.rb', line 26

def bind *args
  f = new *args
  f.instance_eval { @bound = true }
  f
end

.field(name, type, opts = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/cave/form.rb', line 15

def field name, type, opts={}
  @_fields ||= {}
  @_fields[name] = type

  attribute name, type

  opts.each do |k,v|
    validates name, {k => v}
  end
end

.fieldsObject



11
12
13
# File 'lib/cave/form.rb', line 11

def fields
  @_fields ||= {}
end

Instance Method Details

#bind(attrs = {}) ⇒ Object



41
42
43
44
# File 'lib/cave/form.rb', line 41

def bind attrs={}
  self.attributes = (attributes || {}).merge attrs
  @bound = true
end

#bound?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/cave/form.rb', line 46

def bound?
  @bound
end

#persist!Object



59
60
61
# File 'lib/cave/form.rb', line 59

def persist!
  raise "#{self.class} does not define a persist method"
end

#save!Object



54
55
56
57
# File 'lib/cave/form.rb', line 54

def save!
  raise Cave::ValidationError.new errors.full_messages.join(',') unless valid?
  persist!
end

#unbound?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/cave/form.rb', line 50

def unbound?
  !@bound
end