Class: Sip::Config

Inherits:
Hash
  • Object
show all
Defined in:
lib/sip/config.rb

Constant Summary collapse

DBCONF_DEFAULT =
{ 
  'type' => 'mysql', 
  'host' => 'localhost',
  'dbport' => nil
}
TABLECONF_DEFAULT =
{ 
  'incremental_index' => 'id', 
  'method' => 'append', 
  'incremental_index_value' => 0, 
  'partition_by' => nil, 
  'columns' => nil 
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_values) ⇒ Config

Returns a new instance of Config.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sip/config.rb', line 24

def initialize(initial_values)
  # temp_keys are ones we'll delete before saving to a file
  @temp_keys = []
  merge! initial_values

  # initialize defaults, including setting dbname and tablename ease of use keys
  self['databases'].each { |dbname, dbconf|
    self['databases'][dbname] = DBCONF_DEFAULT.merge(dbconf)
    self['databases'][dbname]['dbname'] = dbname
    self['databases'][dbname]['tables'].each { |tablename, tableconf|
      tableconf = {'hive_table_name' => "#{dbname}_#{tablename}"}.merge(TABLECONF_DEFAULT).merge(tableconf)
      self['databases'][dbname]['tables'][tablename] = tableconf
      self['databases'][dbname]['tables'][tablename]['tablename'] = tablename
    }
  }
end

Class Method Details

.load_file(location) ⇒ Object



20
21
22
# File 'lib/sip/config.rb', line 20

def self.load_file(location)
  Config.new YAML.load_file(location)
end

Instance Method Details

#dbconf(dbname) ⇒ Object



57
58
59
# File 'lib/sip/config.rb', line 57

def dbconf(dbname)
  self['databases'][dbname]
end

#save_file(location) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sip/config.rb', line 41

def save_file(location)
  # remove unecessary dbname and tablename keys
  self['databases'].each { |dbname, dbconf|
    dbconf.delete 'dbname'
    dbconf['tables'].each { |tablename, tableconf|
      tableconf.delete 'tablename'
    }
  }

  File.open(location, 'w') { |f| 
    h = Hash.new.merge self
    @temp_keys.each { |k| h.delete k }
    YAML.dump(h, f) 
  }
end

#set_temp(other, keys) ⇒ Object



73
74
75
76
77
78
# File 'lib/sip/config.rb', line 73

def set_temp(other, keys)
  keys.each { |k|
    @temp_keys << k
    self[k] = other[k]
  }
end

#store_database(dbname, conf) ⇒ Object



65
66
67
# File 'lib/sip/config.rb', line 65

def store_database(dbname, conf)
  self['databases'][dbname] = conf
end

#store_table(dbname, tablename, conf) ⇒ Object



69
70
71
# File 'lib/sip/config.rb', line 69

def store_table(dbname, tablename, conf)
  self['databases'][dbname]['tables'][tablename] = conf
end

#tconf(dbname, tablename) ⇒ Object



61
62
63
# File 'lib/sip/config.rb', line 61

def tconf(dbname, tablename)
  dbconf(dbname)['tables'][tablename]
end