honey_format 0.16.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.hound.yml +3 -0
  3. data/.rubocop.yml +7 -0
  4. data/.ruby-style-guide.yml +264 -0
  5. data/CHANGELOG.md +15 -0
  6. data/Gemfile +2 -0
  7. data/README.md +63 -15
  8. data/Rakefile +2 -0
  9. data/bin/benchmark +2 -0
  10. data/bin/console +1 -0
  11. data/exe/honey_format +1 -0
  12. data/honey_format.gemspec +5 -4
  13. data/lib/honey_format/cli/benchmark_cli.rb +15 -13
  14. data/lib/honey_format/cli/cli.rb +17 -12
  15. data/lib/honey_format/cli/result_writer.rb +2 -0
  16. data/lib/honey_format/configuration.rb +114 -11
  17. data/lib/honey_format/converters/convert_boolean.rb +24 -0
  18. data/lib/honey_format/converters/convert_date_and_time.rb +30 -0
  19. data/lib/honey_format/converters/convert_number.rb +33 -0
  20. data/lib/honey_format/converters/convert_string.rb +42 -0
  21. data/lib/honey_format/converters/converters.rb +12 -0
  22. data/lib/honey_format/converters/header_column_converter.rb +57 -0
  23. data/lib/honey_format/csv.rb +22 -14
  24. data/lib/honey_format/errors.rb +8 -2
  25. data/lib/honey_format/helpers/helpers.rb +41 -0
  26. data/lib/honey_format/{header.rb → matrix/header.rb} +48 -12
  27. data/lib/honey_format/matrix/matrix.rb +104 -0
  28. data/lib/honey_format/{row.rb → matrix/row.rb} +6 -3
  29. data/lib/honey_format/{row_builder.rb → matrix/row_builder.rb} +5 -4
  30. data/lib/honey_format/{rows.rb → matrix/rows.rb} +7 -4
  31. data/lib/honey_format/matrix.rb +6 -89
  32. data/lib/honey_format/registry.rb +99 -0
  33. data/lib/honey_format/version.rb +4 -2
  34. data/lib/honey_format.rb +14 -6
  35. metadata +34 -24
  36. data/lib/honey_format/header_column_converter.rb +0 -40
  37. data/lib/honey_format/value_converter.rb +0 -117
@@ -1,117 +0,0 @@
1
- require 'date'
2
- require 'time'
3
- require 'set'
4
- require 'digest'
5
-
6
- require 'honey_format/header_column_converter'
7
-
8
- module HoneyFormat
9
- # Converts values
10
- class ValueConverter
11
- # String values considered truthy
12
- TRUTHY = Set.new(%w[t T 1 y Y true TRUE]).freeze
13
- # String values considered falsy
14
- FALSY = Set.new(%w[f F 0 n N false FALSE]).freeze
15
-
16
- # Tries to convert value boolean to, returns nil if it can't convert
17
- CONVERT_BOOLEAN = lambda { |v|
18
- value = v&.downcase
19
- if TRUTHY.include?(value)
20
- true
21
- elsif FALSY.include?(value)
22
- false
23
- else
24
- nil
25
- end
26
- }
27
-
28
- # Default value converters
29
- DEFAULT_CONVERTERS = {
30
- # strict variants
31
- decimal!: proc { |v| Float(v) },
32
- integer!: proc { |v| Integer(v) },
33
- date!: proc { |v| Date.parse(v) },
34
- datetime!: proc { |v| Time.parse(v) },
35
- symbol!: proc { |v| v&.to_sym || raise(ArgumentError, "can't convert nil to symbol") },
36
- downcase!: proc { |v| v&.downcase || raise(ArgumentError, "can't convert nil to downcased string") },
37
- upcase!: proc { |v| v&.upcase || raise(ArgumentError, "can't convert nil to upcased string") },
38
- boolean!: proc { |v|
39
- value = CONVERT_BOOLEAN.call(v)
40
- raise(ArgumentError, "can't convert #{v} to boolean") if value.nil?
41
- value
42
- },
43
- # safe variants
44
- decimal: proc { |v| Float(v) rescue nil },
45
- decimal_or_zero: proc { |v| Float(v) rescue 0.0 },
46
- integer: proc { |v| Integer(v) rescue nil },
47
- integer_or_zero: proc { |v| Integer(v) rescue 0 },
48
- date: proc { |v| Date.parse(v) rescue nil },
49
- datetime: proc { |v| Time.parse(v) rescue nil },
50
- symbol: proc { |v| v&.to_sym },
51
- downcase: proc { |v| v&.downcase },
52
- upcase: proc { |v| v&.upcase },
53
- boolean: proc { |v| CONVERT_BOOLEAN.call(v) },
54
- md5: proc { |v| Digest::MD5.hexdigest(v) if v },
55
- nil: proc {},
56
- header_column: HeaderColumnConverter,
57
- }.freeze
58
-
59
- # Instantiate a value converter
60
- def initialize
61
- @converters = DEFAULT_CONVERTERS.dup
62
- end
63
-
64
- # Returns list of registered types
65
- # @return [Array<Symbol>] list of registered types
66
- def types
67
- @converters.keys
68
- end
69
-
70
- # Register a value converter
71
- # @param [Symbol, String] type the name of the type
72
- # @param [#call] converter that responds to #call
73
- # @return [ValueConverter] returns self
74
- # @raise [ValueTypeExistsError] if type is already registered
75
- def register(type, converter)
76
- self[type] = converter
77
- self
78
- end
79
-
80
- # Convert value
81
- # @param [Symbol, String] type the name of the type
82
- # @param [Object] value to be converted
83
- def call(value, type)
84
- self[type].call(value)
85
- end
86
-
87
- # Register a value converter
88
- # @param [Symbol, String] type the name of the type
89
- # @param [#call] converter that responds to #call
90
- # @return [Object] returns the converter
91
- # @raise [ValueTypeExistsError] if type is already registered
92
- def []=(type, converter)
93
- type = type.to_sym
94
-
95
- if type?(type)
96
- raise(Errors::ValueTypeExistsError, "type '#{type}' already exists")
97
- end
98
-
99
- @converters[type] = converter
100
- end
101
-
102
- # @param [Symbol, String] type the name of the type
103
- # @return [Object] returns the converter
104
- # @raise [UnknownValueTypeError] if type does not exist
105
- def [](type)
106
- @converters.fetch(type.to_sym) do
107
- raise(Errors::UnknownValueTypeError, "unknown type '#{type}'")
108
- end
109
- end
110
-
111
- # @param [Symbol, String] type the name of the type
112
- # @return [true, false] true if type exists, false otherwise
113
- def type?(type)
114
- @converters.key?(type.to_sym)
115
- end
116
- end
117
- end