Class: Mushy::FileWatch

Inherits:
Flux
  • Object
show all
Defined in:
lib/mushy/fluxs/file_watch.rb

Instance Attribute Summary

Attributes inherited from Flux

#config, #flow, #id, #masher, #parent_fluxs, #subscribed_to, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Flux

#convert_this_to_an_array, #convert_to_symbolized_hash, #execute, #execute_single_event, #group_these_results, #guard, #ignore_these_results, inherited, #initialize, #join_these_results, #limit_these_results, #merge_these_results, #model_these_results, #outgoing_split_these_results, #shape_these, #sort_these_results, #standardize_these

Constructor Details

This class inherits a constructor from Mushy::Flux

Class Method Details

.detailsObject



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
# File 'lib/mushy/fluxs/file_watch.rb', line 4

def self.details
  {
    name: 'FileWatch',
    title: 'Start a flow when files change',
    fluxGroup: { name: 'Starters', position: 0 },
    description: 'Watch for file changes.',
    config: {
      directory: {
        description: 'The directory to watch, defaults to the current directory.',
        type: 'text',
        shrink: true,
        value: ''
      },
      include_all_file_details: {
        description: 'If true, returns all details for the file. If false, just path & name are returned.',
        type: 'boolean',
        shrink: true,
        value: ''
      },
      merge_all_file_changes: {
        description: 'If true, all changes are merged into one "files". If false, added/modified/removed are returned separately.',
        type: 'boolean',
        shrink: true,
        value: ''
      },
      include_added: {
        description: 'Include added fields, defaults to true.',
        type: 'boolean',
        shrink: true,
        value: ''
      },
      include_modified: {
        description: 'Include modified fields, defaults to true.',
        type: 'boolean',
        shrink: true,
        value: ''
      },
      include_removed: {
        description: 'Include removed fields, defaults to true.',
        type: 'boolean',
        shrink: true,
        value: ''
      }
    },
    examples: {
      'Files Added' => {
        description: 'When a file is added, this type of result will be returned.',
        result: {
          modified: [],
          added: [{ path: '/home/pi/Desktop/mushy/bin/hey.txt', directory: '/home/pi/Desktop/mushy/bin', name: 'hey.txt' }],
          removed: []
        }
      },
      'Files Removed' => {
        description: 'When a file is deleted, this type of result will be returned.',
        result: {
          modified: [],
          added: [],
          removed: [{ path: '/home/pi/Desktop/mushy/mushy-0.15.3.gem', directory: '/home/pi/Desktop/mushy', name: 'mushy-0.15.3.gem'}]
        }
      },
      'Files Modified' => {
        description: 'When a file is modified, this type of result will be returned.',
        result: {
          modified: [{ path: '/home/pi/Desktop/mushy/lib/mushy/fluxs/environment.rb', directory: '/home/pi/Desktop/mushy/lib/mushy/fluxs/environment.rb', name: 'environment.rb' }],
          added: [],
          removed: []
        }
      }
    }
  }
end

Instance Method Details

#convert_changes_to_event(modified, added, removed, config) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mushy/fluxs/file_watch.rb', line 100

def convert_changes_to_event(modified, added, removed, config)
  modified = [] if config[:include_modified].to_s == 'false'
  added = [] if config[:include_added].to_s == 'false'
  removed = [] if config[:include_removed].to_s == 'false'

  result = {
    modified: modified.map { |f| get_the_details_for(f, config) },
    added: added.map { |f| get_the_details_for(f, config) },
    removed: removed.map { |f| get_the_details_for(f, config) }
  }

  return result unless config[:merge_all_file_changes].to_s == 'true'

  {
    files: [:added, :modified, :removed].map { |x| result[x] }.flatten
  }
end

#get_the_details_for(file, config) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/mushy/fluxs/file_watch.rb', line 118

def get_the_details_for(file, config)
  if config[:include_all_file_details].to_s == 'true'
    Mushy::Ls.new.process({}, { path: file })[0]
  else
    segments = file.split("\/")
    {
      path: file,
      name: segments.pop,
      directory: segments.join("\/")
    }
  end
end

#loop(&block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/mushy/fluxs/file_watch.rb', line 77

def loop(&block)
  directory = config[:directory].to_s != '' ? config[:directory] : Dir.pwd

  listener = Listen.to(directory) do |modified, added, removed|
    block.call convert_changes_to_event(modified, added, removed, config)
  end

  listener.start

  sleep
end

#process(event, _) ⇒ Object



89
90
91
# File 'lib/mushy/fluxs/file_watch.rb', line 89

def process(event, _)
  event
end

#test(_, config) ⇒ Object



93
94
95
96
97
98
# File 'lib/mushy/fluxs/file_watch.rb', line 93

def test(_, config)
  modified = [Mushy::Ls.new.process({}, {}).select { |x| x[:type] == '-' }.sample]
             .compact
             .map { |x| x[:path] }
  convert_changes_to_event(modified, [], [], config)
end