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.



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

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

Instance Method Details

#<<(record) ⇒ Object



447
448
449
450
451
452
453
# File 'lib/formkeeper.rb', line 447

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



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

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

#failed?Boolean

Returns:

  • (Boolean)


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

def failed?
  !@failed_records.empty?
end

#failed_constraints(name) ⇒ Object



480
481
482
483
484
# File 'lib/formkeeper.rb', line 480

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



477
478
479
# File 'lib/formkeeper.rb', line 477

def failed_fields
  @failed_records.keys 
end

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

Returns:

  • (Boolean)


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

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



503
504
505
506
507
508
509
# File 'lib/formkeeper.rb', line 503

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



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/formkeeper.rb', line 485

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)


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

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

#valid_fieldsObject



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

def valid_fields
  @valid_params.keys
end

#value(name) ⇒ Object



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

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