Sha256: 7f43e72060a494b247a32924b5311965481e6023f7c0157a40408b8ed2e8252e

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

require_relative 'resource'

module Medivo

  class PrimaryInsurance
    include ActiveModel::Validations

    validates_presence_of :relationship, :company_name, :policy_number, :group_number
    validates :relationship, :inclusion => { :in => 1..2 }

    def initialize(attributes = {})
      @attributes = attributes
    end

    def read_attribute_for_validation(key)
      @attributes[key]
    end

    def to_hash
      @attributes
    end

    def validate(record)
      primary_insurance = record.attributes[:primary_insurance]
      record.errors.add(:primary_insurance, "primary_insurance field must be present") and return unless primary_insurance
      record.errors.add(:primary_insurance, "primary_insurance field must be of PrimaryInsurance class") unless primary_insurance.is_a? PrimaryInsurance
      primary_insurance.valid?
      primary_insurance.errors.messages.each do |field,value|
        record.errors.add(:"primary_insurance.#{field}", value.first)
      end
    end
  end

  class InsuranceOrder < Order
    set_element_name 'customer'
    set_collection_name 'customers'

    validates_with Medivo::ArrayValidator, :fields => [:diagnosis_code]
    validates_with PrimaryInsurance

    def to_xml(options={})
      options.merge!(:root=>self.class.element_name)
      @attributes.merge(:primary_insurance=>primary_insurance.to_hash).to_xml(options)
    end

    # when getting the insurance info from medivo labs
    def insurance
      @insurance ||= try(:insurance_information).try(:primary_insurance_info)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
medivo-0.0.11 app/models/medivo/insurance_order.rb
medivo-0.0.9 app/models/medivo/insurance_order.rb