module Coolkit
Useful ruby code with no dependencies
Constants
- VERSION
Public Class Methods
data_dir()
click to toggle source
Directory for writing out files. @return [String]
# File lib/coolkit/paths.rb, line 10 def self.data_dir return File.join(self.root_dir, "data") end
root_dir()
click to toggle source
Directory where the gem is located. @return [String]
# File lib/coolkit/paths.rb, line 4 def self.root_dir return File.expand_path("../../..", __FILE__) end
symbolize_keys(arg)
click to toggle source
Recursively convert keys in a Hash or Array
to symbols.
See:
-
[original code](avdi.codes/recursively-symbolize-keys/)
-
[array support](gist.github.com/neektza/8585746)
@param arg [Hash,Array] @return [Hash,Array]
# File lib/coolkit/symbolize.rb, line 10 def self.symbolize_keys(arg) if arg.is_a?(Hash) arg.inject({}) do |result, (key, value)| new_key = case key when String then key.to_sym() else key end new_value = case value when Hash then symbolize_keys(value) when Array then symbolize_keys(value) else value end result[new_key] = new_value result end elsif arg.is_a?(Array) arg.map { |e| symbolize_keys(e) } else arg end end
tmp_dir()
click to toggle source
Directory for writing out files. @return [String]
# File lib/coolkit/paths.rb, line 16 def self.tmp_dir return File.join(self.root_dir, "tmp") end
to_est(time)
click to toggle source
Convert a `UTC` {Time} object to `EST`.
@param time [Time] @raise [ArgumentError] Unless passed a `UTC` formatted Time object. @return [Time]
# File lib/coolkit/time.rb, line 9 def self.to_est(time) raise ArgumentError "argument is not in UTC format" unless time.utc?() return time + Time.zone_offset("EST") end
write_to_file(file, data)
click to toggle source
Append data to the end of a file. The file is created if it doesn“t exist.
@param file [String] Path to the file. @param data [String,#to_s] Data to write to the file. @return [void]
# File lib/coolkit/filesystem.rb, line 10 def self.write_to_file(file, data) FileUtils.mkdir_p(File.dirname(file)) File.open(file, "a") { |f| f.write(data) } end