File: support.rb

package info (click to toggle)
ruby-fssm 0.2.10-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 380 kB
  • sloc: ruby: 1,082; makefile: 2
file content (75 lines) | stat: -rw-r--r-- 1,443 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require 'rbconfig'

module FSSM::Support
  class << self
    def usable_backend
      case
        when mac? && rb_fsevent?
          'RBFSEvent'
        when linux? && rb_inotify?
          'Inotify'
        else
          'Polling'
      end
    end
    
    def optimal_backend_dependency
      return case
        when mac?     then  ['rb-fsevent', '>= 0.4.3.1']
        when linux?   then  ['rb-inotify', '>= 0.8.8']
        else                [nil, nil]
      end
    end

    def backend
      @@backend ||= usable_backend
    end

    def jruby?
      defined?(JRUBY_VERSION)
    end

    def mac?
      RbConfig::CONFIG['target_os'] =~ /darwin/i
    end

    def lion?
      RbConfig::CONFIG['target_os'] =~ /darwin11/i
    end

    def linux?
      RbConfig::CONFIG['target_os'] =~ /linux/i
    end

    def rb_fsevent?
      begin
        require 'rb-fsevent'
        defined?(FSEvent::VERSION) ? FSEvent::VERSION.to_f >= 0.4 : false
      rescue LoadError
        false
      end
    end

    def rb_inotify?
      begin
        require 'rb-inotify'
        if defined?(INotify::VERSION)
          version = INotify::VERSION
          version[0] > 0 || version[1] >= 6
        end
      rescue LoadError
        false
      end
    end

    def use_block(context, block)
      return if block.nil?
      if block.arity == 1
        block.call(context)
      else
        context.instance_eval(&block)
      end
    end

  end
end