Class: Burner::Library::Collection::Number

Inherits:
JobWithRegister show all
Defined in:
lib/burner/library/collection/number.rb

Overview

This job can iterate over a set of records and sequence them (set the specified key to a sequential index value.)

Expected Payload input: array of objects. Payload output: array of objects.

Constant Summary collapse

BLANK =
''
DEFAULT_KEY =
'number'
DEFAULT_START_AT =
1

Instance Attribute Summary collapse

Attributes inherited from JobWithRegister

#register

Attributes inherited from Job

#name

Instance Method Summary collapse

Methods included from Util::Arrayable

#array

Constructor Details

#initialize(key: DEFAULT_KEY, name: BLANK, register: Burner::DEFAULT_REGISTER, separator: BLANK, start_at: DEFAULT_START_AT) ⇒ Number

Returns a new instance of Number.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/burner/library/collection/number.rb', line 25

def initialize(
  key: DEFAULT_KEY,
  name: BLANK,
  register: Burner::DEFAULT_REGISTER,
  separator: BLANK,
  start_at: DEFAULT_START_AT
)
  super(name: name, register: register)

  @key      = key.to_s
  @resolver = Objectable.resolver(separator: separator)
  @start_at = start_at.to_i

  freeze
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



23
24
25
# File 'lib/burner/library/collection/number.rb', line 23

def key
  @key
end

#resolverObject (readonly)

Returns the value of attribute resolver.



23
24
25
# File 'lib/burner/library/collection/number.rb', line 23

def resolver
  @resolver
end

#start_atObject (readonly)

Returns the value of attribute start_at.



23
24
25
# File 'lib/burner/library/collection/number.rb', line 23

def start_at
  @start_at
end

Instance Method Details

#perform(output, payload) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/burner/library/collection/number.rb', line 41

def perform(output, payload)
  output.detail("Setting '#{key}' for each record with values starting at #{start_at}")

  ensure_array(payload).each.with_index(start_at) do |record, index|
    resolver.set(record, key, index)
  end
end