Class: Polymorpheus::InterfaceBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/polymorpheus.rb,
lib/polymorpheus/interface_builder.rb,
lib/polymorpheus/interface_builder/association.rb

Defined Under Namespace

Classes: Association

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface_name, association_names, options) ⇒ InterfaceBuilder

Returns a new instance of InterfaceBuilder.



9
10
11
12
13
14
# File 'lib/polymorpheus/interface_builder.rb', line 9

def initialize(interface_name, association_names, options)
  @interface_name = interface_name
  @associations = association_names.map do |association_name|
    Polymorpheus::InterfaceBuilder::Association.new(association_name, options)
  end
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



6
7
8
# File 'lib/polymorpheus/interface_builder.rb', line 6

def associations
  @associations
end

#interface_nameObject (readonly)

Returns the value of attribute interface_name.



6
7
8
# File 'lib/polymorpheus/interface_builder.rb', line 6

def interface_name
  @interface_name
end

Instance Method Details

#active_association(calling_object) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/polymorpheus/interface_builder.rb', line 32

def active_association(calling_object)
  active_associations = associations.select do |association|
    # If the calling object has a non-nil value for the association
    # key, we know it has an active associatin without having to
    # make a database query to retrieve the associated object itself.
    #
    # If it has a nil value for the association key, we then ask if
    # it has a non-nil result for the association itself, since it
    # may have an active association that has not yet been saved to
    # the database.
    #
    calling_object.public_send(association.key).present? ||
      calling_object.public_send(association.name).present?
  end

  active_associations.first if active_associations.length == 1
end

#active_association_key(calling_object) ⇒ Object



50
51
52
53
54
55
# File 'lib/polymorpheus/interface_builder.rb', line 50

def active_association_key(calling_object)
  association = active_association(calling_object)
  return unless association

  association.key if calling_object.public_send(association.key)
end

#association_keysObject



24
25
26
# File 'lib/polymorpheus/interface_builder.rb', line 24

def association_keys
  @association_keys ||= associations.map(&:key)
end

#association_namesObject



28
29
30
# File 'lib/polymorpheus/interface_builder.rb', line 28

def association_names
  @association_names ||= associations.map(&:name)
end

#exposed_interface(calling_object) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/polymorpheus/interface_builder.rb', line 16

def exposed_interface(calling_object)
  OpenStruct.new(
    associations: associations,
    active_association: active_association(calling_object),
    query_condition: query_condition(calling_object)
  )
end

#get_associated_object(calling_object) ⇒ Object



64
65
66
67
# File 'lib/polymorpheus/interface_builder.rb', line 64

def get_associated_object(calling_object)
  association = active_association(calling_object)
  calling_object.public_send(association.name) if association
end

#get_relevant_association_for_object(object_to_associate) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/polymorpheus/interface_builder.rb', line 78

def get_relevant_association_for_object(object_to_associate)
  match = associations.select do |association|
    object_to_associate.is_a?(association.association_class)
  end

  if match.blank?
    raise Polymorpheus::Interface::InvalidTypeError, association_names
  elsif match.length > 1
    raise Polymorpheus::Interface::AmbiguousTypeError
  end

  match.first
end

#query_condition(calling_object) ⇒ Object



57
58
59
60
61
62
# File 'lib/polymorpheus/interface_builder.rb', line 57

def query_condition(calling_object)
  key = active_association_key(calling_object)
  object = calling_object.public_send(key) if key

  { key.to_s => object } if object
end

#set_associated_object(calling_object, object_to_associate) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/polymorpheus/interface_builder.rb', line 69

def set_associated_object(calling_object, object_to_associate)
  association = get_relevant_association_for_object(object_to_associate)
  calling_object.public_send("#{association.name}=", object_to_associate)

  (associations - [association]).each do |association|
    calling_object.public_send("#{association.name}=", nil)
  end
end