Sha256: 507fbea47e0d0096e7ccd6b7303a6500deac9ab6d42d1d270d1b0e52d37e48ee

Contents?: true

Size: 1.65 KB

Versions: 9

Compression:

Stored size: 1.65 KB

Contents

module SharedBehaviors
  def self.included(model)
    model.send :include, HasData
    model.send :has_data, :transform
    
    model.send :include, InstanceMethods
    model.send :extend, ClassMethods
  end
  
  
  module InstanceMethods    
    def guid_generate
      key = "#{Time.now.to_i}::#{rand(999999999)}::#{attributes.values.join("::")}"
      Digest::MD5.hexdigest(key)
    end

    def apply_transform(table)
      return table if transform.blank?
      t = transform.constantize.new(table, transform_data)
      t.result
    end
    
    protected
    
    def settable_guid
      prepend = respond_to?(:guid_prepend) ? guid_prepend : ""
      append = respond_to?(:guid_append) ? guid_append : ""
      "#{prepend}#{guid_generate}#{append}"
    end
    
    def generate_guid_if_needed
      return true unless new_record?
      val = send(self.class.guid_field)
      return true unless val.blank?
      send("#{self.class.guid_field}=", settable_guid)
    end
  end
  
  module ClassMethods
    def generate_guid(field)
      @guid_field = field.to_sym
      validates_presence_of @guid_field
      before_validation :generate_guid_if_needed
    end
    def guid_field
      @guid_field
    end
    
    def validates_stripped_presence_of(field)
      validates_presence_of field
      method = "strip_before_validation_#{field}"
      define_method method do
        return true if send(field).nil?
        send("#{field}=", send(field).strip)
      end
      before_validation method
    end
    
    def validates_unique_presence_of(field)
      validates_stripped_presence_of field
      validates_uniqueness_of field, :case_sensitive => false
    end
  end
  
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
daily-0.0.10 lib/daily/shared_behaviors.rb
daily-0.0.9 lib/daily/shared_behaviors.rb
daily-0.0.8 lib/daily/shared_behaviors.rb
daily-0.0.7 lib/daily/shared_behaviors.rb
daily-0.0.6 lib/daily/shared_behaviors.rb
daily-0.0.5 lib/daily/shared_behaviors.rb
daily-0.0.4 lib/daily/shared_behaviors.rb
daily-0.0.3 lib/daily/shared_behaviors.rb
daily-0.0.2 lib/daily/shared_behaviors.rb