Sha256: c404b31df491cd5322bcd69193ac1cd65e29eb2c274005ca18c3c1099fb9bd1d

Contents?: true

Size: 938 Bytes

Versions: 1

Compression:

Stored size: 938 Bytes

Contents

module Recliner
  module Associations
    class Reference
      attr_reader :id
      
      def initialize(id=nil)
        @id = id
      end
      
      def ==(other)
        other.is_a?(Reference) && id == other.id
      end

      def self.from_couch(id)
        new(id)
      end
      
      def self.parse(id)
        new(id)
      end
      
      def to_s
        id.to_s
      end
      
      def blank?
        id.blank?
      end
      
      def inspect
        id.nil? ? 'nil' : id
      end
      
      def replace(object)
        @id = object.id
        @target = object
      end
      
      def reload
        @target = nil
      end
      
      def target
        @target ||= Recliner::Document.load!(id) if id
      end
    end
  end
  
  Conversions.register(Associations::Reference, :couch) { |ref| ref.id }
  Conversions.register(String, Associations::Reference) { |str| Associations::Reference.new(str) }
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
recliner-0.0.1 lib/recliner/associations/reference.rb