module UtilityMm
Constants
- VERSION
Public Class Methods
create_tables(name)
click to toggle source
creates tables based off of a yml file
# File lib/utility_mm.rb, line 36 def self.create_tables name config = YAML.load_file('config.yml') _config["objects"].each do |n,o| table_name = name indexes = o["indexes"] unless @db.table_exists?(table_name.to_sym) puts table_name.to_sym @db.create_table?(table_name.to_sym){ String :source } o["fields"].merge(o["keys"]).each do |field_name,cfg| @db.add_column table_name.to_sym, UtilityMm.underscore(field_name).to_sym, Object.const_get(cfg['kind']), cfg.has_key?('opts') ? cfg['opts'] : {} end _keys = o['keys'].collect { |k,v| UtilityMm.underscore(k).to_sym } @db.alter_table(table_name.to_sym) do add_primary_key _keys end indexes.each do |i| puts "creating index: #{i}" s="#{table_name}_#{i}_idx" @db.add_index table_name.to_sym, Sequel.identifier(i.to_sym), :name => (s.length > 64 ? "#{s[0,28]}-#{rand 100000..999999}-#{s[-28,28]}" : s) end unless indexes.nil? end end end
push_msg(_msg, _title)
click to toggle source
pushes a message to pushover must be connected
# File lib/utility_mm.rb, line 75 def self.push_msg _msg, _title Pushover.notification(message: _msg, title: _title) end
pushover_connect()
click to toggle source
connects to pushover api
# File lib/utility_mm.rb, line 67 def self.pushover_connect Pushover.configure do |config| config.user='uz5KiL8CCmTSTvSA5SZMXeBfUgdHce' config.token='aousJUNyemjFbFu5cio4vfrNHFoXRB' end end
sql_connect()
click to toggle source
# File lib/utility_mm.rb, line 12 def self.sql_connect dbconfig = YAML.load_file("dbconfig.yml") if @db.nil? dbconfig['db'].tap do |db| @db = Sequel.connect("mysql2://#{db['user']}:#{db['pass']}@#{db['host']}:#{db['port']}/#{db['schema']}", :pool_timeout=>30) end end return @db end
underscore(_s)
click to toggle source
formats string for DW entry ex: “Hello Friend” => “hello_friend”
# File lib/utility_mm.rb, line 24 def self.underscore _s _s.tr(" ", "_"). gsub(/\./, '__'). gsub(/::/, '/'). tr(":", "_"). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end
unpivot(csv, _titles, dim)
click to toggle source
unpivot method to unpivot csv
# File lib/utility_mm.rb, line 80 def self.unpivot csv, _titles, dim sheet = CSV.read(csv) headers = sheet[0] titles = headers.slice!(0..(_titles - 1)) titles.map! do |title| title = underscore title title = title.to_sym end dimensions = [] dim.times do |x| dimensions[x] = titles.zip(sheet[x+1].slice!(0..(_titles -1))).to_h end counter = sheet[0].count puts dimensions end