Class: Palmade::Cableguy::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/palmade/cableguy/migration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cabler) ⇒ Migration

Returns a new instance of Migration.



6
7
8
9
10
11
12
# File 'lib/palmade/cableguy/migration.rb', line 6

def initialize(cabler)
  @cabler = cabler

  @db = @cabler.db
  @cabling_path = @cabler.cabling_path
  @utils = Palmade::Cableguy::Utils
end

Instance Attribute Details

#cablerObject (readonly)

Returns the value of attribute cabler.



3
4
5
# File 'lib/palmade/cableguy/migration.rb', line 3

def cabler
  @cabler
end

#dbObject (readonly)

Returns the value of attribute db.



4
5
6
# File 'lib/palmade/cableguy/migration.rb', line 4

def db
  @db
end

Instance Method Details

#bootObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/palmade/cableguy/migration.rb', line 14

def boot
  @db.create_table_if_needed

  file_stack = []

  sort_directories.each do |p|
    path = File.join(@cabling_path, p)

    if p == 'targets'
      if !@cabler.location.nil? && @cabler.location != @cabler.target
        f = File.join(path, "#{@cabler.target}_#{@cabler.location}.rb")
      else
        f = File.join(path, "#{@cabler.target}.rb")
      end

      if File.exists?(f)
        require f
        file_stack.push(f)
      else
        raise "File #{f} doesn't exist!"
      end
    elsif p == '.'
      if File.exist?(File.join(path, 'custom.rb'))
        f = Dir["#{path}/custom.rb"].shift
        require f
        file_stack.push(f)
      end
    else
      Dir["#{path}/*.rb"].each do |d|
        require d
        file_stack.push(d)
      end
    end

    file_stack.each do |f|
      class_name = File.basename(f).chomp(".rb")
      camelized_class_name = (@utils.camelize(class_name))
      klass = Palmade::Cableguy::Migrations.const_get(camelized_class_name)
      k = klass.new(@cabler)
      k.migrate!
    end
    file_stack.clear
  end
end

#delete_key(key, group = nil) ⇒ Object



84
85
86
# File 'lib/palmade/cableguy/migration.rb', line 84

def delete_key(key, group = nil)
  @db.delete_key(key, group)
end

#globals(&block) ⇒ Object



76
77
78
# File 'lib/palmade/cableguy/migration.rb', line 76

def globals(&block)
  @db.globals(&block)
end

#group(group, &block) ⇒ Object



71
72
73
74
# File 'lib/palmade/cableguy/migration.rb', line 71

def group(group, &block)
  @group = group
  @db.group(@group, &block)
end

#migrate!Object



63
64
65
# File 'lib/palmade/cableguy/migration.rb', line 63

def migrate!
  raise "class #{self.class.name} doesn't have a migrate method. Override!"
end

#prefix(prefix, &block) ⇒ Object



80
81
82
# File 'lib/palmade/cableguy/migration.rb', line 80

def prefix(prefix, &block)
  @db.prefix(prefix, &block)
end

#set(key, value, set = nil) ⇒ Object



67
68
69
# File 'lib/palmade/cableguy/migration.rb', line 67

def set(key, value, set = nil)
  @db.set(key, value, set)
end

#sort_directoriesObject



59
60
61
# File 'lib/palmade/cableguy/migration.rb', line 59

def sort_directories
  dir_stack = ["base", "targets", '.']
end

#update(key, value) ⇒ Object



88
89
90
# File 'lib/palmade/cableguy/migration.rb', line 88

def update(key, value)
  @db.update(key, value)
end