Module: UtilityMm
- Defined in:
- lib/utility_mm.rb,
lib/utility_mm/version.rb
Constant Summary collapse
- VERSION =
"0.1.3"
Class Method Summary collapse
-
.create_tables(name) ⇒ Object
creates tables based off of a yml file.
-
.push_msg(_msg, _title) ⇒ Object
pushes a message to pushover must be connected.
-
.pushover_connect ⇒ Object
connects to pushover api.
- .sql_connect ⇒ Object
-
.underscore(_s) ⇒ Object
formats string for DW entry ex: “Hello Friend” => “hello_friend”.
-
.unpivot(csv, _titles, dim) ⇒ Object
unpivot method to unpivot csv.
Class Method Details
.create_tables(name) ⇒ Object
creates tables based off of a yml file
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# 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) ⇒ Object
pushes a message to pushover must be connected
75 76 77 |
# File 'lib/utility_mm.rb', line 75 def self.push_msg _msg, _title Pushover.notification(message: _msg, title: _title) end |
.pushover_connect ⇒ Object
connects to pushover api
67 68 69 70 71 72 |
# 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 ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# 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) ⇒ Object
formats string for DW entry ex: “Hello Friend” => “hello_friend”
24 25 26 27 28 29 30 31 32 33 |
# 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) ⇒ Object
unpivot method to unpivot csv
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# 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 |