Class: Sequencescape::Api::Associations::BelongsTo::AssociationProxy

Inherits:
Sequencescape::Api::Associations::Base show all
Defined in:
lib/sequencescape-api/associations/belongs_to.rb

Defined Under Namespace

Classes: LoadHandler

Instance Method Summary (collapse)

Methods included from InstanceMethods

#attributes_for, #attributes_for?, #errors, #run_validations!

Constructor Details

- (AssociationProxy) initialize(*args, &block)

Returns a new instance of AssociationProxy



7
8
9
10
# File 'lib/sequencescape-api/associations/belongs_to.rb', line 7

def initialize(*args, &block)
  super
  @object, @loaded = new(@attributes, false), false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(name, *args, &block) (protected)



17
18
19
20
# File 'lib/sequencescape-api/associations/belongs_to.rb', line 17

def method_missing(name, *args, &block)
  target = @object.eager_loaded_attribute?(name) ? @object : object
  target.send(name, *args, &block)
end

Instance Method Details

- (Object) as_json(options = nil)



48
49
50
# File 'lib/sequencescape-api/associations/belongs_to.rb', line 48

def as_json(options = nil)
  @object.as_json({ :root => false, :uuid => false }.reverse_merge(options || {}))
end

- (Boolean) eql?(proxy_or_object)

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/sequencescape-api/associations/belongs_to.rb', line 53

def eql?(proxy_or_object)
  proxy_or_object = proxy_or_object.instance_variable_get(:@object) if proxy_or_object.is_a?(self.class)
  @object.eql?(proxy_or_object)
end

- (Boolean) respond_to?(name, include_private = false)

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/sequencescape-api/associations/belongs_to.rb', line 12

def respond_to?(name, include_private = false)
  # One of our methods, or an eager loaded attribute, or the object needs to be loaded & checked
  super or @object.eager_loaded_attribute?(name) or object.respond_to?(name, include_private)
end