Sha256: ccd283ae285e24e888f2208c0a296bef88a3e0928aeadd3b858b1863c2ec4179

Contents?: true

Size: 843 Bytes

Versions: 1

Compression:

Stored size: 843 Bytes

Contents

module Easy
  module ReferenceData
    def self.refresh(clazz, unique_attribute_symbol, unique_attribute_value, attributes)
      record = clazz.where(unique_attribute_symbol => unique_attribute_value).first

      if record.nil?
        record = clazz.new(unique_attribute_symbol => unique_attribute_value)
      end

      attributes.each_pair do |key, value|
        record.send "#{key}=", value
      end

      if record.new_record?
        puts "  creating #{clazz}(#{unique_attribute_value})"
      elsif record.changed?
        puts "  updating #{clazz}(#{unique_attribute_value})"
      end

      begin
        record.save!
      rescue
        puts "Save failed for #{record.class}[#{unique_attribute_symbol}: #{unique_attribute_value}] with attributes #{attributes.inspect}"
        raise
      end

      record
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
easy_reference_data-0.1.1 lib/easy/reference_data/refresh.rb