Class: Toy::Reference

Inherits:
Object show all
Defined in:
lib/toy/reference.rb

Defined Under Namespace

Classes: ReferenceProxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, *args) ⇒ Reference

Returns a new instance of Reference.



5
6
7
8
9
10
11
12
13
14
# File 'lib/toy/reference.rb', line 5

def initialize(model, name, *args)
  @model   = model
  @name    = name.to_sym
  @options = args.extract_options!
  @type    = args.shift

  model.references[name] = self
  model.attribute(key, type.key_type)
  create_accessors
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



3
4
5
# File 'lib/toy/reference.rb', line 3

def model
  @model
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/toy/reference.rb', line 3

def name
  @name
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/toy/reference.rb', line 3

def options
  @options
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Returns:



32
33
34
35
36
# File 'lib/toy/reference.rb', line 32

def eql?(other)
  self.class.eql?(other.class) &&
    model == other.model &&
    name  == other.name
end

#instance_variableObject



24
25
26
# File 'lib/toy/reference.rb', line 24

def instance_variable
  @instance_variable ||= :"@_#{name}"
end

#keyObject



20
21
22
# File 'lib/toy/reference.rb', line 20

def key
  @key ||= :"#{name.to_s.singularize}_id"
end

#new_proxy(owner) ⇒ Object



28
29
30
# File 'lib/toy/reference.rb', line 28

def new_proxy(owner)
  ReferenceProxy.new(self, owner)
end

#typeObject



16
17
18
# File 'lib/toy/reference.rb', line 16

def type
  @type ||= name.to_s.classify.constantize
end