Class: ActiveRecord::ConnectionAdapters::VirtualTable
- Defined in:
- lib/rmtools/active_record/declarative.rb
Instance Method Summary collapse
- #change(*args) ⇒ Object
- #change_default(*args) ⇒ Object
- #column(name, *args) ⇒ Object
- #column_exists(*args) ⇒ Object
- #debug_str(meth, called, exist, *args) ⇒ Object
- #index(name, *args) ⇒ Object
- #index_exists(*indexes) ⇒ Object
-
#initialize(name, connection, map = nil) ⇒ VirtualTable
constructor
A new instance of VirtualTable.
- #map! ⇒ Object
- #references(*args) ⇒ Object (also: #belongs_to)
- #remove(*args) ⇒ Object
- #remove_index(options) ⇒ Object
- #remove_references(*args) ⇒ Object (also: #remove_belongs_to)
- #remove_timestamps ⇒ Object
- #rename(column_name, new_column_name) ⇒ Object
- #timestamps ⇒ Object
Constructor Details
#initialize(name, connection, map = nil) ⇒ VirtualTable
Returns a new instance of VirtualTable.
30 31 32 33 34 35 36 |
# File 'lib/rmtools/active_record/declarative.rb', line 30 def initialize name, connection, map=nil super name, connection case map when true; @map = [] when Array; @map = map end end |
Instance Method Details
#change(*args) ⇒ Object
80 81 82 |
# File 'lib/rmtools/active_record/declarative.rb', line 80 def change *args raise NotImplementedError, "don't use #change in declaration!" end |
#change_default(*args) ⇒ Object
84 85 86 |
# File 'lib/rmtools/active_record/declarative.rb', line 84 def change_default *args raise NotImplementedError, "don't use #change_default in declaration!" end |
#column(name, *args) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/rmtools/active_record/declarative.rb', line 46 def column name, *args to_be_called = !column_exists(name) super if to_be_called $log.debug {debug_str :column, to_be_called, !to_be_called, name, *args} @map << [name, *args] if @map end |
#column_exists(*args) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/rmtools/active_record/declarative.rb', line 13 def column_exists *args column_names = @base.columns(@table_name).names = args. names = args.dup args << _or_ = (names[0] == :all) ? !names.shift : true names.each {|name| return _or_ if name.to_s.in(column_names) == _or_} !_or_ end |
#debug_str(meth, called, exist, *args) ⇒ Object
9 10 11 |
# File 'lib/rmtools/active_record/declarative.rb', line 9 def debug_str meth, called, exist, *args "Table.#{meth}(#{args.inspects*', '}) was#{' NOT' if !called} called due to #{'in' if !exist}existance" end |
#index(name, *args) ⇒ Object
67 68 69 70 71 |
# File 'lib/rmtools/active_record/declarative.rb', line 67 def index name, *args to_be_called = !index_exists(name) super if to_be_called $log.debug {debug_str :index, to_be_called, !to_be_called, name, *args} end |
#index_exists(*indexes) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/rmtools/active_record/declarative.rb', line 23 def index_exists *indexes column_indexes = @base.indexes(@table_name).columnss.flatten _or_ = (indexes[0] == :all) ? !indexes.shift : true indexes.each {|index| return _or_ if index.to_s.in(column_indexes) == _or_} !_or_ end |
#map! ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/rmtools/active_record/declarative.rb', line 38 def map! map_names = @map.firsts.to_ss @base.columns(@table_name).names.each {|name| name.in(map_names) ? @map.reject! {|_| _[0] == name} : remove(name) } @map.each {|col| column *col} end |
#references(*args) ⇒ Object Also known as: belongs_to
94 95 96 97 98 |
# File 'lib/rmtools/active_record/declarative.rb', line 94 def references *args to_be_called = !column_exists(*args.map {|col| "#{col}_id"}) super if to_be_called $log.debug {debug_str :references, to_be_called, !to_be_called, *args} end |
#remove(*args) ⇒ Object
101 102 103 104 105 |
# File 'lib/rmtools/active_record/declarative.rb', line 101 def remove *args to_be_called = column_exists :all, *args super if to_be_called $log.debug {debug_str :remove, to_be_called, to_be_called, *args} end |
#remove_index(options) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/rmtools/active_record/declarative.rb', line 114 def remove_index indexes = .is(Hash) ? [:column] : raise ArgumentError, "can remove only default format named indexes in declaration!" if !indexes to_be_called = index_exists :all, *indexes super if to_be_called $log.debug {debug_str :remove_index, to_be_called, to_be_called, } end |
#remove_references(*args) ⇒ Object Also known as: remove_belongs_to
107 108 109 110 111 |
# File 'lib/rmtools/active_record/declarative.rb', line 107 def remove_references *args to_be_called = column_exists(:all, *args.map {|col| "#{col}_id"}) super if to_be_called $log.debug {debug_str :remove_references, to_be_called, to_be_called, *args} end |
#remove_timestamps ⇒ Object
122 123 124 125 126 |
# File 'lib/rmtools/active_record/declarative.rb', line 122 def to_be_called = column_exists 'created_at', 'updated_at' super if to_be_called $log.debug {debug_str :remove_timestamps, to_be_called, to_be_called} end |
#rename(column_name, new_column_name) ⇒ Object
88 89 90 91 92 |
# File 'lib/rmtools/active_record/declarative.rb', line 88 def rename column_name, new_column_name to_be_called = !column_exists(new_column_name) super if to_be_called $log.debug {debug_str :rename, to_be_called, !to_be_called, column_name, new_column_name} end |
#timestamps ⇒ Object
73 74 75 76 77 78 |
# File 'lib/rmtools/active_record/declarative.rb', line 73 def to_be_called = !column_exists('created_at', 'updated_at') super if to_be_called $log.debug {debug_str :timestamps, to_be_called, !to_be_called} @map.concat [[:created_at, :datetime], [:updated_at, :datetime]] if @map end |