Module: DefaultOpSets

Defined in:
lib/midas/node_element_operations.rb

Overview

rename module to DefaultOpSets

Constant Summary collapse

StaticAddOpDef =

Static Operations are for fixed values (i.e., any attempts at changes are ignored)

lambda{|this, other| Hash[:update_this => this] }
StaticSubtractOpDef =
lambda{|this, other| Hash[:update_this => this]}
StaticOpSet =
{:add => StaticAddOpDef, :subtract => StaticSubtractOpDef}
ReplaceAddOpDef =

We define a field where adding will replace the existing value for that field, and subtracting a matching value will set the value to nil

lambda { |this, other|   Hash[:update_this => other]  }
ReplaceSubtractOpDef =
lambda do |this, other|
  if (this == other)
    Hash[:update_this => nil] 
  else
    Hash[:update_this => this]
  end
end
ReplaceOpSet =
{:add => ReplaceAddOpDef, :subtract => ReplaceSubtractOpDef}
ListAddOpDef =

We define a field where adding will add the value to the existing list, and subtracting will remove matching values from the list

lambda  do |this,other|
  this = [this].flatten
  other = [other].flatten
  this = this + other
  this.uniq!; this.compact!
  Hash[:update_this => this]
end
ListSubtractOpDef =
lambda do |this,other| 
  this = [this].flatten
  other = [other].flatten
  this -= other
  this.uniq!
  this.compact!
  Hash[:update_this => this]
end
ListOpSet =
{:add => ListAddOpDef, :subtract => ListSubtractOpDef}
KListAddOpDef =

A bit more complicated is if we have a field that holds key-value pairs, but we want our operations to operate on the underlying values of the key-value pair, and not on the actual key value sets. Here the values are a list type. What happens is if an existing key is passed, the value is added to the set of values for the existing key. If a new key is passed, the new key and its value are added to the list

lambda do |this, other|
  this = this || {}  
  other = other || {}
  all_keys = this.keys + other.keys
  combined = {}
  all_keys.each do |k|
    this_list = [this[k]].flatten
    other_list = [other[k]].flatten
    combined[k] = (this_list + other_list).flatten
    #if this[k]
    #  this[k] = [this[k] ].flatten + [ other[k] ].flatten
    #else
    #  this[k] = [ other[k] ].flatten
    #end 
    combined[k].uniq!
    combined[k].compact!
  end
  Hash[:update_this => combined] 
end
KListSubtractOpDef =
lambda do |this, other|
  this = this || {}
  other = other || {}
  subtracted_list = {}
  this.keys.each do |k|
    this_list = [this[k]].flatten
    other_list = [other[k]].flatten
    #other[s].each {|olnk| this[k].delete(olnk) if this[k]}
    #this[k].delete(other[k]) if this[k]
    subtracted_list[k] = (this_list - other_list).flatten
    subtracted_list[k].compact!
    subtracted_list[k].uniq!
    #this.delete(k) if (this[k].nil? || this[k].empty?)
  end
  Hash[:update_this => subtracted_list]
end
KListGetKeyforValueOpDef =

With the KVP, we might want the keys that contain a given value note that in this case, the return value is not the same as the value stored in the field, hence the explicit return_value parameter Something to think about is whether this should be some type of recursive operation since the record is key-value, and the field is key-value

lambda do |this, values|
  values = [values].flatten
  this = this|| {}
  keys = []
  this.each do |k,v|
    values.each do |value|
      keys << k if v.include? value 
    end
  end
  rtn_val = if keys.size > 0
    {:return_value => keys, :update_this => this}
  else
    {:return_value => nil, :update_this => this}
  end
  rtn_val
end
KListOpSet =
{:add => KListAddOpDef, 
:subtract => KListSubtractOpDef,
:getkeys => KListGetKeyforValueOpDef}

Class Attribute Summary collapse

Class Attribute Details

.op_sets_to_def_tableObject

Returns the value of attribute op_sets_to_def_table.



13
14
15
# File 'lib/midas/node_element_operations.rb', line 13

def op_sets_to_def_table
  @op_sets_to_def_table
end