Sha256: 9700a128b7ec49237290576ff5754c8d6309b6914157ee9ad0e0a258db02aa16

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

module Saasable::SaasDocument
  @saas_document = nil
  
  def self.included klass
    if @saas_document and @saas_document.name != klass.name
      raise Saasable::Errors::MultipleSaasDocuments, "you can only have one Saasable::SaasDocument"
    else
      @saas_document = klass
    end
    
    klass.extend ClassMethods
    klass.send(:include, InstanceMethods)
    klass.class_eval do
      field :hosts, :type => Array
    end
  end
  
  def self.saas_document
    @saas_document
  end
  
  module InstanceMethods
    def activate!
      Saasable::ScopedDocument.scoped_documents.each do |klass|
        # Create a default scope without messing with the ones already in place.
        klass.default_scoping ||= {}
        klass.default_scoping[:where] ||= {:saas_id => nil}
        
        klass.default_scoping[:where][:saas_id] = self._id
        klass.class_eval "field :saas_id, :type => BSON::ObjectId, :default => BSON::ObjectId(\"#{self._id}\")"
      end
    end
  end
  
  module ClassMethods
    def find_by_host! a_host
      if Saasable::SaasDocument.saas_document.nil?
        if Rails.env.production?
          raise Saasable::Errors::NoSaasDocuments, "you need to set one Saasable::SaasDocument"
        else
          return nil
        end
      end
      
      possible_saas = Saasable::SaasDocument.saas_document.where(:hosts => a_host)
      if possible_saas.empty?
        raise Saasable::Errors::SaasNotFound, "no #{Saasable::SaasDocument.saas_document.name} found for the host: \"#{a_host}\""
      elsif possible_saas.count > 1
        raise Saasable::Errors::MultipleSaasFound, "more then 1 #{Saasable::SaasDocument.saas_document.name} found for the host: \"#{a_host}\""
      else
        return possible_saas.first
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
saasable-0.1.6 lib/saasable/saas_document.rb
saasable-0.1.5 lib/saasable/saas_document.rb
saasable-0.1.4 lib/saasable/saas_document.rb