Class: Driskell::Listen::Adapter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/driskell-listen/adapter/base.rb

Direct Known Subclasses

BSD, Darwin, Linux, Polling, Windows

Constant Summary collapse

DEFAULTS =

TODO: only used by tests

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/driskell-listen/adapter/base.rb', line 15

def initialize(config)
  @started = false
  @config = config

  @configured = nil

  fail 'No directories to watch!' if config.directories.empty?

  defaults = self.class.const_get('DEFAULTS')
  @options = Driskell::Listen::Options.new(config.adapter_options, defaults)
rescue
  _log_exception 'adapter config failed: %s:%s called from: %s', caller
  raise
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/driskell-listen/adapter/base.rb', line 13

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/driskell-listen/adapter/base.rb', line 8

def options
  @options
end

Class Method Details

.usable?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/driskell-listen/adapter/base.rb', line 92

def self.usable?
  const_get('OS_REGEXP') =~ RbConfig::CONFIG['target_os']
end

Instance Method Details

#configureObject

TODO: it’s a separate method as a temporary workaround for tests



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/driskell-listen/adapter/base.rb', line 31

def configure
  if @configured
    _log(:warn, 'Adapter already configured!')
    return
  end

  @configured = true

  @callbacks ||= {}
  config.directories.each do |dir|
    callback = @callbacks[dir] || lambda do |event|
      _process_event(dir, event)
    end
    @callbacks[dir] = callback
    _configure(dir, &callback)
  end

  @snapshots ||= {}
  # TODO: separate config per directory (some day maybe)
  change_config = Change::Config.new(config.queue, config.silencer)
  config.directories.each do |dir|
    record = Record.new(dir)
    snapshot = Change.new(change_config, record)
    @snapshots[dir] = snapshot
  end
end

#preempt_change(dir, rel_path, data) ⇒ Object

Seeds



97
98
99
100
101
102
103
104
# File 'lib/driskell-listen/adapter/base.rb', line 97

def preempt_change(dir, rel_path, data)
  if @snapshots.nil? || !@snapshots.key?(dir)
    _log(:warn, 'Cannot preempt change for directory not being watched')
    return
  end

  @snapshots[dir].record.update_file(rel_path, data)
end

#startObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/driskell-listen/adapter/base.rb', line 62

def start
  configure

  if started?
    _log(:warn, 'Adapter already started!')
    return
  end

  @started = true

  calling_stack = caller.dup
  Driskell::Listen::Internals::ThreadPool.add do
    begin
      @snapshots.values.each do |snapshot|
        _timed('Record.build()') { snapshot.record.build }
      end
      _run
    rescue
      msg = 'run() in thread failed: %s:\n'\
        ' %s\n\ncalled from:\n %s'
      _log_exception(msg, calling_stack)
      raise # for unit tests mostly
    end
  end
end

#started?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/driskell-listen/adapter/base.rb', line 58

def started?
  @started
end

#stopObject



88
89
90
# File 'lib/driskell-listen/adapter/base.rb', line 88

def stop
  _stop
end