Class: SmsOnRails::SchemaHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/sms_on_rails/schema_helper.rb

Class Method Summary collapse

Class Method Details

.create(*files) ⇒ Object



4
5
6
# File 'lib/sms_on_rails/schema_helper.rb', line 4

def create(*files)
  each_file(*files) {|file, options| file_to_string(file, options) }
end

.drop(*files) ⇒ Object



8
9
10
# File 'lib/sms_on_rails/schema_helper.rb', line 8

def drop(*files)
  each_file(*files) {|file, options| drop_tables(file, options)}
end

.drop_tables(file, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/sms_on_rails/schema_helper.rb', line 28

def drop_tables(file, options={})
  str = "\n"
  data = file_to_string(file)
  data.scan(/create_table\s+[":]([^\W]*)/) do
    table_name = $1.dup
    str << safe_code(str, options) { |code| code << "    drop_table :#{table_name}\n" }
  end
  str
end

.each_file(*files, &block) ⇒ Object



12
13
14
15
# File 'lib/sms_on_rails/schema_helper.rb', line 12

def each_file(*files, &block)
  options = parse_options(files)
  files.inject('') {|str, f| str << yield(f, options); str }
end

.file_to_string(file, options = {}) ⇒ Object



24
25
26
# File 'lib/sms_on_rails/schema_helper.rb', line 24

def file_to_string(file, options={})
  File.read(File.join(File.dirname(__FILE__), "../../db/migrate/#{file}.rb"))
end

.parse_options(files) ⇒ Object



46
47
48
# File 'lib/sms_on_rails/schema_helper.rb', line 46

def parse_options(files)
  options = files.last.is_a?(Hash) ? files.pop : {}
end

.safe_code(str, options) {|str| ... } ⇒ Object

Yields:

  • (str)


38
39
40
41
42
43
44
# File 'lib/sms_on_rails/schema_helper.rb', line 38

def safe_code(str, options, &block)
  str = ''
  str << "  begin\n  " if options[:safe]
  yield str
  str << "  rescue Exception => e\n  end\n" if options[:safe]
  str
end

.schema(command, *files) ⇒ Object



17
18
19
20
21
22
# File 'lib/sms_on_rails/schema_helper.rb', line 17

def schema(command, *files)
  str = "ActiveRecord::Schema.define do\n"
  str << self.send(command, *files)
  str << "\nend"
  str
end