Class: PencilPusher::Form

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Form

Returns a new instance of Form.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pencil_pusher/form.rb', line 16

def initialize(data)
  super(data)
  if data.nil?
    @validated = true
    @bound = false
    @data = {}
  else
    @validated = false
    @bound = true
    @data = data
  end
  errors.clear
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



10
11
12
# File 'lib/pencil_pusher/form.rb', line 10

def data
  @data
end

Class Method Details

.attribute_namesObject



12
13
14
# File 'lib/pencil_pusher/form.rb', line 12

def self.attribute_names
  attribute_set.to_a.map(&:name)
end

Instance Method Details

#bound?Boolean

Returns:

  • (Boolean)


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

def bound?
  @bound
end

#cleaned_dataObject



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

def cleaned_data
  attributes.reject { |key, value| value.nil? }
end

#error_messagesObject



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

def error_messages
  @_error_messages ||= errors.messages
end

#recognized_dataObject



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

def recognized_data
  data.slice(*attributes.keys)
end

#valid?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
# File 'lib/pencil_pusher/form.rb', line 32

def valid?
  @_valid ||= (
    if bound?
      super
    else
      true
    end
  )
end