Class: Naf::Affinity

Inherits:
NafBase show all
Defined in:
app/models/naf/affinity.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NafBase

full_table_name_prefix

Class Method Details

.names_listObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/naf/affinity.rb', line 54

def self.names_list
  selectable.map do |a|
    classification = a.affinity_classification
    if classification.affinity_classification_name == 'machine'
      if a.affinity_short_name.present?
        [a.affinity_short_name, a.id]
      elsif ::Naf::Machine.find_by_id(Integer(a.affinity_name)).present?
        machine = ::Naf::Machine.find_by_id(Integer(a.affinity_name))
        [machine.hostname, a.id]
      else
        ['Bad affinity: ' + classification.affinity_classification_name + ', ' + a.affinity_name, a.id]
      end
    else
      [classification.affinity_classification_name + ', ' + a.affinity_name, a.id]
    end
  end
end

.pre_unpickle(unpickler) ⇒ Object



72
73
74
75
76
77
78
79
# File 'app/models/naf/affinity.rb', line 72

def self.pre_unpickle(unpickler)
  unpickler.references.merge!(Hash[::Naf::Affinity.all.map do |m|
                                [{ association_model_name: ::Naf::Affinity.name,
                                   association_classification_value: m.affinity_classification_id,
                                   association_affinity_value: m.affinity_name }, m]
                              end
                             ])
end

.selectableObject


*** Class Methods *** ++++++++++++++++++++++



50
51
52
# File 'app/models/naf/affinity.rb', line 50

def self.selectable
  where(selectable: true)
end

.unpickle(preserve, unpickler) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/naf/affinity.rb', line 81

def self.unpickle(preserve, unpickler)
  reference = unpickler.references[{ association_model_name: self.name,
                                     association_classification_value: preserve['affinity_classification_id']['association_value'],
                                     association_affinity_value: preserve['affinity_name'] }]
  if reference.present?
    return {}
  end

  attributes = {}
  reference_methods = []
  preserve.each do |method_name, value|
    unless method_name == 'id'
      if value.is_a?(Hash)
        # this is a reference
        reference_instance = unpickler.retrieve_reference(value)
        if reference_instance.nil?
          logger.error value.inspect
          logger.error { unpickler.references.map{ |r| r.inspect }.join("\n") }
          raise "couldn't find reference for #{value.inspect} in #{preserve.inspect}"
        end
        attributes[method_name.to_sym] = reference_instance.id
      else
        attributes[method_name.to_sym] = value
      end
    end
  end

  instance = self.new
  attributes.each do |method, value|
    instance.send("#{method}=".to_sym, value)
  end
  instance.save!
  logger.info "created #{instance.inspect}"

  return { { association_model_name: self.name,
             association_classification_value: preserve['affinity_classification_id']['association_value'],
             association_affinity_value: preserve['affinity_name'] } => instance }
end

Instance Method Details

#to_sObject


*** Instance Methods *** +++++++++++++++++++++++++



124
125
126
127
128
129
130
131
# File 'app/models/naf/affinity.rb', line 124

def to_s
  components = []
  components << "UNSELECTABLE" unless selectable
  components << "classification: \"#{affinity_classification_name}\""
  components << "name: \"#{affinity_name}\""

  return "::Naf::Affinity<#{components.join(', ')}>"
end

#validate_affinity_nameObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/models/naf/affinity.rb', line 133

def validate_affinity_name
  if affinity_classification.present? &&
    affinity_classification.affinity_classification_name == 'machine'

    machine = ::Naf::Machine.find_by_id(affinity_name)
    if machine.blank?
      return "There isn't a machine with that id!"
    end

    count = ::Naf::Affinity.
      where(affinity_classification_id: ::Naf::AffinityClassification.
              find_by_affinity_classification_name('machine').id,
            affinity_name: machine.id.to_s).count

    if count > 0
      return 'An affinity with the pair value (affinity_classification_id, affinity_name) already exists!'
    end
  end

  nil
end