Class: RubySync::Event

Inherits:
Object show all
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

Constructor Details

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

Returns a new instance of Event.



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

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 …



47
48
49
# File 'lib/ruby_sync/event.rb', line 47

def association
  @association
end

#payloadObject

delete, add, modify …



47
48
49
# File 'lib/ruby_sync/event.rb', line 47

def payload
  @payload
end

#sourceObject

delete, add, modify …



47
48
49
# File 'lib/ruby_sync/event.rb', line 47

def source
  @source
end

#source_pathObject

delete, add, modify …



47
48
49
# File 'lib/ruby_sync/event.rb', line 47

def source_path
  @source_path
end

#target_pathObject

delete, add, modify …



47
48
49
# File 'lib/ruby_sync/event.rb', line 47

def target_path
  @target_path
end

#typeObject

delete, add, modify …



47
48
49
# File 'lib/ruby_sync/event.rb', line 47

def type
  @type
end

Class Method Details

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



62
63
64
# File 'lib/ruby_sync/event.rb', line 62

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



58
59
60
# File 'lib/ruby_sync/event.rb', line 58

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

.force_resync(source) ⇒ Object



54
55
56
# File 'lib/ruby_sync/event.rb', line 54

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

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



66
67
68
# File 'lib/ruby_sync/event.rb', line 66

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



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

def add_default field_name, value
  add_value field_name, value unless sets_value? field_name
end

#add_value(field_name, value) ⇒ Object



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

def add_value field_name, value
  uncommitted_operations << Operation.new(:add, field_name, value.as_array)
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.



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

def append new_operations
  uncommitted_operations
  @uncommitted_operations += new_operations.as_array
end

#associated?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/ruby_sync/event.rb', line 92

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

#commit_changesObject



192
193
194
195
196
197
# File 'lib/ruby_sync/event.rb', line 192

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.



105
106
107
108
109
110
111
112
# File 'lib/ruby_sync/event.rb', line 105

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



114
115
116
117
118
119
120
# File 'lib/ruby_sync/event.rb', line 114

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



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

def drop_all_but_changes_to subject
  subjects = subject.as_array.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).



143
144
145
146
147
# File 'lib/ruby_sync/event.rb', line 143

def drop_changes_to subject
  subjects = subject.as_array
  uncommitted_operations
  @uncommitted_operations = @uncommitted_operations.delete_if {|op| subjects.include? op.subject }
end

#merge(other) ⇒ Object



96
97
98
99
# File 'lib/ruby_sync/event.rb', line 96

def merge other
  # TODO implement merge
  log.warn "Event.merge not yet implemented"
end

#retrieve_association(context) ⇒ Object



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

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



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

def rollback_changes
  @uncommitted_operations = nil
end

#set_value(field_name, value) ⇒ Object



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

def set_value field_name, value
  uncommitted_operations << Operation.new(:replace, field_name, value.as_array)
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)


133
134
135
136
137
138
139
# File 'lib/ruby_sync/event.rb', line 133

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 == value.as_array)
  end
  return false
end

#to_yaml_propertiesObject



123
124
125
# File 'lib/ruby_sync/event.rb', line 123

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

#uncommitted_operationsObject



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

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

#uncommitted_operations=(ops) ⇒ Object



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

def uncommitted_operations= ops
  @uncommitted_operations = ops
end