Class: FormKeeper::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/formkeeper.rb

Instance Method Summary collapse

Constructor Details

#initialize(messages = nil) ⇒ Report

Returns a new instance of Report.



435
436
437
438
439
# File 'lib/formkeeper.rb', line 435

def initialize(messages=nil)
  @failed_records = {}
  @messages = messages || Messages.new({})
  @valid_params = {}
end

Instance Method Details

#<<(record) ⇒ Object



440
441
442
443
444
445
446
# File 'lib/formkeeper.rb', line 440

def <<(record)
  if record.failed?
    @failed_records[record.name.to_sym] = record
  else
    @valid_params[record.name.to_sym] = record.value
  end
end

#[](name) ⇒ Object



447
448
449
# File 'lib/formkeeper.rb', line 447

def [](name)
  @valid_params[name.to_sym]
end

#failed?Boolean

Returns:

  • (Boolean)


456
457
458
# File 'lib/formkeeper.rb', line 456

def failed?
  !@failed_records.empty?
end

#failed_constraints(name) ⇒ Object



468
469
470
471
472
# File 'lib/formkeeper.rb', line 468

def failed_constraints(name)
  return [] unless @failed_records.has_key?(name.to_sym)
  record = @failed_records[name.to_sym]
  record.failed_constraints
end

#failed_fieldsObject



465
466
467
# File 'lib/formkeeper.rb', line 465

def failed_fields
  @failed_records.keys 
end

#failed_on?(name, constraint = nil) ⇒ Boolean

Returns:

  • (Boolean)


459
460
461
462
463
464
# File 'lib/formkeeper.rb', line 459

def failed_on?(name, constraint=nil)
  return false unless @failed_records.has_key?(name.to_sym)
  return true if constraint.nil?
  record = @failed_records[name.to_sym]
  record.failed_by?(constraint)
end

#message(action, name, constraint) ⇒ Object



491
492
493
494
495
496
497
# File 'lib/formkeeper.rb', line 491

def message(action, name, constraint)
  if @failed_records.has_key?(name.to_sym) and @failed_records[name.to_sym].failed_by?(constraint)
    @messages.get(action, name, constraint)
  else
    nil
  end
end

#messages(action, name = nil) ⇒ Object



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/formkeeper.rb', line 473

def messages(action, name=nil)
  messages = []
  if name.nil?
    @failed_records.values.each do |record|
      record.failed_constraints.each do |constraint|
        messages << @messages.get(action, record.name, constraint)
      end
    end
  else
    if @failed_records.has_key?(name.to_sym)
      record = @failed_records[name.to_sym]
      record.failed_constraints.each do |constraint|
        messages << @messages.get(action, record.name, constraint)
      end
    end
  end
  messages.select{ |m| !m.nil? and !m.empty? }.uniq
end

#valid?(name) ⇒ Boolean

Returns:

  • (Boolean)


453
454
455
# File 'lib/formkeeper.rb', line 453

def valid?(name)
  @valid_params.has_key?(name.to_sym)
end

#valid_fieldsObject



450
451
452
# File 'lib/formkeeper.rb', line 450

def valid_fields
  @valid_params.keys
end