csv_row_model 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: afc7a06355cb7f9aa742c357c75b9a99a13ed8a9
4
- data.tar.gz: b07b9d35bb0b984e61adaa8790b1d43179285d3d
3
+ metadata.gz: b6df2d90030bb50e17423d8be4fe83d1ebd7b10f
4
+ data.tar.gz: 488cf4953e84be182919da7c8f71abccaa601dbb
5
5
  SHA512:
6
- metadata.gz: f806cc42c9e551555540d770333ce719adc2614eae82b7fe0246bd2f4f4c66b119c89cebe5522286e02e8dfddd0d760f0e3360f714d9d318b9019074c43825bc
7
- data.tar.gz: b1d2cbf9c9cf4a9c0e0557d41913f79e8b2165a74fe2310651d0c999f9be4290b96ab22cb7c67b66aaaccfd75a3e572ac67723b112d8954ae98c8b8217e37778
6
+ metadata.gz: de36927a51089fbbed5b182c55d982175a44eef1cc68fa052da35588822adba8518651100b8b9222b35be1f1b9ef626be855c18c715d0729528c5ce28eb0232c
7
+ data.tar.gz: 88ecafabf8d4757814d556aadbcaa2ecd90650994357b40734a559f4ca79cc49d00088fe7e273cb3accd710d96829dc291de253b88294dff072a970ee03549cf
data/README.md CHANGED
@@ -124,7 +124,7 @@ def original_attribute(column_name)
124
124
  value = mapped_row[column_name]
125
125
 
126
126
  # 2. Clean or format each cell
127
- value = self.class.format_cell(cell, column_name, column_index)
127
+ value = self.class.format_cell(cell, column_name, column_index, context)
128
128
 
129
129
  if value.present?
130
130
  # 3a. Parse the cell value (which does nothing if no parsing is specified)
@@ -146,7 +146,7 @@ Override the `format_cell` method to clean/format every cell:
146
146
  class ProjectImportRowModel < ProjectRowModel
147
147
  include CsvRowModel::Import
148
148
  class << self
149
- def format_cell(cell, column_name, column_index)
149
+ def format_cell(cell, column_name, column_index, context={})
150
150
  cell = cell.strip
151
151
  cell.blank? ? nil : cell
152
152
  end
@@ -7,7 +7,7 @@ module CsvRowModel
7
7
  self.column_names.each { |*args| define_attribute_method(*args) }
8
8
  end
9
9
 
10
- # @return [Hash] a map of `column_name => self.class.format_cell()public_send(column_name))`
10
+ # @return [Hash] a map of `column_name => self.class.format_cell(public_send(column_name))`
11
11
  def formatted_attributes
12
12
  formatted_attributes_from_column_names self.class.column_names
13
13
  end
@@ -16,7 +16,8 @@ module CsvRowModel
16
16
  self.class.format_cell(
17
17
  public_send(column_name),
18
18
  column_name,
19
- self.class.index(column_name)
19
+ self.class.index(column_name),
20
+ context
20
21
  )
21
22
  end
22
23
 
@@ -43,7 +44,7 @@ module CsvRowModel
43
44
  # Safe to override. Method applied to each cell by default
44
45
  #
45
46
  # @param cell [Object] the cell's value
46
- def format_cell(cell, column_name, column_index)
47
+ def format_cell(cell, column_name, column_index, context={})
47
48
  cell
48
49
  end
49
50
  end
@@ -34,7 +34,8 @@ module CsvRowModel
34
34
  self.class.format_cell(
35
35
  public_send(self.class.singular_dynamic_attribute_method_name(column_name), header_model),
36
36
  column_name,
37
- self.class.dynamic_index(column_name)
37
+ self.class.dynamic_index(column_name),
38
+ context
38
39
  )
39
40
  end
40
41
  end
@@ -11,7 +11,7 @@ module CsvRowModel
11
11
  row.each do |cell|
12
12
  if self.class.is_row_name? cell
13
13
  header_matchs = self.class.options(cell)[:header_matchs]
14
- result << "#{header_matchs ? header_matchs.first : self.class.format_header(cell)}"
14
+ result << "#{header_matchs ? header_matchs.first : self.class.format_header(cell, context)}"
15
15
  result << "#{attributes[cell]}"
16
16
  else
17
17
  result << cell.to_s
@@ -31,7 +31,7 @@ module CsvRowModel
31
31
  csv_string_model.valid?
32
32
  return nil unless csv_string_model.errors[column_name].blank?
33
33
 
34
- value = self.class.format_cell(mapped_row[column_name], column_name, self.class.index(column_name))
34
+ value = self.class.format_cell(mapped_row[column_name], column_name, self.class.index(column_name), context)
35
35
  if value.present?
36
36
  value = instance_exec(value, &self.class.parse_lambda(column_name))
37
37
  elsif self.class.options(column_name)[:default]
@@ -61,7 +61,7 @@ module CsvRowModel
61
61
  # @param cell [String] the cell's string
62
62
  # @param column_name [Symbol] the cell's column_name
63
63
  # @param column_index [Integer] the column_name's index
64
- def format_cell(cell, column_name, column_index)
64
+ def format_cell(cell, column_name, column_index, context={})
65
65
  cell
66
66
  end
67
67
 
@@ -34,7 +34,8 @@ module CsvRowModel
34
34
  value = self.class.format_cell(
35
35
  dynamic_source_row[index],
36
36
  source_header,
37
- self.class.dynamic_index(column_name)
37
+ self.class.dynamic_index(column_name),
38
+ context
38
39
  )
39
40
  public_send(self.class.singular_dynamic_attribute_method_name(column_name), value, source_header)
40
41
  end
@@ -58,7 +58,7 @@ module CsvRowModel
58
58
  column_names = self.class.column_names
59
59
  hash = column_names.zip(
60
60
  column_names.map.with_index do |column_name, index|
61
- self.class.format_cell(source_row[index], column_name, index)
61
+ self.class.format_cell(source_row[index], column_name, index, context)
62
62
  end
63
63
  ).to_h
64
64
  else
@@ -1,3 +1,3 @@
1
1
  module CsvRowModel
2
- VERSION = "0.3.3"
2
+ VERSION = "0.3.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv_row_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Chung
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-07 00:00:00.000000000 Z
11
+ date: 2015-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel