Class: Mushy::RegexMatches

Inherits:
Flux
  • Object
show all
Defined in:
lib/mushy/fluxs/regex_matches.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



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

def self.details
  {
    name: 'RegexMatches',
    title: 'Find regex matches',
    description: 'Use a regex to search content.',
    fluxGroup: { name: 'Regex' },
    config: {
      regex: { description: 'The regular expression to use.',
               type: 'text',
               value: '(\w+)' },
      value: { description: 'The value against which to use the regular expression.',
               type: 'text',
               value: '{{value}}' }
    },
    examples: {
      'Simple Example' => {
                            description: 'The simplest regex.',
                            input: { text: 'apple orange' },
                            config: {
                              regex: '(\w+)',
                              value: 'apple orange'
                            },
                            result: [ { match1: 'apple' }, { match2: 'orange' } ]
                          },
      'Named Parameters' => {
                     description: 'Named Parameters.',
                     input: {
                              text: 'apple 1 orange 2'
                            },
                     config: {
                               regex: '(?<name>\w+) (?<count>\d+)',
                               value: '{{text}}'
                             },
                     result: [ { name: 'apple', count: '1' }, { name: 'orange', count: '2' } ]
                   },
              }
}
end

Instance Method Details

#convert_the_match_to_a_hash(match, keys) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/mushy/fluxs/regex_matches.rb', line 55

def convert_the_match_to_a_hash(match, keys)
  {}.tap do |hash|
    match.each_with_index do |item, index|
      key = (keys[index] || "match#{index + 1}").to_sym
      hash[key] = item
    end
  end
end

#process(_, config) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mushy/fluxs/regex_matches.rb', line 42

def process(_, config)
  return [] unless config[:value]
  return [] if (config[:regex] || '').strip == ''

  keys = config[:regex].scan(/\?<(\w+)>/).flatten

  regex = Regexp.new config[:regex]

  config[:value].scan(regex).map do |match|
    convert_the_match_to_a_hash match, keys
  end
end