Sha256: b067c6c6f8de0146874fb856d8b89800ece55d4550443a9bbbaa98964e97674c

Contents?: true

Size: 1.41 KB

Versions: 5

Compression:

Stored size: 1.41 KB

Contents

module Zuul
  class Context < Struct.new(:class_name, :id)

    def self.parse(*args)
      if args.length >= 2
        new(*args)
      elsif args[0].is_a?(self)
        return args[0]
      elsif args[0].is_a?(Class)
        new(args[0].name)
      elsif args[0].class.ancestors.include?(::ActiveRecord::Base) && args[0].respond_to?(:id)
        new(args[0].class.name, args[0].id)
      else
        new
      end
    end
    
    def instance?
      !class_name.nil? && !id.nil?
    end
    alias_method :object?, :instance?

    def class?
      !class_name.nil? && id.nil?
    end

    def nil?
      class_name.nil? && id.nil?
    end

    def type
      return :nil if class_name.nil?
      return :class if id.nil?
      :instance
    end

    def type_s
      return 'global' if class_name.nil?
      return class_name if id.nil?
      "#{class_name}(#{id})"
    end

    def to_context
      return nil if class_name.nil?
      return class_name.constantize if id.nil?
      class_name.constantize.find(id)
    end
    alias_method :context, :to_context
    
    protected

    def initialize(class_name=nil, id=nil)
      raise Exceptions::InvalidContext, "Invalid Context Class" unless class_name.nil? || class_name.is_a?(String)
      raise Exceptions::InvalidContext, "Invalid Context ID" unless id.nil? || id.is_a?(Integer)
      raise Exceptions::InvalidContext if !id.nil? && class_name.nil?
      super
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
zuul-0.2.7 lib/zuul/context.rb
zuul-0.2.6 lib/zuul/context.rb
zuul-0.2.5 lib/zuul/context.rb
zuul-0.2.4 lib/zuul/context.rb
zuul-0.2.3 lib/zuul/context.rb