Class: Kiba::Extend::Utils::Fieldset

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/utils/fieldset.rb

Overview

Data structure class used in processing merge transforms

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields:, null_placeholder: nil) ⇒ Fieldset

Returns a new instance of Fieldset.



10
11
12
13
14
# File 'lib/kiba/extend/utils/fieldset.rb', line 10

def initialize(fields:, null_placeholder: nil)
  @null_placeholder = null_placeholder
  @hash = {}
  fields.each { |field| hash[field] = [] }
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



8
9
10
# File 'lib/kiba/extend/utils/fieldset.rb', line 8

def hash
  @hash
end

Instance Method Details

#add_constant_values(field, value) ⇒ Object



16
17
18
19
# File 'lib/kiba/extend/utils/fieldset.rb', line 16

def add_constant_values(field, value)
  hash[field] = []
  value_ct.times { hash[field] << value }
end

#fieldsObject



21
22
23
# File 'lib/kiba/extend/utils/fieldset.rb', line 21

def fields
  hash.keys
end

#join_values(delim) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kiba/extend/utils/fieldset.rb', line 25

def join_values(delim)
  hash.transform_values! do |vals|
    placeholdered = if null_placeholder
      vals.map do |val|
        val.nil? ? null_placeholder : val
      end
    else
      vals
    end
    placeholdered.join(delim)
  end
end

#populate(rows) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/kiba/extend/utils/fieldset.rb', line 38

def populate(rows)
  return self if rows.blank?

  rows.each { |row| get_field_values(row) }
  remove_valueless_rows
  self
end

#value_ctObject



46
47
48
# File 'lib/kiba/extend/utils/fieldset.rb', line 46

def value_ct
  hash.values.first.length
end