lib/array_hasher/formatter.rb in array_hasher-0.1.4 vs lib/array_hasher/formatter.rb in array_hasher-0.1.5
- old
+ new
@@ -1,18 +1,22 @@
+# frozen_string_literal: true
+
require 'time'
module ArrayHasher
class Formatter
attr_accessor :cols, :types
REGEXP_EMPTY = /\A\s*\z/
TYPES = {
- int: Proc.new {|v| (v.nil? || v =~ REGEXP_EMPTY) ? nil : v.gsub(/[^\d]+/, '').to_i },
- float: Proc.new {|v| (v.nil? || v =~ REGEXP_EMPTY) ? nil : v.gsub(/[^\d\.]+/, '').to_f },
- string: Proc.new {|v| v.to_s },
- time: Proc.new {|v| v ? Time.parse(v) : nil }
+ int: Proc.new { |v| (v.nil? || v =~ REGEXP_EMPTY) ? nil : v.gsub(/[^\d]+/, '').to_i },
+ float: Proc.new { |v| (v.nil? || v =~ REGEXP_EMPTY) ? nil : v.gsub(/[^\d\.]+/, '').to_f },
+ string: Proc.new { |v| v.to_s },
+ time: Proc.new { |v| v ? Time.parse(v) : nil },
+ json: Proc.new { |v| v ? JSON.parse(v) : nil },
+ date: Proc.new { |v| v ? Date.parse(v) : nil }
}
# cols:
# [
# [], # ignore this col
@@ -27,11 +31,11 @@
@cols = cols.map do |name, type, opts|
[
name ? name.to_sym : nil,
(type.nil? || type.is_a?(Proc)) ? type : type.to_sym,
- (opts || {}).each_with_object({}) {|kv, r| r[kv[0].to_sym] = kv[1] }
+ (opts || {}).each_with_object({}) { |kv, r| r[kv[0].to_sym] = kv[1] }
]
end
end
def define_type(type, &block)
@@ -57,6 +61,5 @@
end
end
end
end
end
-