Class: Protip::Resource::Associations::BelongsToAssociation

Inherits:
Object
  • Object
show all
Includes:
Association
Defined in:
lib/protip/resource/associations/belongs_to_association.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Association

#define_accessors!

Constructor Details

#initialize(resource_class, association_name, id_field: nil, class_name: nil) ⇒ BelongsToAssociation

Returns a new instance of BelongsToAssociation.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/protip/resource/associations/belongs_to_association.rb', line 14

def initialize(resource_class, association_name, id_field: nil, class_name: nil)
  # The resource type that houses the association
  @resource_class = resource_class

  # The name for generating accessor methods
  @association_name = association_name

  # The field that holds the ID for the association
  @id_field = (id_field || self.class.default_id_field(association_name)).to_sym

  @class_name = (class_name || self.class.default_class_name(association_name)).to_s
end

Instance Attribute Details

#association_nameObject (readonly)

Returns the value of attribute association_name.



12
13
14
# File 'lib/protip/resource/associations/belongs_to_association.rb', line 12

def association_name
  @association_name
end

#id_fieldObject (readonly)

Returns the value of attribute id_field.



12
13
14
# File 'lib/protip/resource/associations/belongs_to_association.rb', line 12

def id_field
  @id_field
end

#resource_classObject (readonly)

Returns the value of attribute resource_class.



12
13
14
# File 'lib/protip/resource/associations/belongs_to_association.rb', line 12

def resource_class
  @resource_class
end

Class Method Details

.default_class_name(association_name) ⇒ Object



56
57
58
# File 'lib/protip/resource/associations/belongs_to_association.rb', line 56

def default_class_name(association_name)
  association_name.to_s.classify
end

.default_id_field(association_name) ⇒ Object



53
54
55
# File 'lib/protip/resource/associations/belongs_to_association.rb', line 53

def default_id_field(association_name)
  "#{association_name}_id".to_sym
end

Instance Method Details

#associated_resource_classObject



27
28
29
# File 'lib/protip/resource/associations/belongs_to_association.rb', line 27

def associated_resource_class
  @associated_resource_class ||= @class_name.constantize
end

#read(resource) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/protip/resource/associations/belongs_to_association.rb', line 31

def read(resource)
  id = resource.public_send(@id_field)
  if id == nil
    nil
  else
    associated_resource_class.find id
  end
end

#write(resource, value) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/protip/resource/associations/belongs_to_association.rb', line 40

def write(resource, value)
  if value != nil
    unless value.is_a?(associated_resource_class)
      raise ArgumentError.new("Cannot assign #{value.class} to #{resource_class}##{@id_field}")
    end
    unless value.persisted?
      raise "Cannot assign non-persisted resource to association #{resource_class}##{association_name}"
    end
  end
  resource.public_send(:"#{@id_field}=", value.try(:id))
end