module FSEvent::Util

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/>.

Public Instance Methods

nested_hash(n) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/fsevent/util.rb, line 63
def valid_status_name_pat_for_read?(str)
  /\A_?(?:[a-z][a-z0-9_]*)?\*?\z/ =~ str
end