Module: DataAssociator

Included in:
Item, Operation, OperationType
Defined in:
app/helpers/data_associator.rb

Overview

Associates and manages DataAssociations

Instance Method Summary collapse

Instance Method Details

#append_notes(text) ⇒ Object

Appends text to the associated notes for this object.

Parameters:

  • text (String)

    the content to be added



167
168
169
170
171
172
173
174
175
176
177
# File 'app/helpers/data_associator.rb', line 167

def append_notes(text)
  da = get_association :notes
  if da
    current = da.full_object[:notes]
    da.object = { notes: current + text.to_s }.to_json
    da.save
  else
    associate :notes, text.to_s
  end
  text
end

#associate(key, value, upload = nil, options = { duplicates: false }) ⇒ Object

Add a DataAssociation to this object to a value and an Upload, and with the given key.

If an association with the key exists, then the association will be modified (@see modify).

params options[:duplicates] [Boolean] whether to duplicate an existing key. Default is false

Examples:

Associate concentration with an operation's input

op.input("Fragment").item.associate :concentration, 42

Parameters:

  • key (String)

    the key for the new association

  • value (Object)

    the value for the new association (may be any serializable value)

  • upload (Upload) (defaults to: nil)

    the upload object (default: nil)



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/helpers/data_associator.rb', line 93

def associate(key, value, upload = nil, options = { duplicates: false })

  if options[:duplicates] || data_associations(key).empty?
    da = DataAssociation.create_from(
      parent_id: id,
      parent_class: self.class.to_s,
      key: key,
      value: value,
      upload: upload
    )
    da.save
    errors.add :data_association_error, "Could not save data association named '#{key}': #{da.errors.full_messages.join(', ')}" unless da.errors.empty?
  else
    modify key, value, upload
  end

  self
end

#associationsHash

Return the Hash of all DataAssociations for this object.

Returns:

  • (Hash)

    the hash map of all associations for this object



29
30
31
32
33
34
35
36
37
# File 'app/helpers/data_associator.rb', line 29

def associations
  h = HashWithIndifferentAccess.new
  associations = DataAssociation.select_most_recent(data_associations)
  associations.each do |association|
    h[association.key] = association.value
  end

  h
end

#data_associations(key = nil) ⇒ Array<DataAssociation>

Return the DataAssociations for this object that have the given key, or all associations if no key is given. Includes the upload object for the association if there is one.

Parameters:

  • key (String) (defaults to: nil)

    the key for the association

Returns:



19
20
21
22
23
24
# File 'app/helpers/data_associator.rb', line 19

def data_associations(key = nil)
  klass = self.class.to_s
  klass = %w[Item Collection] if is_a?(Item) || is_a?(Collection)

  DataAssociation.associations_for(parent_id: id, parent_class: klass, key: key)
end

#get(key) ⇒ DataAssociation

Get the DataAssociation with the given key for this object.

Parameters:

  • key (String)

    the key for the association

Returns:



52
53
54
55
# File 'app/helpers/data_associator.rb', line 52

def get(key)
  da = get_association key
  da ? da.full_object[key] : nil
end

#get_association(key) ⇒ DataAssociation

Get the DataAssociation with the given key for this object.

Parameters:

  • key (String)

    the key for the association

Returns:



43
44
45
46
# File 'app/helpers/data_associator.rb', line 43

def get_association(key)
  das = data_associations key
  das.length >= 1 ? das[0] : nil
end

#lazy_associate(key, value, upload = nil) ⇒ Object

Create a DataAssociation to this object to a value and an Upload, and with the given key. Does not save the association. For use with methods that collect a set of associations and save them all at once.

If an association with the key exists, then do nothing.

Examples:

Associate concentration with an operation's input

da = op.input("Fragment").item.lazy_associate :concentration, 42

Parameters:

  • key (String)

    the key for the new association

  • value (Object)

    the value for the new association (may be any serializable value)

  • upload (Upload) (defaults to: nil)

    the upload object (default: nil)



123
124
125
126
127
128
129
130
131
# File 'app/helpers/data_associator.rb', line 123

def lazy_associate(key, value, upload = nil)
  DataAssociation.create_from(
    parent_id: id,
    parent_class: self.class.to_s,
    key: key,
    value: value,
    upload: upload
  )
end

#modify(key, value, upload = nil) ⇒ Object

Modifies the existing association for the key.

Parameters:

  • key (String)

    the key for the association

  • value (Object)

    the new value for the association (may be any serializable value)

  • upload (Upload) (defaults to: nil)

    the upload object (default: nil)

See Also:



139
140
141
142
143
144
145
146
147
148
149
# File 'app/helpers/data_associator.rb', line 139

def modify(key, value, upload = nil)
  da = get_association key
  if da
    da.object = { key => value }.to_json
    da.upload = upload if upload
    da.save
  else
    errors.add :data_association_error, "Data association named '#{key}' not found."
  end
  self
end

#notesObject

Return the notes association for this object.

Returns:

  • the notes association for this object



69
70
71
72
73
74
75
76
77
# File 'app/helpers/data_associator.rb', line 69

def notes
  da = get_association :notes
  if da
    da.full_object[:notes]
  else
    associate :notes, ''
    ''
  end
end

#notes=(text) ⇒ Object

Sets the notes association for this object.

Parameters:

  • text (String)

    the text content of the notes for this object



154
155
156
157
158
159
160
161
162
# File 'app/helpers/data_associator.rb', line 154

def notes=(text)
  da = get_association :notes
  if da
    da.object = { notes: text.to_s }.to_json
    da.save
  else
    associate :notes, text.to_s
  end
end

#upload(key) ⇒ Upload

Get Upload from DataAssociation by key.

Parameters:

  • key (String)

    the key for the association

Returns:

  • (Upload)

    the upload object for the association, nill if there is none.



61
62
63
64
# File 'app/helpers/data_associator.rb', line 61

def upload(key)
  da = get_association key
  da ? da.upload : nil
end