Class: Mushy::Collection

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

Instance Attribute Summary collapse

Attributes inherited from Flux

#config, #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, inherited, #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

#initializeCollection

Returns a new instance of Collection.



27
28
29
30
# File 'lib/mushy/fluxs/collection.rb', line 27

def initialize
  self.collection = {}
  super
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



5
6
7
# File 'lib/mushy/fluxs/collection.rb', line 5

def collection
  @collection
end

Class Method Details

.detailsObject



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

def self.details
  {
    name: 'Collection',
    description: 'Collects events.',
    config: {
      id: {
            description: 'The path to the unique id in the body of the element.',
            type: 'id',
            value: { url: 'a|@href' },
          },
      operation: {
                   description: 'Perform this operation.',
                   type:        'select',
                   options:     ['all', 'delete', 'upsert', 'update', 'insert'],
                   value:       'upsert',
                 },
    },
  }
end

Instance Method Details

#all(event, config) ⇒ Object



40
41
42
# File 'lib/mushy/fluxs/collection.rb', line 40

def all event, config
  self.collection.values
end

#delete(event, config) ⇒ Object



44
45
46
47
48
# File 'lib/mushy/fluxs/collection.rb', line 44

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

#get_the_id(event, config) ⇒ Object



36
37
38
# File 'lib/mushy/fluxs/collection.rb', line 36

def get_the_id event, config
  event[config[:id]]
end

#insert(event, config) ⇒ Object



65
66
67
68
69
# File 'lib/mushy/fluxs/collection.rb', line 65

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

#process(event, config) ⇒ Object



32
33
34
# File 'lib/mushy/fluxs/collection.rb', line 32

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

#update(event, config) ⇒ Object



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

def update event, config
  item = self.collection[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



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

def upsert event, config
  if self.collection[get_the_id(event, config)]
    update event, config
  else
    insert event, config
  end
end