Module: Isimud::ModelWatcher::ClassMethods

Defined in:
lib/isimud/model_watcher.rb

Instance Method Summary collapse

Instance Method Details

#isimud_model_watcher_typeObject



72
73
74
# File 'lib/isimud/model_watcher.rb', line 72

def isimud_model_watcher_type
  (respond_to?(:base_class) ? base_class.name : name).demodulize
end

#sync_include(_sync_includes) ⇒ Object

Include the following tables when fetching records for synchronization



37
38
39
# File 'lib/isimud/model_watcher.rb', line 37

def sync_include(_sync_includes)
  self.sync_includes = _sync_includes
end

#synchronize(options = {}) ⇒ Integer

Synchronize instances of this model with the data warehouse. This is accomplished by calling isimud_notify_updated() on each instance fetched from the database.

Parameters:

  • options (Hash) (defaults to: {})

    synchronize options

Options Hash (options):

  • :where (ActiveRecord::Relation)

    where_clause filter for limiting records to sync. By default, all records are synchronized.

  • :output (IO)

    optional stream for writing progress. A ‘.’ is printed for every 100 records synchronized.

Returns:

  • (Integer)

    number of records synchronized



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/isimud/model_watcher.rb', line 47

def synchronize(options = {})
  where_clause = options[:where] || {}
  output       = options[:output] || nil
  count        = 0
  query        = self.where(where_clause)
  query        = query.includes(sync_includes) if sync_includes
  query.find_each do |m|
    next unless m.isimud_synchronize?
    begin
      m.isimud_sync
    rescue Bunny::ClientTimeout, Timeout::Error => e
      output && output.print("\n#{e}, sleeping for 10 seconds")
      sleep(10)
      m.isimud_sync
    end
    if (count += 1) % 100 == 0
      output && output.print('.')
    end
    if (count % 1000) == 0
      GC.start
    end
  end
  count
end

#watch_attributes(*attributes) ⇒ Object

Set attributes to observe and include in messages. Any property method with a return value may be included in the list of attributes.

Parameters:

  • attributes (Array<String,Symbol>)

    list of attributes / properties



32
33
34
# File 'lib/isimud/model_watcher.rb', line 32

def watch_attributes(*attributes)
  self.isimud_watch_attributes = attributes.flatten.map(&:to_s) if attributes.present?
end