Class: Mushy::Collection

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



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mushy/fluxs/collection.rb', line 5

def self.details
  {
    name: 'Collection',
    description: 'Collects events.',
    config: {
      id: {
            description: 'The path to the unique id in the body of the element.',
            type: 'text',
            value: '{{id}}',
          },
      collection_name: {
                         description: 'The name of the collection to interact with.',
                         type:        'text',
                         value:       'records',
                       },
      operation: {
                   description: 'Perform this operation.',
                   type:        'select',
                   options:     ['all', 'delete', 'upsert', 'update', 'insert'],
                   value:       'upsert',
                 },
    },
  }
end

.guard_the_flow(flow) ⇒ Object



82
83
84
85
86
87
# File 'lib/mushy/fluxs/collection.rb', line 82

def self.guard_the_flow flow
  return if flow.respond_to?(:collection_data)

  flow.instance_eval { class << self; self end }.send(:attr_accessor, :collection_data)
  flow.collection_data = {}
end

Instance Method Details

#all(event, config) ⇒ Object



38
39
40
# File 'lib/mushy/fluxs/collection.rb', line 38

def all event, config
  the_collection(config).values
end

#delete(event, config) ⇒ Object



42
43
44
45
46
# File 'lib/mushy/fluxs/collection.rb', line 42

def delete event, config
  the_collection(config).delete get_the_id(event, config)
  event[config[:operation_performed]] = 'deleted' if config[:operation_performed]
  event
end

#get_the_collection(name) ⇒ Object



76
77
78
79
80
# File 'lib/mushy/fluxs/collection.rb', line 76

def get_the_collection name
  found_collection = self.flow.collection_data[name]
  return found_collection if found_collection
  self.flow.collection_data[name] = SymbolizedHash.new
end

#get_the_id(event, config) ⇒ Object



34
35
36
# File 'lib/mushy/fluxs/collection.rb', line 34

def get_the_id event, config
  config[:id]
end

#insert(event, config) ⇒ Object



63
64
65
66
67
# File 'lib/mushy/fluxs/collection.rb', line 63

def insert event, config
  the_collection(config)[get_the_id(event, config)] = event
  event[config[:operation_performed]] = 'inserted' if config[:operation_performed]
  event
end

#process(event, config) ⇒ Object



30
31
32
# File 'lib/mushy/fluxs/collection.rb', line 30

def process event, config
  self.send(config[:operation].to_sym, event, config)
end

#the_collection(config) ⇒ Object



69
70
71
72
73
74
# File 'lib/mushy/fluxs/collection.rb', line 69

def the_collection config
  Mushy::Collection.guard_the_flow self.flow
  the_collection_name = config[:collection_name]

  get_the_collection the_collection_name
end

#update(event, config) ⇒ Object



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

def update event, config
  item = the_collection(config)[get_the_id(event, config)]
  event.each { |k, v| item[k] = v } if item
  event[config[:operation_performed]] = (item ? 'updated' : 'not exist') if config[:operation_performed]
  event
end

#upsert(event, config) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/mushy/fluxs/collection.rb', line 48

def upsert event, config
  if the_collection(config)[get_the_id(event, config)]
    update event, config
  else
    insert event, config
  end
end