class TermuxRubyApi::Utils::Xformer
Public Class Methods
xform(object, args)
click to toggle source
# File lib/termux_ruby_api/utils/xformer.rb, line 4 def self.xform(object, args) collection = object.is_a?(Array) ? object : [object] collection.each { |i| xform_item(i, args) } object end
Private Class Methods
fix_time(time_str)
click to toggle source
# File lib/termux_ruby_api/utils/xformer.rb, line 47 def self.fix_time(time_str) # Termux likes to give hours in 24:xx:xx or 24:xx format instead of 00:xx:xx or 00:xx time_str.gsub(/\ (24)(:\d\d)/, ' 00' + '\2') end
to_duration(value)
click to toggle source
# File lib/termux_ruby_api/utils/xformer.rb, line 39 def self.to_duration(value) parts = value.split(':') res = parts.pop.to_i res += (parts.pop.to_i * 60) if parts.any? res += (parts.pop.to_i * 60 * 60) if parts.any? res end
xform_item(item, args)
click to toggle source
# File lib/termux_ruby_api/utils/xformer.rb, line 12 def self.xform_item(item, args) return unless item.is_a?(Hash) args.each do |arg, type| next unless item.key?(arg) item[arg] = xform_prop(item[arg], type) end end
xform_prop(value, type)
click to toggle source
# File lib/termux_ruby_api/utils/xformer.rb, line 20 def self.xform_prop(value, type) case type when :date Date.parse(value) when :time Time.parse(fix_time(value)) when :duration to_duration(value) when :integer value.to_i when :float value.to_f when :symbol value.to_sym else value end end