csv_row_model 0.1.0 → 0.1.1

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.
@@ -7,7 +7,8 @@ module CsvRowModel
7
7
  included do
8
8
  extend ActiveModel::Callbacks
9
9
 
10
- define_model_callbacks :yield
10
+ define_model_callbacks :each_iteration
11
+ define_model_callbacks :next
11
12
  define_model_callbacks :abort, :skip, only: :before
12
13
  end
13
14
  end
@@ -3,16 +3,10 @@ module CsvRowModel
3
3
  # represents just one model.
4
4
  # It needs CsvRowModel::Import
5
5
  module Import
6
- module SingleModel
6
+ module FileModel
7
7
  extend ActiveSupport::Concern
8
8
 
9
9
  class_methods do
10
-
11
- # @return [Symbol] returns type of import
12
- def type
13
- :single_model
14
- end
15
-
16
10
  # Safe to override
17
11
  #
18
12
  # @param cell [String] the cell's string
@@ -33,6 +27,25 @@ module CsvRowModel
33
27
  end
34
28
  end
35
29
  end
30
+
31
+ def next(csv, context={}, previous=nil)
32
+ return csv.read_row unless csv.next_row
33
+
34
+ source_row = Array.new(header_matchers.size)
35
+
36
+ while csv.next_row
37
+ current_row = csv.read_row
38
+ current_row.each_with_index do |cell, position|
39
+ next if cell.blank?
40
+ index = index_header_match(cell)
41
+ next unless index
42
+ source_row[index] = current_row[position + 1]
43
+ break
44
+ end
45
+ end
46
+
47
+ new(source_row, context: context, previous: previous)
48
+ end
36
49
  end
37
50
  end
38
51
  end
@@ -1,7 +1,7 @@
1
1
  module CsvRowModel
2
2
  module Import
3
3
  class Presenter
4
- include Concerns::DeepClassVar
4
+ include Concerns::InheritedClassVar
5
5
  include Concerns::Inspect
6
6
  include ActiveWarnings
7
7
 
@@ -38,6 +38,12 @@ module CsvRowModel
38
38
  row_model.previous.try(:presenter)
39
39
  end
40
40
 
41
+ def attributes
42
+ self.class.attribute_names
43
+ .zip(self.class.attribute_names.map { |attribute_name| public_send(attribute_name) })
44
+ .to_h
45
+ end
46
+
41
47
  protected
42
48
 
43
49
  # add errors from row_model and remove each dependent attribute from errors if it's row_model_dependencies
@@ -76,14 +82,14 @@ module CsvRowModel
76
82
  Presenter
77
83
  end
78
84
 
79
- # @return [Array<Symbol>] attribute names for the Mapper
85
+ # @return [Array<Symbol>] attribute names for the Presenter
80
86
  def attribute_names
81
87
  attributes.keys
82
88
  end
83
89
 
84
90
  # @return [Hash{Symbol => Array}] map of `attribute_name => [options, block]`
85
91
  def attributes
86
- deep_class_var :@_mapper_attributes, {}, :merge
92
+ inherited_class_var :@_presenter_attributes, {}, :merge
87
93
  end
88
94
 
89
95
  # @param [Symbol] attribute_name name of attribute to find option
@@ -98,9 +104,9 @@ module CsvRowModel
98
104
  attributes[attribute_name].last
99
105
  end
100
106
 
101
- # @return [Hash{Symbol => Array}] map of `dependency => [array of mapper attributes dependent on dependency]`
107
+ # @return [Hash{Symbol => Array}] map of `dependency => [array of presenter attributes dependent on dependency]`
102
108
  def dependencies
103
- deep_class_cache(:@_mapper_dependencies) do
109
+ class_cache(:@_presenter_dependencies) do
104
110
  dependencies = {}
105
111
  attribute_names.each do |attribute_name|
106
112
  options(attribute_name)[:dependencies].each do |dependency|
@@ -118,10 +124,10 @@ module CsvRowModel
118
124
  end
119
125
 
120
126
  def merge_attribute(attribute_hash)
121
- @_mapper_attributes ||= {}
122
- clear_deep_class_cache(:@_mapper_attributes)
123
- clear_deep_class_cache(:@_mapper_dependencies)
124
- @_mapper_attributes.merge! attribute_hash
127
+ @_presenter_attributes ||= {}
128
+ deep_clear_class_cache(:@_presenter_attributes)
129
+ deep_clear_class_cache(:@_presenter_dependencies)
130
+ @_presenter_attributes.merge! attribute_hash
125
131
  end
126
132
 
127
133
  # Adds column to the row model
@@ -9,7 +9,7 @@ module CsvRowModel
9
9
  extend ActiveSupport::Concern
10
10
 
11
11
  included do
12
- include Concerns::DeepClassVar
12
+ include Concerns::InheritedClassVar
13
13
 
14
14
  include ActiveWarnings
15
15
  include Validators::ValidateAttributes
@@ -44,13 +44,13 @@ module CsvRowModel
44
44
  #
45
45
  # @return [Hash] map of `relation_name => CsvRowModel::Import or CsvRowModel::Export class`
46
46
  def has_many_relationships
47
- deep_class_var :@_has_many_relationships, {}, :merge
47
+ inherited_class_var :@_has_many_relationships, {}, :merge
48
48
  end
49
49
 
50
50
  protected
51
51
  def merge_has_many_relationships(relation_hash)
52
52
  @_has_many_relationships ||= {}
53
- clear_deep_class_cache(:@_has_many_relationships)
53
+ deep_clear_class_cache(:@_has_many_relationships)
54
54
  @_has_many_relationships.merge! relation_hash
55
55
  end
56
56
 
@@ -22,7 +22,7 @@ module CsvRowModel
22
22
 
23
23
  # @return [Hash] column names mapped to their options
24
24
  def columns
25
- deep_class_var(:@_columns, {}, :merge)
25
+ inherited_class_var(:@_columns, {}, :merge)
26
26
  end
27
27
 
28
28
  # @param [Symbol] column_name name of column to find option
@@ -37,11 +37,34 @@ module CsvRowModel
37
37
  column_names.index column_name
38
38
  end
39
39
 
40
+ # @param [Symbol] column_name name of column to check
41
+ # @return [Boolean] true if it's a column name
42
+ def is_column_name? column_name
43
+ column_name.is_a?(Symbol) && index(column_name)
44
+ end
45
+
46
+
47
+ # @return [Array] column headers for the row model
48
+ def headers
49
+ @headers ||= begin
50
+ columns.map do |name, options|
51
+ options[:header] || format_header(name)
52
+ end
53
+ end
54
+ end
55
+
56
+ # Safe to override
57
+ #
58
+ # @return [String] formatted header
59
+ def format_header(column_name)
60
+ column_name
61
+ end
62
+
40
63
  protected
41
64
 
42
65
  def merge_columns(column_hash)
43
66
  @_columns ||= {}
44
- clear_deep_class_cache(:@_columns)
67
+ deep_clear_class_cache(:@_columns)
45
68
  @_columns.merge!(column_hash)
46
69
  end
47
70
 
@@ -1,16 +1,7 @@
1
1
  module CsvRowModel
2
2
  module Model
3
- class CsvStringModel
3
+ class CsvStringModel < OpenStruct
4
4
  include ActiveWarnings
5
-
6
- def initialize(source)
7
- @source = source.symbolize_keys
8
- end
9
-
10
- def method_missing(name, *args, &block)
11
- return super unless @source.keys.include? name
12
- @source[name]
13
- end
14
5
  end
15
6
  end
16
7
  end
@@ -1,6 +1,6 @@
1
1
  module CsvRowModel
2
2
  module Model
3
- module SingleModel
3
+ module FileModel
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  included do
@@ -9,6 +9,7 @@ module CsvRowModel
9
9
  alias_method :row_names, :column_names
10
10
  alias_method :rows, :columns
11
11
  alias_method :row, :column
12
+ alias_method :is_row_name?, :is_column_name?
12
13
  end
13
14
  end
14
15
  end
@@ -1,3 +1,3 @@
1
1
  module CsvRowModel
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
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.1.0
4
+ version: 0.1.1
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-08-31 00:00:00.000000000 Z
11
+ date: 2015-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -40,7 +40,7 @@ dependencies:
40
40
  version: 0.1.2
41
41
  description:
42
42
  email:
43
- - steve.chung7@gmail.com
43
+ - hello@stevenchung.ca
44
44
  executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
@@ -60,12 +60,12 @@ files:
60
60
  - bin/setup
61
61
  - csv_row_model.gemspec
62
62
  - lib/csv_row_model.rb
63
- - lib/csv_row_model/concerns/deep_class_var.rb
63
+ - lib/csv_row_model/concerns/inherited_class_var.rb
64
64
  - lib/csv_row_model/concerns/inspect.rb
65
65
  - lib/csv_row_model/engine.rb
66
66
  - lib/csv_row_model/export.rb
67
- - lib/csv_row_model/export/csv.rb
68
- - lib/csv_row_model/export/single_model.rb
67
+ - lib/csv_row_model/export/file.rb
68
+ - lib/csv_row_model/export/file_model.rb
69
69
  - lib/csv_row_model/import.rb
70
70
  - lib/csv_row_model/import/attributes.rb
71
71
  - lib/csv_row_model/import/csv.rb
@@ -73,13 +73,13 @@ files:
73
73
  - lib/csv_row_model/import/file.rb
74
74
  - lib/csv_row_model/import/file/callbacks.rb
75
75
  - lib/csv_row_model/import/file/validations.rb
76
+ - lib/csv_row_model/import/file_model.rb
76
77
  - lib/csv_row_model/import/presenter.rb
77
- - lib/csv_row_model/import/single_model.rb
78
78
  - lib/csv_row_model/model.rb
79
79
  - lib/csv_row_model/model/children.rb
80
80
  - lib/csv_row_model/model/columns.rb
81
81
  - lib/csv_row_model/model/csv_string_model.rb
82
- - lib/csv_row_model/model/single_model.rb
82
+ - lib/csv_row_model/model/file_model.rb
83
83
  - lib/csv_row_model/validators/boolean_format.rb
84
84
  - lib/csv_row_model/validators/date_format.rb
85
85
  - lib/csv_row_model/validators/default_change.rb
@@ -1,43 +0,0 @@
1
- require 'csv'
2
-
3
- module CsvRowModel
4
- module Export
5
- class Csv
6
- attr_reader :export_model_class, :csv, :file
7
-
8
- # @param [Export] export_model export model class
9
- def initialize(export_model_class)
10
- @export_model_class = export_model_class
11
- @file = Tempfile.new("#{export_model_class}.csv")
12
- end
13
-
14
- def header
15
- export_model_class.column_headers
16
- end
17
-
18
- def append_header
19
- csv << header
20
- end
21
-
22
- def append_model(model, context={})
23
- export_model_class.new(model, context).to_rows.each do |row|
24
- csv << row
25
- end
26
- end
27
-
28
- def generate(with_header: true)
29
- CSV.open(file.path,"wb") do |csv|
30
- @csv = csv
31
- append_header if with_header && !export_model_class.single_model?
32
- yield self
33
- end
34
- ensure
35
- @csv = nil
36
- end
37
-
38
- def to_s
39
- file.read
40
- end
41
- end
42
- end
43
- end