class Opium::Model::Reference

Attributes

context[RW]
metadata[RW]

Public Class Methods

new( metadata, context ) click to toggle source
Calls superclass method
# File lib/opium/model/reference.rb, line 17
def initialize( metadata, context )
  self.metadata = metadata
  self.context = context
  fail ArgumentError, 'did not receive a context object!' unless context
  super( nil )
end
to_ruby( value ) click to toggle source
# File lib/opium/model/reference.rb, line 5
def to_ruby( value )
  case value
  when Hash
    new( value[:metadata] || value['metadata'], value[:context] || value['context'] )
  when self
    value
  else
    fail ArgumentError, "could not convert #{ value.inspect } into an Opium::Model::Reference"
  end
end

Public Instance Methods

__getobj__() click to toggle source
# File lib/opium/model/reference.rb, line 26
def __getobj__
  @reference || __setobj__( lookup_reference )
end
__setobj__( obj ) click to toggle source
# File lib/opium/model/reference.rb, line 30
def __setobj__( obj )
  @reference = obj
end
inspect() click to toggle source
# File lib/opium/model/reference.rb, line 34
def inspect
  if @reference
    @reference.inspect
  else
    "#<#{ self.class.name }<#{ self.metadata.target_class_name }>>"
  end
end

Private Instance Methods

lookup_reference() click to toggle source
# File lib/opium/model/reference.rb, line 44
def lookup_reference
  return nil if context.new_record?
  self.metadata.target_class_name.constantize.where( self.metadata.inverse_relation_name => self.context ).first
end