Class: Embulk::Input::GoogleSpreadsheets::RecordTypecaster

Inherits:
Object
  • Object
show all
Defined in:
lib/embulk/input/google_spreadsheets/record_typecaster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task) ⇒ RecordTypecaster

Returns a new instance of RecordTypecaster.



11
12
13
14
# File 'lib/embulk/input/google_spreadsheets/record_typecaster.rb', line 11

def initialize(task)
  @column_names = task['columns'].map{|c| c['name']}
  @column_details = configure_column_details(task)
end

Instance Attribute Details

#column_detailsObject (readonly)

Returns the value of attribute column_details.



9
10
11
# File 'lib/embulk/input/google_spreadsheets/record_typecaster.rb', line 9

def column_details
  @column_details
end

#column_namesObject (readonly)

Returns the value of attribute column_names.



9
10
11
# File 'lib/embulk/input/google_spreadsheets/record_typecaster.rb', line 9

def column_names
  @column_names
end

Instance Method Details

#configure_column_details(task) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/embulk/input/google_spreadsheets/record_typecaster.rb', line 16

def configure_column_details(task)
  _column_details = task['columns'].dup.each_with_index.inject({}) do |details, column_with_index|
    c, i = *column_with_index
    details.tap do |ds|
      ds[c['name']] = {}.tap do |d|
        d['index'] = i
        d['name'] = c['name']
        d['type'] = c['type'].to_sym
        d['format'] = c['format']
        d['timezone'] = c['timezone']
        d['typecast'] = TypecastFactory.create(c['typecast'], task)
      end
    end
  end

  logger.debug { "`embulk-input-google_spreadsheets`: configured column details '#{_column_details.to_json}'"}
  _column_details
end

#loggerObject



35
36
37
# File 'lib/embulk/input/google_spreadsheets/record_typecaster.rb', line 35

def logger
  GoogleSpreadsheets.logger
end

#transform_by_columns(record) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/embulk/input/google_spreadsheets/record_typecaster.rb', line 39

def transform_by_columns(record)
  column_names.map do |n|
    d = column_details[n]
    typecast = d['typecast']
    value = record[d['index']]
    type = d['type']

    begin
      case type
      when :string
        typecast.as_string(value)
      when :long
        typecast.as_long(value)
      when :double
        typecast.as_double(value)
      when :boolean
        typecast.as_boolean(value)
      when :timestamp
        typecast.as_timestamp(value, d['format'], d['timezone'])
      when :json
        typecast.as_json(value)
      else
        raise ConfigError.new("`google_spreadsheets`: Unsupported type `#{type}`")
      end
    rescue => e
      # for adding column information
      raise TypecastError.new(e, ", column: #{n}, column_detail: #{d.to_json}")
    end
  end
end