csv_row_model 0.1.0

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 (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +2 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +10 -0
  7. data/.yardopts +10 -0
  8. data/CONTRIBUTING.md +7 -0
  9. data/Gemfile +20 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +369 -0
  12. data/Rakefile +1 -0
  13. data/bin/console +14 -0
  14. data/bin/setup +7 -0
  15. data/csv_row_model.gemspec +23 -0
  16. data/lib/csv_row_model.rb +46 -0
  17. data/lib/csv_row_model/concerns/deep_class_var.rb +86 -0
  18. data/lib/csv_row_model/concerns/inspect.rb +10 -0
  19. data/lib/csv_row_model/engine.rb +5 -0
  20. data/lib/csv_row_model/export.rb +70 -0
  21. data/lib/csv_row_model/export/csv.rb +43 -0
  22. data/lib/csv_row_model/export/single_model.rb +47 -0
  23. data/lib/csv_row_model/import.rb +125 -0
  24. data/lib/csv_row_model/import/attributes.rb +105 -0
  25. data/lib/csv_row_model/import/csv.rb +136 -0
  26. data/lib/csv_row_model/import/csv/row.rb +17 -0
  27. data/lib/csv_row_model/import/file.rb +133 -0
  28. data/lib/csv_row_model/import/file/callbacks.rb +16 -0
  29. data/lib/csv_row_model/import/file/validations.rb +39 -0
  30. data/lib/csv_row_model/import/presenter.rb +160 -0
  31. data/lib/csv_row_model/import/single_model.rb +41 -0
  32. data/lib/csv_row_model/model.rb +68 -0
  33. data/lib/csv_row_model/model/children.rb +79 -0
  34. data/lib/csv_row_model/model/columns.rb +73 -0
  35. data/lib/csv_row_model/model/csv_string_model.rb +16 -0
  36. data/lib/csv_row_model/model/single_model.rb +16 -0
  37. data/lib/csv_row_model/validators/boolean_format.rb +11 -0
  38. data/lib/csv_row_model/validators/date_format.rb +9 -0
  39. data/lib/csv_row_model/validators/default_change.rb +6 -0
  40. data/lib/csv_row_model/validators/float_format.rb +9 -0
  41. data/lib/csv_row_model/validators/integer_format.rb +9 -0
  42. data/lib/csv_row_model/validators/number_validator.rb +16 -0
  43. data/lib/csv_row_model/validators/validate_attributes.rb +27 -0
  44. data/lib/csv_row_model/version.rb +3 -0
  45. metadata +116 -0
@@ -0,0 +1,11 @@
1
+ class BooleanFormatValidator < ActiveModel::EachValidator # :nodoc:
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
4
+ TRUE_BOOLEAN_REGEX = /^(true|t|yes|y|1)$/i
5
+
6
+ def validate_each(record, attribute, value)
7
+ return if value =~ FALSE_BOOLEAN_REGEX || value =~ TRUE_BOOLEAN_REGEX
8
+
9
+ record.errors.add(attribute, 'is not a Boolean format')
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ class DateFormatValidator < ActiveModel::EachValidator # :nodoc:
2
+ def validate_each(record, attribute, value)
3
+ begin
4
+ Date.parse(value.to_s)
5
+ rescue ArgumentError
6
+ record.errors.add(attribute, 'is not a Date format')
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ class DefaultChangeValidator < ActiveModel::EachValidator # :nodoc:
2
+ def validate_each(record, attribute, value)
3
+ return unless record.default_changes[attribute]
4
+ record.errors.add(attribute, 'changed by default')
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ class FloatFormatValidator < CsvRowModel::Validators::NumberValidator # :nodoc:
2
+ def validate_each(record, attribute, value)
3
+ before, after = before_after_decimal(value)
4
+
5
+ return if value.present? && value.to_f.to_s =~ /#{before}\.#{after.present? ? after : 0}/
6
+
7
+ record.errors.add(attribute, 'is not a Float format')
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class IntegerFormatValidator < CsvRowModel::Validators::NumberValidator # :nodoc:
2
+ def validate_each(record, attribute, value)
3
+ before, after = before_after_decimal(value)
4
+
5
+ return if value.to_i.to_s == before && after.empty?
6
+
7
+ record.errors.add(attribute, 'is not a Integer format')
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ module CsvRowModel
2
+ module Validators
3
+ class NumberValidator < ActiveModel::EachValidator # :nodoc:
4
+ def before_after_decimal(value)
5
+ value ||= ""
6
+
7
+ # if value == "0003434.1233000"
8
+ # before = "3434"; after = "1233"
9
+ before, decimal, after = value.partition(".")
10
+ before.sub!(/\A0+/, "")
11
+ after.sub!(/0+\z/, "")
12
+ [before, after]
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ module CsvRowModel
2
+ module Validators
3
+ # adds validates_attributes method to validate the attributes of an attributes
4
+ module ValidateAttributes
5
+ extend ActiveSupport::Concern
6
+
7
+ class AttributeValidator < ActiveModel::EachValidator # :nodoc:
8
+ def validate_each(record, attribute, value)
9
+ return unless value && (record.try(:using_warnings?) ? value.unsafe? : value.invalid?)
10
+ record.errors.add(attribute)
11
+ end
12
+ end
13
+
14
+ class_methods do
15
+ protected
16
+
17
+ # Adds validation check to add errors any attribute of `attributes` passed is truthy and invalid.
18
+ # Inspired by: https://github.com/rails/rails/blob/2bb0abbec0e4abe843131f188129a1189b1bf714/activerecord/lib/active_record/validations/associated.rb#L46
19
+ #
20
+ # @param [Array<Symbol>] attributes array of attributes to validate their attributes
21
+ def validate_attributes(*attributes)
22
+ validates_with AttributeValidator, { attributes: attributes }
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module CsvRowModel
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv_row_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Steve Chung
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: active_warnings
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.2
41
+ description:
42
+ email:
43
+ - steve.chung7@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".ruby-gemset"
51
+ - ".ruby-version"
52
+ - ".travis.yml"
53
+ - ".yardopts"
54
+ - CONTRIBUTING.md
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - bin/console
60
+ - bin/setup
61
+ - csv_row_model.gemspec
62
+ - lib/csv_row_model.rb
63
+ - lib/csv_row_model/concerns/deep_class_var.rb
64
+ - lib/csv_row_model/concerns/inspect.rb
65
+ - lib/csv_row_model/engine.rb
66
+ - lib/csv_row_model/export.rb
67
+ - lib/csv_row_model/export/csv.rb
68
+ - lib/csv_row_model/export/single_model.rb
69
+ - lib/csv_row_model/import.rb
70
+ - lib/csv_row_model/import/attributes.rb
71
+ - lib/csv_row_model/import/csv.rb
72
+ - lib/csv_row_model/import/csv/row.rb
73
+ - lib/csv_row_model/import/file.rb
74
+ - lib/csv_row_model/import/file/callbacks.rb
75
+ - lib/csv_row_model/import/file/validations.rb
76
+ - lib/csv_row_model/import/presenter.rb
77
+ - lib/csv_row_model/import/single_model.rb
78
+ - lib/csv_row_model/model.rb
79
+ - lib/csv_row_model/model/children.rb
80
+ - lib/csv_row_model/model/columns.rb
81
+ - lib/csv_row_model/model/csv_string_model.rb
82
+ - lib/csv_row_model/model/single_model.rb
83
+ - lib/csv_row_model/validators/boolean_format.rb
84
+ - lib/csv_row_model/validators/date_format.rb
85
+ - lib/csv_row_model/validators/default_change.rb
86
+ - lib/csv_row_model/validators/float_format.rb
87
+ - lib/csv_row_model/validators/integer_format.rb
88
+ - lib/csv_row_model/validators/number_validator.rb
89
+ - lib/csv_row_model/validators/validate_attributes.rb
90
+ - lib/csv_row_model/version.rb
91
+ homepage: https://github.com/s12chung/csv_row_model
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.5
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Import and export your custom CSVs with a intuitive shared Ruby interface.
115
+ test_files: []
116
+ has_rdoc: