csv_row_model 1.0.0.beta1 → 1.0.0.beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +2 -1
  3. data/README.md +22 -9
  4. data/lib/csv_row_model/concerns/attributes_base.rb +86 -0
  5. data/lib/csv_row_model/concerns/check_options.rb +7 -9
  6. data/lib/csv_row_model/concerns/dynamic_columns_base.rb +47 -0
  7. data/lib/csv_row_model/concerns/export/attributes.rb +26 -0
  8. data/lib/csv_row_model/{export → concerns/export}/base.rb +1 -1
  9. data/lib/csv_row_model/concerns/export/dynamic_columns.rb +33 -0
  10. data/lib/csv_row_model/concerns/hidden_module.rb +15 -0
  11. data/lib/csv_row_model/concerns/import/attributes.rb +52 -0
  12. data/lib/csv_row_model/{import → concerns/import}/base.rb +10 -18
  13. data/lib/csv_row_model/{import → concerns/import}/csv_string_model.rb +2 -3
  14. data/lib/csv_row_model/concerns/import/dynamic_columns.rb +50 -0
  15. data/lib/csv_row_model/{import → concerns/import}/represents.rb +2 -2
  16. data/lib/csv_row_model/concerns/inspect.rb +5 -6
  17. data/lib/csv_row_model/{model/columns.rb → concerns/model/attributes.rb} +8 -31
  18. data/lib/csv_row_model/{model → concerns/model}/base.rb +1 -11
  19. data/lib/csv_row_model/{model → concerns/model}/children.rb +7 -1
  20. data/lib/csv_row_model/{model → concerns/model}/dynamic_columns.rb +23 -42
  21. data/lib/csv_row_model/export/file_model.rb +2 -15
  22. data/lib/csv_row_model/export.rb +3 -3
  23. data/lib/csv_row_model/import/file.rb +62 -22
  24. data/lib/csv_row_model/import/file_model.rb +3 -4
  25. data/lib/csv_row_model/import.rb +5 -9
  26. data/lib/csv_row_model/internal/attribute_base.rb +22 -0
  27. data/lib/csv_row_model/internal/concerns/column_shared.rb +21 -0
  28. data/lib/csv_row_model/internal/concerns/dynamic_column_shared.rb +28 -0
  29. data/lib/csv_row_model/internal/dynamic_column_attribute_base.rb +42 -0
  30. data/lib/csv_row_model/internal/export/attribute.rb +15 -0
  31. data/lib/csv_row_model/internal/export/dynamic_column_attribute.rb +21 -0
  32. data/lib/csv_row_model/{import/cell.rb → internal/import/attribute.rb} +4 -13
  33. data/lib/csv_row_model/{import → internal/import}/csv.rb +11 -13
  34. data/lib/csv_row_model/internal/import/dynamic_column_attribute.rb +33 -0
  35. data/lib/csv_row_model/{import → internal/import}/representation.rb +2 -2
  36. data/lib/csv_row_model/internal/model/dynamic_column_header.rb +22 -0
  37. data/lib/csv_row_model/internal/model/header.rb +25 -0
  38. data/lib/csv_row_model/model/file_model.rb +0 -1
  39. data/lib/csv_row_model/model.rb +5 -12
  40. data/lib/csv_row_model/validators/{boolean_format.rb → boolean_format_validator.rb} +1 -1
  41. data/lib/csv_row_model/validators/date_format_validator.rb +7 -0
  42. data/lib/csv_row_model/validators/date_time_format_validator.rb +7 -0
  43. data/lib/csv_row_model/validators/{default_change.rb → default_change_validator.rb} +0 -0
  44. data/lib/csv_row_model/validators/float_format_validator.rb +7 -0
  45. data/lib/csv_row_model/validators/integer_format_validator.rb +15 -0
  46. data/lib/csv_row_model/version.rb +1 -1
  47. data/lib/csv_row_model.rb +16 -37
  48. metadata +35 -33
  49. data/lib/csv_row_model/engine.rb +0 -5
  50. data/lib/csv_row_model/export/attributes.rb +0 -46
  51. data/lib/csv_row_model/export/cell.rb +0 -24
  52. data/lib/csv_row_model/export/dynamic_column_cell.rb +0 -29
  53. data/lib/csv_row_model/export/dynamic_columns.rb +0 -46
  54. data/lib/csv_row_model/import/attributes.rb +0 -66
  55. data/lib/csv_row_model/import/dynamic_column_cell.rb +0 -37
  56. data/lib/csv_row_model/import/dynamic_columns.rb +0 -59
  57. data/lib/csv_row_model/import/file/callbacks.rb +0 -17
  58. data/lib/csv_row_model/import/file/validations.rb +0 -43
  59. data/lib/csv_row_model/model/comparison.rb +0 -15
  60. data/lib/csv_row_model/model/dynamic_column_cell.rb +0 -44
  61. data/lib/csv_row_model/validators/date_format.rb +0 -9
  62. data/lib/csv_row_model/validators/date_time_format.rb +0 -9
  63. data/lib/csv_row_model/validators/float_format.rb +0 -9
  64. data/lib/csv_row_model/validators/integer_format.rb +0 -9
  65. data/lib/csv_row_model/validators/number_validator.rb +0 -16
  66. data/lib/csv_row_model/validators/validate_attributes.rb +0 -27
@@ -0,0 +1,22 @@
1
+ require 'csv_row_model/internal/model/header'
2
+ require 'csv_row_model/internal/concerns/dynamic_column_shared'
3
+
4
+ module CsvRowModel
5
+ module Model
6
+ class DynamicColumnHeader < Header
7
+ include DynamicColumnShared
8
+
9
+ def value
10
+ header_models.map { |header_model| header_proc.call(header_model) }
11
+ end
12
+
13
+ def header_proc
14
+ options[:header] || ->(header_model) { format_header(header_model) }
15
+ end
16
+
17
+ def format_header(header_model)
18
+ row_model_class.format_dynamic_column_header(header_model, column_name, column_index, context)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ require 'csv_row_model/internal/concerns/column_shared'
2
+
3
+ module CsvRowModel
4
+ module Model
5
+ class Header
6
+ include ColumnShared
7
+
8
+ attr_reader :column_name, :row_model_class, :context
9
+
10
+ def initialize(column_name, row_model_class, context)
11
+ @column_name = column_name
12
+ @row_model_class = row_model_class
13
+ @context = OpenStruct.new(context)
14
+ end
15
+
16
+ def value
17
+ options[:header] || formatted_header
18
+ end
19
+
20
+ def formatted_header
21
+ row_model_class.format_header(column_name, column_index, context)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -8,7 +8,6 @@ module CsvRowModel
8
8
  alias_method :row_names, :column_names
9
9
  alias_method :rows, :columns
10
10
  alias_method :row, :column
11
- alias_method :is_row_name?, :is_column_name?
12
11
  end
13
12
  end
14
13
  end
@@ -1,25 +1,18 @@
1
- require 'csv_row_model/model/base'
2
- require 'csv_row_model/model/columns'
3
- require 'csv_row_model/model/children'
4
- require 'csv_row_model/model/dynamic_columns'
5
- require 'csv_row_model/model/comparison'
1
+ require 'csv_row_model/concerns/model/base'
2
+ require 'csv_row_model/concerns/model/attributes'
3
+ require 'csv_row_model/concerns/model/children'
4
+ require 'csv_row_model/concerns/model/dynamic_columns'
6
5
 
7
6
  module CsvRowModel
8
7
  # Base module for representing a RowModel---a model that represents row(s).
9
8
  module Model
10
9
  extend ActiveSupport::Concern
11
10
 
12
- include InheritedClassVar
13
-
14
11
  include ActiveWarnings
15
- include Validators::ValidateAttributes
16
12
 
17
13
  include Base
18
-
19
- include Columns
14
+ include Attributes
20
15
  include Children
21
16
  include DynamicColumns
22
-
23
- include Comparison
24
17
  end
25
18
  end
@@ -1,6 +1,6 @@
1
1
  class BooleanFormatValidator < ActiveModel::EachValidator # :nodoc:
2
2
  # inspired by https://github.com/MrJoy/to_bool/blob/5c9ed38e47c638725e33530ea1a8aec96281af20/lib/to_bool.rb#L23
3
- FALSE_BOOLEAN_REGEX = /^(false|f|no|n|0|)$/i
3
+ FALSE_BOOLEAN_REGEX = /^(false|f|no|n|0)$/i
4
4
  TRUE_BOOLEAN_REGEX = /^(true|t|yes|y|1)$/i
5
5
 
6
6
  def validate_each(record, attribute, value)
@@ -0,0 +1,7 @@
1
+ class DateFormatValidator < ActiveModel::EachValidator # :nodoc:
2
+ def validate_each(record, attribute, value)
3
+ Date.parse(value)
4
+ rescue ArgumentError, TypeError
5
+ record.errors.add(attribute, 'is not a Date format')
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class DateTimeFormatValidator < ActiveModel::EachValidator # :nodoc:
2
+ def validate_each(record, attribute, value)
3
+ DateTime.parse(value)
4
+ rescue ArgumentError, TypeError
5
+ record.errors.add(attribute, 'is not a DateTime format')
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class FloatFormatValidator < ActiveModel::EachValidator # :nodoc:
2
+ def validate_each(record, attribute, value)
3
+ Float(value)
4
+ rescue ArgumentError, TypeError
5
+ record.errors.add(attribute, 'is not a Float format')
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ class IntegerFormatValidator < ActiveModel::EachValidator # :nodoc:
2
+ def validate_each(record, attribute, value)
3
+ Integer(value)
4
+ rescue ArgumentError
5
+ integer = value.to_i
6
+ return if integer == value.to_f && integer != 0
7
+ add_error(record, attribute)
8
+ rescue TypeError
9
+ add_error(record, attribute)
10
+ end
11
+
12
+ def add_error(record, attribute)
13
+ record.errors.add(attribute, 'is not a Integer format')
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module CsvRowModel
2
- VERSION = "1.0.0.beta1"
2
+ VERSION = "1.0.0.beta2"
3
3
  end
data/lib/csv_row_model.rb CHANGED
@@ -1,49 +1,28 @@
1
- autoload = false
2
- # autoload = true #uncomment for testing purposes only, not covered by rspec
3
-
4
1
  class Boolean; end unless defined? Boolean
5
2
 
6
3
  require 'csv_row_model/version'
7
4
 
5
+ require 'csv'
8
6
  require 'active_model'
9
- require 'active_support/all'
10
7
  require 'active_warnings'
11
- require 'csv'
12
-
13
- require 'inherited_class_var'
14
-
15
- if autoload && defined?(Rails)
16
- require 'csv_row_model/engine'
17
- else
18
- require 'csv_row_model/concerns/inspect'
19
- require 'csv_row_model/concerns/check_options'
20
-
21
- require 'csv_row_model/validators/validate_attributes'
22
-
23
- require 'csv_row_model/model'
24
- require 'csv_row_model/model/file_model'
25
8
 
26
- require 'csv_row_model/import'
27
- require 'csv_row_model/import/file_model'
28
- require 'csv_row_model/import/csv'
29
- require 'csv_row_model/import/file'
9
+ require 'csv_row_model/concerns/check_options'
30
10
 
11
+ require 'csv_row_model/model'
12
+ require 'csv_row_model/model/file_model'
31
13
 
32
- require 'csv_row_model/export'
33
- require 'csv_row_model/export/file'
34
- require 'csv_row_model/export/file_model'
35
- end
14
+ require 'csv_row_model/import'
15
+ require 'csv_row_model/import/file_model'
16
+ require 'csv_row_model/import/file'
36
17
 
37
- require 'csv_row_model/validators/default_change'
38
18
 
39
- require 'csv_row_model/validators/number_validator'
40
- require 'csv_row_model/validators/boolean_format'
41
- require 'csv_row_model/validators/date_time_format'
42
- require 'csv_row_model/validators/date_format'
43
- require 'csv_row_model/validators/float_format'
44
- require 'csv_row_model/validators/integer_format'
19
+ require 'csv_row_model/export'
20
+ require 'csv_row_model/export/file'
21
+ require 'csv_row_model/export/file_model'
45
22
 
46
- module CsvRowModel
47
- class RowModelClassNotDefined < StandardError; end
48
- class AccessedInvalidAttribute < StandardError; end
49
- end
23
+ require 'csv_row_model/validators/default_change_validator'
24
+ require 'csv_row_model/validators/boolean_format_validator'
25
+ require 'csv_row_model/validators/date_time_format_validator'
26
+ require 'csv_row_model/validators/date_format_validator'
27
+ require 'csv_row_model/validators/float_format_validator'
28
+ require 'csv_row_model/validators/integer_format_validator'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv_row_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta1
4
+ version: 1.0.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Chung
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-06-30 00:00:00.000000000 Z
12
+ date: 2016-07-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -74,47 +74,49 @@ files:
74
74
  - bin/setup
75
75
  - csv_row_model.gemspec
76
76
  - lib/csv_row_model.rb
77
+ - lib/csv_row_model/concerns/attributes_base.rb
77
78
  - lib/csv_row_model/concerns/check_options.rb
79
+ - lib/csv_row_model/concerns/dynamic_columns_base.rb
80
+ - lib/csv_row_model/concerns/export/attributes.rb
81
+ - lib/csv_row_model/concerns/export/base.rb
82
+ - lib/csv_row_model/concerns/export/dynamic_columns.rb
83
+ - lib/csv_row_model/concerns/hidden_module.rb
84
+ - lib/csv_row_model/concerns/import/attributes.rb
85
+ - lib/csv_row_model/concerns/import/base.rb
86
+ - lib/csv_row_model/concerns/import/csv_string_model.rb
87
+ - lib/csv_row_model/concerns/import/dynamic_columns.rb
88
+ - lib/csv_row_model/concerns/import/represents.rb
78
89
  - lib/csv_row_model/concerns/inspect.rb
79
- - lib/csv_row_model/engine.rb
90
+ - lib/csv_row_model/concerns/model/attributes.rb
91
+ - lib/csv_row_model/concerns/model/base.rb
92
+ - lib/csv_row_model/concerns/model/children.rb
93
+ - lib/csv_row_model/concerns/model/dynamic_columns.rb
80
94
  - lib/csv_row_model/export.rb
81
- - lib/csv_row_model/export/attributes.rb
82
- - lib/csv_row_model/export/base.rb
83
- - lib/csv_row_model/export/cell.rb
84
- - lib/csv_row_model/export/dynamic_column_cell.rb
85
- - lib/csv_row_model/export/dynamic_columns.rb
86
95
  - lib/csv_row_model/export/file.rb
87
96
  - lib/csv_row_model/export/file_model.rb
88
97
  - lib/csv_row_model/import.rb
89
- - lib/csv_row_model/import/attributes.rb
90
- - lib/csv_row_model/import/base.rb
91
- - lib/csv_row_model/import/cell.rb
92
- - lib/csv_row_model/import/csv.rb
93
- - lib/csv_row_model/import/csv_string_model.rb
94
- - lib/csv_row_model/import/dynamic_column_cell.rb
95
- - lib/csv_row_model/import/dynamic_columns.rb
96
98
  - lib/csv_row_model/import/file.rb
97
- - lib/csv_row_model/import/file/callbacks.rb
98
- - lib/csv_row_model/import/file/validations.rb
99
99
  - lib/csv_row_model/import/file_model.rb
100
- - lib/csv_row_model/import/representation.rb
101
- - lib/csv_row_model/import/represents.rb
100
+ - lib/csv_row_model/internal/attribute_base.rb
101
+ - lib/csv_row_model/internal/concerns/column_shared.rb
102
+ - lib/csv_row_model/internal/concerns/dynamic_column_shared.rb
103
+ - lib/csv_row_model/internal/dynamic_column_attribute_base.rb
104
+ - lib/csv_row_model/internal/export/attribute.rb
105
+ - lib/csv_row_model/internal/export/dynamic_column_attribute.rb
106
+ - lib/csv_row_model/internal/import/attribute.rb
107
+ - lib/csv_row_model/internal/import/csv.rb
108
+ - lib/csv_row_model/internal/import/dynamic_column_attribute.rb
109
+ - lib/csv_row_model/internal/import/representation.rb
110
+ - lib/csv_row_model/internal/model/dynamic_column_header.rb
111
+ - lib/csv_row_model/internal/model/header.rb
102
112
  - lib/csv_row_model/model.rb
103
- - lib/csv_row_model/model/base.rb
104
- - lib/csv_row_model/model/children.rb
105
- - lib/csv_row_model/model/columns.rb
106
- - lib/csv_row_model/model/comparison.rb
107
- - lib/csv_row_model/model/dynamic_column_cell.rb
108
- - lib/csv_row_model/model/dynamic_columns.rb
109
113
  - lib/csv_row_model/model/file_model.rb
110
- - lib/csv_row_model/validators/boolean_format.rb
111
- - lib/csv_row_model/validators/date_format.rb
112
- - lib/csv_row_model/validators/date_time_format.rb
113
- - lib/csv_row_model/validators/default_change.rb
114
- - lib/csv_row_model/validators/float_format.rb
115
- - lib/csv_row_model/validators/integer_format.rb
116
- - lib/csv_row_model/validators/number_validator.rb
117
- - lib/csv_row_model/validators/validate_attributes.rb
114
+ - lib/csv_row_model/validators/boolean_format_validator.rb
115
+ - lib/csv_row_model/validators/date_format_validator.rb
116
+ - lib/csv_row_model/validators/date_time_format_validator.rb
117
+ - lib/csv_row_model/validators/default_change_validator.rb
118
+ - lib/csv_row_model/validators/float_format_validator.rb
119
+ - lib/csv_row_model/validators/integer_format_validator.rb
118
120
  - lib/csv_row_model/version.rb
119
121
  homepage: https://github.com/s12chung/csv_row_model
120
122
  licenses:
@@ -1,5 +0,0 @@
1
- module CsvRowModel
2
- class Engine < ::Rails::Engine
3
- config.autoload_paths += %W[#{config.root}/lib/]
4
- end
5
- end
@@ -1,46 +0,0 @@
1
- require 'csv_row_model/export/cell'
2
-
3
- module CsvRowModel
4
- module Export
5
- module Attributes
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- self.column_names.each { |*args| define_attribute_method(*args) }
10
- end
11
-
12
- def cell_objects
13
- @cell_objects ||= array_to_block_hash(self.class.column_names) { |column_name| Cell.new(column_name, self) }
14
- end
15
-
16
- # @return [Hash] a map of `column_name => formatted_attributes`
17
- def formatted_attributes
18
- array_to_block_hash(self.class.column_names) { |column_name| formatted_attribute(column_name) }
19
- end
20
-
21
- def formatted_attribute(column_name)
22
- cell_objects[column_name].try(:value)
23
- end
24
-
25
- def source_attribute(column_name)
26
- cell_objects[column_name].try(:source_value)
27
- end
28
-
29
- class_methods do
30
- protected
31
- # See {Model#column}
32
- def column(column_name, options={})
33
- super
34
- define_attribute_method(column_name)
35
- end
36
-
37
- # Define default attribute method for a column
38
- # @param column_name [Symbol] the cell's column_name
39
- def define_attribute_method(column_name)
40
- return if method_defined? column_name
41
- define_method(column_name) { source_model.public_send(column_name) }
42
- end
43
- end
44
- end
45
- end
46
- end
@@ -1,24 +0,0 @@
1
- module CsvRowModel
2
- module Export
3
- class Cell
4
- attr_reader :column_name, :row_model
5
-
6
- def initialize(column_name, row_model)
7
- @column_name = column_name
8
- @row_model = row_model
9
- end
10
-
11
- def value
12
- formatted_value
13
- end
14
-
15
- def formatted_value
16
- @formatted_value ||= row_model.class.format_cell(source_value, column_name, row_model.class.index(column_name), row_model.context)
17
- end
18
-
19
- def source_value
20
- row_model.public_send(column_name)
21
- end
22
- end
23
- end
24
- end
@@ -1,29 +0,0 @@
1
- module CsvRowModel
2
- module Export
3
- class DynamicColumnCell < CsvRowModel::Model::DynamicColumnCell
4
- def unformatted_value
5
- formatted_cells
6
- end
7
-
8
- def formatted_cells
9
- cells.map.with_index.map do |cell, index|
10
- row_model.class.format_cell(cell, column_name, dynamic_column_index + index, row_model.context)
11
- end
12
- end
13
-
14
- def cells
15
- header_models.map { |header_model| call_process_cell(header_model) }
16
- end
17
-
18
- def header_models
19
- row_model.context.public_send(column_name)
20
- end
21
-
22
- class << self
23
- def define_process_cell(row_model_class, column_name)
24
- super { |header_model| header_model }
25
- end
26
- end
27
- end
28
- end
29
- end
@@ -1,46 +0,0 @@
1
- require 'csv_row_model/export/dynamic_column_cell'
2
-
3
- module CsvRowModel
4
- module Export
5
- module DynamicColumns
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- self.dynamic_column_names.each { |*args| define_dynamic_attribute_method(*args) }
10
- end
11
-
12
- def cell_objects
13
- @dynamic_column_cell_objects ||= super.merge(array_to_block_hash(self.class.dynamic_column_names) do |column_name|
14
- DynamicColumnCell.new(column_name, self)
15
- end)
16
- end
17
-
18
- # @return [Array] an array of public_send(column_name) of the CSV model
19
- def to_row
20
- super.flatten
21
- end
22
-
23
- # See Model::Export#formatted_attributes
24
- def formatted_attributes
25
- super.merge!(array_to_block_hash(self.class.dynamic_column_names) { |column_name| formatted_attribute(column_name) })
26
- end
27
-
28
- class_methods do
29
- protected
30
-
31
- # See {Model::DynamicColumns#dynamic_column}
32
- def dynamic_column(column_name, options={})
33
- super
34
- define_dynamic_attribute_method(column_name)
35
- end
36
-
37
- # Define default attribute method for a dynamic_column
38
- # @param column_name [Symbol] the cell's column_name
39
- def define_dynamic_attribute_method(column_name)
40
- define_method(column_name) { formatted_attribute(column_name) }
41
- DynamicColumnCell.define_process_cell(self, column_name)
42
- end
43
- end
44
- end
45
- end
46
- end
@@ -1,66 +0,0 @@
1
- require 'csv_row_model/import/cell'
2
-
3
- module CsvRowModel
4
- module Import
5
- module Attributes
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- self.column_names.each { |*args| define_attribute_method(*args) }
10
- end
11
-
12
- def cell_objects
13
- @cell_objects ||= begin
14
- csv_string_model.valid?
15
- _cell_objects(csv_string_model.errors)
16
- end
17
- end
18
-
19
- # @return [Hash] a map of `column_name => original_attribute(column_name)`
20
- def original_attributes
21
- array_to_block_hash(self.class.column_names) { |column_name| original_attribute(column_name) }
22
- end
23
-
24
- # @return [Object] the column's attribute before override
25
- def original_attribute(column_name)
26
- cell_objects[column_name].try(:value)
27
- end
28
-
29
- # return [Hash] a map changes from {.column}'s default option': `column_name -> [value_before_default, default_set]`
30
- def default_changes
31
- array_to_block_hash(self.class.column_names) { |column_name| cell_objects[column_name].default_change }.delete_if {|k, v| v.blank? }
32
- end
33
-
34
- protected
35
- # to prevent circular dependency with csv_string_model
36
- def _cell_objects(csv_string_model_errors={})
37
- array_to_block_hash(self.class.column_names) do |column_name|
38
- Cell.new(column_name, mapped_row[column_name], csv_string_model_errors[column_name], self)
39
- end
40
- end
41
-
42
- class_methods do
43
- protected
44
- # See {Model#column}
45
- def column(column_name, options={})
46
- super
47
- define_attribute_method(column_name)
48
- end
49
-
50
- def merge_options(column_name, options={})
51
- original_options = columns[column_name]
52
- csv_string_model_class.add_type_validation(column_name, columns[column_name]) unless original_options[:validate_type]
53
- super
54
- end
55
-
56
- # Define default attribute method for a column
57
- # @param column_name [Symbol] the cell's column_name
58
- def define_attribute_method(column_name)
59
- return if method_defined? column_name
60
- csv_string_model_class.add_type_validation(column_name, columns[column_name])
61
- define_method(column_name) { original_attribute(column_name) }
62
- end
63
- end
64
- end
65
- end
66
- end
@@ -1,37 +0,0 @@
1
- module CsvRowModel
2
- module Import
3
- class DynamicColumnCell < CsvRowModel::Model::DynamicColumnCell
4
- attr_reader :source_headers, :source_cells
5
-
6
- def initialize(column_name, source_headers, source_cells, row_model)
7
- @source_headers = source_headers
8
- @source_cells = source_cells
9
- super(column_name, row_model)
10
- end
11
-
12
- def unformatted_value
13
- formatted_cells.zip(formatted_headers).map do |formatted_cell, source_header|
14
- call_process_cell(formatted_cell, source_header)
15
- end
16
- end
17
-
18
- def formatted_cells
19
- source_cells.map.with_index do |source_cell, index|
20
- row_model.class.format_cell(source_cell, column_name, dynamic_column_index + index, row_model.context)
21
- end
22
- end
23
-
24
- def formatted_headers
25
- source_headers.map.with_index do |source_header, index|
26
- row_model.class.format_dynamic_column_header(source_header, column_name, dynamic_column_index, index, row_model.context)
27
- end
28
- end
29
-
30
- class << self
31
- def define_process_cell(row_model_class, column_name)
32
- super { |formatted_cell, source_header| formatted_cell }
33
- end
34
- end
35
- end
36
- end
37
- end
@@ -1,59 +0,0 @@
1
- require 'csv_row_model/import/dynamic_column_cell'
2
-
3
- module CsvRowModel
4
- module Import
5
- module DynamicColumns
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- self.dynamic_column_names.each { |*args| define_dynamic_attribute_method(*args) }
10
- end
11
-
12
- def cell_objects
13
- @dynamic_column_cell_objects ||= super.merge(array_to_block_hash(self.class.dynamic_column_names) do |column_name|
14
- DynamicColumnCell.new(column_name, dynamic_column_source_headers, dynamic_column_source_cells, self)
15
- end)
16
- end
17
-
18
- # @return [Hash] a map of `column_name => original_attribute(column_name)`
19
- def original_attributes
20
- super.merge!(array_to_block_hash(self.class.dynamic_column_names) { |column_name| original_attribute(column_name) })
21
- end
22
-
23
- # @return [Array] dynamic_column headers
24
- def dynamic_column_source_headers
25
- self.class.dynamic_column_source_headers source_header
26
- end
27
-
28
- # @return [Array] dynamic_column row data
29
- def dynamic_column_source_cells
30
- self.class.dynamic_column_source_cells source_row
31
- end
32
-
33
- class_methods do
34
- def dynamic_column_source_headers(source_header)
35
- dynamic_columns? ? source_header[columns.size..-1] : []
36
- end
37
-
38
- def dynamic_column_source_cells(source_row)
39
- dynamic_columns? ? source_row[columns.size..-1] : []
40
- end
41
-
42
- protected
43
-
44
- # See {Model#dynamic_column}
45
- def dynamic_column(column_name, options={})
46
- super
47
- define_dynamic_attribute_method(column_name)
48
- end
49
-
50
- # Define default attribute method for a column
51
- # @param column_name [Symbol] the cell's column_name
52
- def define_dynamic_attribute_method(column_name)
53
- define_method(column_name) { original_attribute(column_name) }
54
- DynamicColumnCell.define_process_cell(self, column_name)
55
- end
56
- end
57
- end
58
- end
59
- end
@@ -1,17 +0,0 @@
1
- module CsvRowModel
2
- module Import
3
- class File
4
- module Callbacks
5
- extend ActiveSupport::Concern
6
-
7
- included do
8
- extend ActiveModel::Callbacks
9
-
10
- define_model_callbacks :each_iteration
11
- define_model_callbacks :next
12
- define_model_callbacks :abort, :skip, only: :before
13
- end
14
- end
15
- end
16
- end
17
- end