module ActiveNull

Constants

VERSION

Public Class Methods

extended(klass) click to toggle source
# File lib/active_null.rb, line 61
  def self.extended(klass)
    klass.class_eval <<-CODE
      after_initialize do
        self.class.null_associations
      end
    CODE
  end

Public Instance Methods

find_by(*args, &block) click to toggle source
Calls superclass method
# File lib/active_null.rb, line 29
def find_by(*args, &block)
  super || null
end
null() click to toggle source
# File lib/active_null.rb, line 5
def null
  null_class.get
end
null_associations() click to toggle source
# File lib/active_null.rb, line 33
  def null_associations
    self.reflect_on_all_associations.each do |relation|
      unless relation.collection?
        klass = begin
          if relation.options[:polymorphic]
            polymorphic_null_defaults[relation.name]
          elsif relation.klass
            relation.klass.name
          end
        rescue
          nil
        end
        next unless klass
        class_eval <<-CODE
          def #{relation.name}(*args)
            result = association(:#{relation.name}).reader(*args)
            return result if result || !#{klass}.respond_to?(:null)
            #{klass}.null
          end
        CODE
      end
    end
  end
null_class() click to toggle source
# File lib/active_null.rb, line 57
def null_class
  @null_class ||= NullModelBuilder.new(self, @null_model_overrides).build
end
null_defaults_for_polymorphic(mappings) click to toggle source
# File lib/active_null.rb, line 9
def null_defaults_for_polymorphic mappings
  @polymorphic_null_defaults = mappings
end
null_model(method_name=nil, &block) click to toggle source
# File lib/active_null.rb, line 17
def null_model(method_name=nil, &block)
  @null_model_overrides = if block_given?
    Module.new.tap { |m| m.module_eval(&block) }
  end

  if method_name
    singleton_class.class_eval do
      define_method(method_name) { null }
    end
  end
end
polymorphic_null_defaults() click to toggle source
# File lib/active_null.rb, line 13
def polymorphic_null_defaults
  @polymorphic_null_defaults || {}
end