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)


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

def failed?
  !@failed_records.empty?
end

#failed_constraints(name) ⇒ Object



473
474
475
476
477
# File 'lib/formkeeper.rb', line 473

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



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

def failed_fields
  @failed_records.keys 
end

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

Returns:

  • (Boolean)


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

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



496
497
498
499
500
501
502
# File 'lib/formkeeper.rb', line 496

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



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/formkeeper.rb', line 478

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)


458
459
460
# File 'lib/formkeeper.rb', line 458

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

#valid_fieldsObject



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

def valid_fields
  @valid_params.keys
end

#value(name) ⇒ Object



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

def value(name)
  name = name.to_sym
  return self[name] if valid?(name)
  failed_fields.include?(name) ? @failed_records[name].value : nil
end