Class: RubySync::Event

Inherits:
Object show all
Includes:
Utilities
Defined in:
lib/ruby_sync/event.rb

Overview

Represents a change of some type to a record in the source datastore. If the event type is :add or :modify then the payload will be an array of RubySync::Operations describing changes to the attributes of the record.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#as_array, #base_path, #call_if_exists, #connector_called, #effective_operations, #ensure_dir_exists, #find_base_path, #get_preference, #get_preference_file_path, #include_in_search_path, #log_progress, #perform_operations, #pipeline_called, #set_preference, #something_called, #with_rescue

Constructor Details

#initialize(type, source, source_path = nil, association = nil, payload = nil) ⇒ Event

Returns a new instance of Event.



72
73
74
75
76
77
78
79
# File 'lib/ruby_sync/event.rb', line 72

def initialize type, source, source_path=nil, association=nil, payload=nil
  self.type = type.to_sym
  self.source = source
  self.source_path = source_path
  self.association = make_association(association)
  self.payload = payload
  @target_path = nil
end

Instance Attribute Details

#associationObject

delete, add, modify …



49
50
51
# File 'lib/ruby_sync/event.rb', line 49

def association
  @association
end

#payloadObject

delete, add, modify …



49
50
51
# File 'lib/ruby_sync/event.rb', line 49

def payload
  @payload
end

#sourceObject

delete, add, modify …



49
50
51
# File 'lib/ruby_sync/event.rb', line 49

def source
  @source
end

#source_pathObject

delete, add, modify …



49
50
51
# File 'lib/ruby_sync/event.rb', line 49

def source_path
  @source_path
end

#target_pathObject

delete, add, modify …



49
50
51
# File 'lib/ruby_sync/event.rb', line 49

def target_path
  @target_path
end

#typeObject

delete, add, modify …



49
50
51
# File 'lib/ruby_sync/event.rb', line 49

def type
  @type
end

Class Method Details

.add(source, source_path, association = nil, payload = nil) ⇒ Object



64
65
66
# File 'lib/ruby_sync/event.rb', line 64

def self.add source, source_path, association=nil, payload=nil
  self.new(:add, source, source_path, association, payload)
end

.delete(source, source_path, association = nil) ⇒ Object



60
61
62
# File 'lib/ruby_sync/event.rb', line 60

def self.delete source, source_path, association=nil
  self.new(:delete, source, source_path, association)
end

.force_resync(source) ⇒ Object



56
57
58
# File 'lib/ruby_sync/event.rb', line 56

def self.force_resync source
  self.new(:force_resync, source)
end

.modify(source, source_path, association = nil, payload = nil) ⇒ Object



68
69
70
# File 'lib/ruby_sync/event.rb', line 68

def self.modify source, source_path, association=nil, payload=nil
  self.new(:modify, source, source_path, association, payload)
end

Instance Method Details

#add_default(field_name, value) ⇒ Object

Add a value to a given subject unless it already sets a value



160
161
162
# File 'lib/ruby_sync/event.rb', line 160

def add_default field_name, value
  add_value(field_name.to_s, value) unless sets_value? field_name.to_s
end

#add_value(field_name, value) ⇒ Object



165
166
167
# File 'lib/ruby_sync/event.rb', line 165

def add_value field_name, value
  uncommitted_operations << Operation.new(:add, field_name.to_s, as_array(value))
end

#append(new_operations) ⇒ Object

Add one or more operations to the list to be performed. The operations won’t be added to the payload until commit_changes is called and won’t be added at all if rollback_changes is called first.



196
197
198
199
# File 'lib/ruby_sync/event.rb', line 196

def append new_operations
  uncommitted_operations
  @uncommitted_operations += as_array(new_operations)
end

#associated?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/ruby_sync/event.rb', line 94

def associated?
  self.association && self.association.context && self.association.key
end

#commit_changesObject



206
207
208
209
210
211
# File 'lib/ruby_sync/event.rb', line 206

def commit_changes
  if uncommitted_operations 
    @payload = uncommitted_operations
    @uncommitted_operations = nil
  end
end

#convert_to_addObject

Retrieves all known values for the record affected by this event and sets the event’s type to :add If the source connector doesn’t implement retrieve we’ll assume thats because it can’t and that it gave us all it had to start with.



110
111
112
113
114
115
116
117
# File 'lib/ruby_sync/event.rb', line 110

def convert_to_add
  log.info "Converting '#{type}' event to add"
  if (source.respond_to? :retrieve)
    full = source.retrieve(source_path)
    payload = full.payload
  end
  @type = :add
end

#convert_to_modifyObject



119
120
121
122
123
124
125
# File 'lib/ruby_sync/event.rb', line 119

def convert_to_modify
  log.info "Converting '#{type}' event to modify"
  @type = :modify
  @payload.each do |op|
    op.type = :replace
  end
end

#drop_all_but_changes_to(subject) ⇒ Object



154
155
156
157
# File 'lib/ruby_sync/event.rb', line 154

def drop_all_but_changes_to subject
  subjects = as_array(subject).map {|s| s.to_s}
  @uncommitted_operations = uncommitted_operations.delete_if {|op| !subjects.include?(op.subject.to_s)}
end

#drop_changes_to(subject) ⇒ Object

Remove any operations from the payload that affect fields with the given key or keys (key can be a single field name or an array of field names).



148
149
150
151
152
# File 'lib/ruby_sync/event.rb', line 148

def drop_changes_to subject
  subjects = as_array(subject).map {|s| s.to_s}
  uncommitted_operations
  @uncommitted_operations = @uncommitted_operations.delete_if {|op| subjects.include? op.subject }
end

#map(left, right = nil, &blk) ⇒ Object

Typically this will be called in the ‘transform_in’ and ‘transform_out’ blocks in a pipeline configuration.



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/ruby_sync/event.rb', line 215

def map(left, right=nil, &blk)
  if right
    drop_changes_to left
    @uncommitted_operations = uncommitted_operations.map do |op|
      (op.subject.to_s == right.to_s)? op.same_but_on(left.to_s) : op
    end
  elsif blk and [:add, :modify].include? @type
    drop_changes_to left.to_s
    uncommitted_operations << RubySync::Operation.replace(left.to_s, blk.call) 
  end
end

#merge(other) ⇒ Object

Reduces the operations in this event to those that will alter the target record



100
101
102
103
104
# File 'lib/ruby_sync/event.rb', line 100

def merge other
#      other.type == :add or raise "Can only merge with add events"
#      record = perform_operations(other.payload)
  payload = effective_operations(@payload, other)
end

#retrieve_association(context) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ruby_sync/event.rb', line 81

def retrieve_association(context)
  if self.source.is_vault?
    self.association ||=  self.source.association_for(context, self.source_path)
  else
    if self.association # association key was supplied when the event was created
      self.association.context = context # just add the context
    else
      key = self.source.own_association_key_for(self.source_path)
      @association = Association.new(context, key)
    end
  end
end

#rollback_changesObject

Rollback any changes that



202
203
204
# File 'lib/ruby_sync/event.rb', line 202

def rollback_changes
  @uncommitted_operations = nil
end

#set_value(field_name, value) ⇒ Object



169
170
171
# File 'lib/ruby_sync/event.rb', line 169

def set_value field_name, value
  uncommitted_operations << Operation.new(:replace, field_name.to_s, as_array(value))
end

#sets_value?(subject, value = nil) ⇒ Boolean

True if this event will lead to the field name given being set. If value is non-nil then if it will lead to it being set to the value given. Note: This implementation is not completely accurate. Just looks at the last operation in the payload. A better implementation would look at all items that affect the named field to work out the value.

Returns:

  • (Boolean)


138
139
140
141
142
143
144
# File 'lib/ruby_sync/event.rb', line 138

def sets_value? subject, value=nil
  return false if @payload == nil
  @payload.reverse_each do |op|
    return true if op.subject == subject.to_s && (value == nil || op.values == as_array(value))
  end
  return false
end

#to_yaml_propertiesObject



128
129
130
# File 'lib/ruby_sync/event.rb', line 128

def to_yaml_properties
  %w{ @type @source_path @target_path @association @payload}
end

#uncommitted_operationsObject



183
184
185
186
# File 'lib/ruby_sync/event.rb', line 183

def uncommitted_operations
  @uncommitted_operations ||= @payload || []
  return @uncommitted_operations
end

#uncommitted_operations=(ops) ⇒ Object



188
189
190
# File 'lib/ruby_sync/event.rb', line 188

def uncommitted_operations= ops
  @uncommitted_operations = ops
end

#value_for(field_name) ⇒ Object



178
179
180
181
# File 'lib/ruby_sync/event.rb', line 178

def value_for field_name
  values = values_for field_name
  (values)? values[0] : nil
end

#values_for(field_name) ⇒ Object



173
174
175
176
# File 'lib/ruby_sync/event.rb', line 173

def values_for field_name
  values = perform_operations @payload, {}, :subjects=>[field_name.to_s]
  values[field_name.to_s]
end