Class: Rulebow::FileFact

Inherits:
Fact
  • Object
show all
Defined in:
lib/rulebow/fact.rb

Overview

This subclass of Fact is specialized for file change conditions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Fact

#&, #set, #|

Constructor Details

#initialize(pattern, &coerce) ⇒ FileFact

Initialize new instance of FileFact.

Parameters:

  • pattern

    File glob or regular expression. [String,Regexp]

  • digest

    The system digest. [Digest]



56
57
58
59
# File 'lib/rulebow/fact.rb', line 56

def initialize(pattern, &coerce)
  @pattern = pattern
  @coerce  = coerce
end

Instance Attribute Details

#coerceObject (readonly)

Returns the value of attribute coerce.



65
66
67
# File 'lib/rulebow/fact.rb', line 65

def coerce
  @coerce
end

#patternObject (readonly)

File glob or regular expression.



62
63
64
# File 'lib/rulebow/fact.rb', line 62

def pattern
  @pattern
end

Instance Method Details

#call(digest) ⇒ Object

Process logic.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rulebow/fact.rb', line 68

def call(digest)
  result = []

  case pattern
  when Regexp
    list = Dir.glob('**/*', File::FNM_PATHNAME)
    list = digest.filter(list)  # excludes ignored files (odd way to do it but if should work)
    list.each do |fname|
      if md = pattern.match(fname)
        if digest.current[fname] != digest.saved[fname]
          result << Match.new(fname, md)
        end
      end
    end
    # NOTE: The problem with using the digest list, is that if a rule
    #       adds a new file to the project, then a subsequent rule needs
    #       to be able to see it.
    #@digest.current.keys.each do |fname|
    #  if md = pattern.match(fname)
    #    if @digest.current[fname] != @digest.saved[fname]
    #      result << Match.new(fname, md)
    #    end
    #  end
    #end
  else
    list = Dir.glob(pattern, File::FNM_PATHNAME)
    list = digest.filter(list)   # excludes ignored files (odd way to do it but if should work)
    list.each do |fname|
      if digest.current[fname] != digest.saved[fname]
        result << fname
      end
    end
    #@digest.current.keys.each do |fname|
    #  if md = File.fnmatch?(pattern, fname, File::FNM_PATHNAME | File::FNM_EXTGLOB)
    #    if @digest.current[fname] != @digest.saved[fname]
    #      result << Match.new(fname, md)
    #    end
    #  end
    #end
  end

  if coerce
    return [coerce.call(result)].flatten.compact
  else
    return result
  end
end