Class: Ergo::FileState

Inherits:
State
  • Object
show all
Defined in:
lib/ergo/state.rb

Overview

This subclass of State is specialized for file change conditions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from State

#&, #set, #|

Constructor Details

#initialize(pattern) ⇒ FileState

Initialize new instance of FileState.

Parameters:

  • pattern

    File glob or regular expression. [String,Regexp]

  • digest

    The system digest. [Digest]



54
55
56
57
# File 'lib/ergo/state.rb', line 54

def initialize(pattern) #, digest)
  @pattern = pattern
  #@digest  = digest
end

Instance Attribute Details

#patternObject (readonly)

File glob or regular expression.



60
61
62
# File 'lib/ergo/state.rb', line 60

def pattern
  @pattern
end

Instance Method Details

#call(digest) ⇒ Object

Process logic.



66
67
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
# File 'lib/ergo/state.rb', line 66

def call(digest)
  result = []

  case pattern
  when Regexp
    list = Dir.glob('**/*', File::FNM_PATHNAME)
    list = digest.filter(list)  # apply ignore
    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)
    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

  return result
end