Class: SmsOnRails::SchemaHelper
- Inherits:
-
Object
- Object
- SmsOnRails::SchemaHelper
- Defined in:
- lib/sms_on_rails/schema_helper.rb
Class Method Summary collapse
- .create(*files) ⇒ Object
- .drop(*files) ⇒ Object
- .drop_tables(file, options = {}) ⇒ Object
- .each_file(*files, &block) ⇒ Object
- .file_to_string(file, options = {}) ⇒ Object
- .parse_options(files) ⇒ Object
- .safe_code(str, options) {|str| ... } ⇒ Object
- .schema(command, *files) ⇒ Object
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, | file_to_string(file, ) } end |
.drop(*files) ⇒ Object
8 9 10 |
# File 'lib/sms_on_rails/schema_helper.rb', line 8 def drop(*files) each_file(*files) {|file, | drop_tables(file, )} 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, ={}) str = "\n" data = file_to_string(file) data.scan(/create_table\s+[":]([^\W]*)/) do table_name = $1.dup str << safe_code(str, ) { |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) = (files) files.inject('') {|str, f| str << yield(f, ); 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, ={}) 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 (files) = files.last.is_a?(Hash) ? files.pop : {} end |
.safe_code(str, options) {|str| ... } ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/sms_on_rails/schema_helper.rb', line 38 def safe_code(str, , &block) str = '' str << " begin\n " if [:safe] yield str str << " rescue Exception => e\n end\n" if [: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 |