Class: MultipleMan::PayloadGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/multiple_man/payload_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, operation = :create, options = {}) ⇒ PayloadGenerator

Returns a new instance of PayloadGenerator.



3
4
5
6
7
# File 'lib/multiple_man/payload_generator.rb', line 3

def initialize(record, operation = :create, options = {})
  self.record = record
  self.operation = operation.to_s
  self.options = options
end

Instance Attribute Details

#operationObject

Returns the value of attribute operation.



34
35
36
# File 'lib/multiple_man/payload_generator.rb', line 34

def operation
  @operation
end

Instance Method Details

#dataObject



26
27
28
29
30
31
32
# File 'lib/multiple_man/payload_generator.rb', line 26

def data
  if options[:with]
    options[:with].new(record).as_json
  else
    AttributeExtractor.new(record, options[:fields]).as_json
  end
end

#idObject



22
23
24
# File 'lib/multiple_man/payload_generator.rb', line 22

def id
  Identity.build(record, options).value
end

#payloadObject



9
10
11
12
13
14
15
16
# File 'lib/multiple_man/payload_generator.rb', line 9

def payload
  {
    type: type,
    operation: operation,
    id: id,
    data: data
  }.to_json
end

#typeObject



18
19
20
# File 'lib/multiple_man/payload_generator.rb', line 18

def type
  options[:as] || record.class.name
end