Module: FSEvent::Util

Included in:
FSEvent, WatchSet
Defined in:
lib/fsevent/util.rb

Overview

util.rb — various utilities

Copyright © 2014 National Institute of Advanced Industrial Science and Technology (AIST)

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>.

Class Method Summary collapse

Class Method Details

.nested_hash(n) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/fsevent/util.rb', line 21

def nested_hash(n)
  if n == 1
    {}
  else
    Hash.new {|h, k|
      h[k] = nested_hash(n-1)
    }
  end
end

.nonempty_hash(h, level) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fsevent/util.rb', line 31

def nonempty_hash(h, level)
  return nil if h.nil?
  result = {}
  h.each {|k, v|
    if level == 1
      result[k] = v
    else
      h2 = nonempty_hash(v, level-1)
      if h2
        result[k] = h2
      end
    end
  }
  if result.empty?
    nil
  else
    result
  end
end

.prefixpat_match(pat, str) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/fsevent/util.rb', line 75

def prefixpat_match(pat, str)
  if /\*\z/ =~ pat
    str.start_with?($`)
  else
    pat == str
  end
end

.reaction_immediate_at_beginning?(reaction) ⇒ Boolean



83
84
85
86
87
88
89
90
91
92
# File 'lib/fsevent/util.rb', line 83

def reaction_immediate_at_beginning?(reaction)
  case reaction
  when :immediate
    true
  when :schedule
    false
  else
    raise ArgumentError, "unexpected reaction: #{reaction.inspect}"
  end
end

.reaction_immediate_at_subsequent?(reaction) ⇒ Boolean



94
95
96
97
98
99
100
101
102
103
# File 'lib/fsevent/util.rb', line 94

def reaction_immediate_at_subsequent?(reaction)
  case reaction
  when :immediate
    true
  when :schedule
    false
  else
    raise ArgumentError, "unexpected reaction: #{reaction.inspect}"
  end
end

.valid_device_name_for_read?(str) ⇒ Boolean



51
52
53
# File 'lib/fsevent/util.rb', line 51

def valid_device_name_for_read?(str)
  /\A_?[a-z][a-z0-9_]*\z/ =~ str
end

.valid_device_name_for_write?(str) ⇒ Boolean



67
68
69
# File 'lib/fsevent/util.rb', line 67

def valid_device_name_for_write?(str)
  /\A[a-z][a-z0-9_]*\z/ =~ str
end

.valid_device_name_pat_for_read?(str) ⇒ Boolean



55
56
57
# File 'lib/fsevent/util.rb', line 55

def valid_device_name_pat_for_read?(str)
  /\A_?(?:[a-z][a-z0-9_]*)?\*?\z/ =~ str
end

.valid_status_name_for_read?(str) ⇒ Boolean



59
60
61
# File 'lib/fsevent/util.rb', line 59

def valid_status_name_for_read?(str)
  /\A_?[a-z][a-z0-9_]*\z/ =~ str
end

.valid_status_name_for_write?(str) ⇒ Boolean



71
72
73
# File 'lib/fsevent/util.rb', line 71

def valid_status_name_for_write?(str)
  /\A[a-z][a-z0-9_]*\z/ =~ str
end

.valid_status_name_pat_for_read?(str) ⇒ Boolean



63
64
65
# File 'lib/fsevent/util.rb', line 63

def valid_status_name_pat_for_read?(str)
  /\A_?(?:[a-z][a-z0-9_]*)?\*?\z/ =~ str
end